我有一个春天启动的项目,1.5.4版配置类,用MongoDB的配置类:春季启动1.5.4:排除单元测试
@Configuration
public class MongoConfig {
@Value("${spring.data.mongo.client.uri:mongodb://localhost:27017/database}")
private String mongoURI;
@Bean
public MongoDbFactory mongoFactory() throws UnknownHostException{
return new SimpleMongoDbFactory(new MongoClientURI(mongoURI));
}
@Bean
public MongoTemplate mongoTemplate() throws UnknownHostException, MongoException{
return new MongoTemplate(mongoFactory());
}
}
在我的集成测试,我想使用嵌入式蒙戈(https://github.com/flapdoodle-oss/de.flapdoodle.embed.mongo)。
的问题是,嵌入式蒙戈的initialitation前MongoDB的配置类开始,并尝试连接到数据库,所以我的测试失败。如果我删除了MongoConfig类,所有测试都很好。
我怎么能排除它只是在我的测试执行?
我知道这是不是你的问题是什么,但看看testContainers:https://www.testcontainers.org/这是解决方案,我用我的测试(主要是集成测试),一个小教程在这里:https://areguig.github.io/test-springboot-apps-using-testContainers-and-spock/ –