我有一個春天啓動的項目,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/ –