我想对我的DAO
层进行JUnit
测试,而无需涉及我的主要弹簧配置。因此,我声明了一个用@Configuration
注释的内部类,以便覆盖用@SpringBootApplication
注释的主应用程序类的配置。
这是代码:
@RunWith(SpringRunner.class)
@JdbcTest
public class InterviewInformationControllerTest {
@Configuration
class TestConfiguration{
@Bean
public InterviewInformationDao getInterviewInformationDao(){
return new InterviewInformationDaoImpl();
}
}
@Autowired
private InterviewInformationDao dao;
@Test
public void testCustomer() {
List<Customer> customers = dao.getCustomers();
assertNotNull(customers);
assertTrue(customers.size() == 4);
}
}
但是我遇到了错误:
Parameter 0 of constructor in com.test.home.controller.InterviewInformationControllerTest$TestConfiguration required a bean of type 'com.test.home.controller.InterviewInformationControllerTest' that could not be found.
任何嵌套配置类都必须声明为静态。因此您的代码应该是:
@Configuration
static class TestConfiguration{
@Bean
public InterviewInformationDao getInterviewInformationDao(){
return new InterviewInformationDaoImpl();
}
}