本文介绍了春季启动测试:无法实例化内部配置类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在不涉及我的主要Spring配置的情况下为我的DAO
层运行JUnit
测试.这样,我声明了一个用@Configuration
注释的内部类,以便它可以覆盖用@SpringBootApplication
注释的主应用程序类的配置.
I want to run JUnit
tests for my DAO
layer without involving my main Spring configurations. As such, I declared an inner class annotated with @Configuration
so that it would override the configurations of the main application class annotated with @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.
推荐答案
任何嵌套配置类都必须声明为静态.因此您的代码应为:
Any nested configuration classes must be declared as static. So your code should be :
@Configuration
static class TestConfiguration{
@Bean
public InterviewInformationDao getInterviewInformationDao(){
return new InterviewInformationDaoImpl();
}
}
这篇关于春季启动测试:无法实例化内部配置类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!