我尝试从Spring Boot 1.4
升级到1.5.2
。据我所知,我已遵循更新指南。
实际上有两个问题,主要与嵌入式mongo有关:
现在必须明确创建EmbeddedServletContainerFactory
在测试配置类中,我必须添加:
@Bean
public EmbeddedServletContainerFactory servletContainer() {
TomcatEmbeddedServletContainerFactory factory = new TomcatEmbeddedServletContainerFactory();
factory.setPort(port);
factory.setSessionTimeout(50, TimeUnit.MINUTES);
return factory;
}
我以前没必要这样做。我必须包含它,因为当我运行测试时它会给我这个错误:
Caused by: org.springframework.context.ApplicationContextException: Unable to start EmbeddedWebApplicationContext due to missing EmbeddedServletContainerFactory bean.
嵌入式Mongo无法启动
现在我收到了这个错误:
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'embeddedMongoServer' available
不知道我应该在哪里添加这个bean(以及为什么我应该添加它)
我当前的设置
我有多个扩展BaseIT
的测试,其开头如下:
@RunWith(SpringRunner.class)
@DataMongoTest
@SpringBootTest(classes = {TestConfig.class, TestMongoConfig.class},, webEnvironment= SpringBootTest.WebEnvironment.DEFINED_PORT, value = "
{$server.port}")
@ActiveProfiles("test")
public abstract class BaseIT {...}
Mongo配置类:
@Configuration
@EnableAutoConfiguration(exclude = { EmbeddedMongoAutoConfiguration.class })
@EnableMongoRepositories(basePackages = "...")
public class TestMongoConfig {
@Bean public MongoClient mongo(){...}
@Bean public MongodProcess mongodProcess(){...}
@Bean public MongodExecutable mongodExecutable(){...}
@Bean public IMongodConfig mongodConfig(){...}
@Bean public MongodStarter mongodStarter(){...}
}
测试配置类:
@Configuration
@EnableAutoConfiguration(exclude = {EmbeddedMongoAutoConfiguration.class})
@ComponentScan(value = {...})
@EnableMongoRepositories(basePackages = {...)
@EnableWebMvc
public class TestConfig extends WebMvcConfigurerAdapter {...}