【发布时间】:2018-03-13 03:09:39
【问题描述】:
我正在为 Spring Boot 应用程序编写集成测试。只要我使用 100% 运行时配置进行测试,一切都会顺利进行。但是当我试图为 bean 提供一个自定义 bean 时,一切都会中断。
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class CombinedControllerIntegrationTest2 {
@TestConfiguration
static class ContextConfiguration {
@Bean
@Primary
public SolrDocumentTypeMapRepository solrDocumentTypeMapRepository() {
LOG.debug("SolrDocumentTypeMapRepository is being initialized.");
// etc.
上面的代码变体导致加载真实的运行时 SolrDocumentTypeMapRepository。我的测试类中的 ContextConfiguration 被忽略了。
如果我尝试在我的内部 ContextConfiguration 上使用 @Configuration 而不是 @TestConfiguration,则执行会落到另一个极端 - 它以
结束org.springframework.context.ApplicationContextException:无法 由于缺少而启动 EmbeddedWebApplicationContext EmbeddedServletContainerFactory bean。
因为显然没有加载其余配置。
如果我尝试放
@ContextConfiguration(类 = {CombinedControllerIntegrationTest2.ContextConfiguration.class,GatewayApplication.class})
在我的主要测试类中,它以与 #1 相同的方式失败 - 即我的 ContextConfiguration 被忽略。
有什么想法吗?
附:我知道我可以使用@MockBean(这甚至在其他情况下也有效),但是在这里,因为在某些时候我依赖于主代码中的@PostConsruct 方法,@MockBeans 没用。
【问题讨论】:
-
我遇到了与此处发布的类似问题stackoverflow.com/questions/45522778/…
标签: java spring testing spring-boot integration-testing