gpt4 book ai didi

java - Spring 启动测试: Unable to instantiate inner configuration class

转载 作者:行者123 更新时间:2023-12-01 19:41:47 25 4
gpt4 key购买 nike

我想为我的 DAO 层运行 JUnit 测试,而不涉及我的主要 Spring 配置。因此,我声明了一个用 @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();
}
}

关于java - Spring 启动测试: Unable to instantiate inner configuration class,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55142962/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com