Spring boot test: Unable to instantiate inner configuration class

故事扮演 提交于 2019-12-11 15:29:31

问题


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.

This is the code:

@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);

    }

}

But I'm getting the error:

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.

回答1:


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();
    }
}


来源:https://stackoverflow.com/questions/55142962/spring-boot-test-unable-to-instantiate-inner-configuration-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!