【问题标题】:Spring boot test: Unable to instantiate inner configuration classSpring Boot 测试:无法实例化内部配置类
【发布时间】:2019-08-04 04:38:34
【问题描述】:

我想在不涉及我的主要 Spring 配置的情况下为我的 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.

【问题讨论】:

    标签: java spring spring-boot junit spring-boot-test


    【解决方案1】:

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

    @Configuration
    static class TestConfiguration{
    
        @Bean
        public InterviewInformationDao getInterviewInformationDao(){
            return new InterviewInformationDaoImpl();
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2018-08-20
      • 1970-01-01
      • 1970-01-01
      • 2018-03-08
      • 1970-01-01
      • 2018-12-03
      • 2014-07-30
      • 2020-03-24
      • 1970-01-01
      相关资源
      最近更新 更多