Spring boot测试:无法实例化内部配置类

Dau*_*aud 0 java junit spring spring-boot spring-boot-test

我想在不涉及主要 Spring 配置的情况下JUnit对我的层运行测试。DAO因此,我声明了一个用 注释的内部类,@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);

    }

}
Run Code Online (Sandbox Code Playgroud)

但我收到错误:

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.
Run Code Online (Sandbox Code Playgroud)

Abd*_*ssi 6

任何嵌套配置类都必须声明为 static。所以你的代码应该是:

@Configuration
static class TestConfiguration{

    @Bean
    public InterviewInformationDao getInterviewInformationDao(){
        return new InterviewInformationDaoImpl();
    }
}
Run Code Online (Sandbox Code Playgroud)