简体   繁体   English

Spring Boot 1.5.4:在单元测试中排除配置类

[英]Spring Boot 1.5.4: exclude configuration class in unit test

I have a Spring Boot project, version 1.5.4, with a MongoDb configuration class:我有一个 Spring Boot 项目,版本 1.5.4,有一个 MongoDb 配置类:

@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());
}
}

In my integration test i want use Embedded Mongo ( https://github.com/flapdoodle-oss/de.flapdoodle.embed.mongo ).在我的集成测试中,我想使用Embedded Mongo ( https://github.com/flapdoodle-oss/de.flapdoodle.embed.mongo )。

The problem is that the MongoDb configuration class start before the initialitation of Embedded mongo and try to connect to the database, so my test fail.问题是MongoDb配置类在Embedded mongo初始化之前就启动了,尝试连接数据库,所以我的测试失败了。 If i remove the MongoConfig class, all test work well.如果我删除MongoConfig类,则所有测试都运行良好。

How can i exclude it only in my test execution?如何仅在我的测试执行中排除它?

Exclude the MongoDB autoconfiguration by using below annotation over your test class.通过在测试类上使用以下注释来排除 MongoDB 自动配置。

@EnableAutoConfiguration(exclude={MongoAutoConfiguration.class, MongoDataAutoConfiguration.class}) @EnableAutoConfiguration(exclude={MongoAutoConfiguration.class, MongoDataAutoConfiguration.class})

Then in the same path as your test class create a configuration class and define your mongo bean over there.然后在与您的测试类相同的路径中创建一个配置类并在那里定义您的 mongo bean。 This will get picked up during application start up这将在应用程序启动期间获取

**@Configuration
public class MockConfigurations {
@Bean
@Primary
public MongoTemplate getMongoTemplate() {
//define your bean
return mongoTemplate;
}
}**

Please refer the answers here.请参考这里的答案。 It has two ways of excluding configurations.它有两种排除配置的方法。

Spring boot: apply @Configuration to certain package only Spring boot:仅将@Configuration 应用于某些包

Update 1:更新 1:

Alternatively, the most efficient way that I can think of is to use Spring profiles and load the profile for the tests或者,我能想到的最有效的方法是使用 Spring 配置文件并为测试加载配置文件

Define your TestConfiguration class and import it your test class.定义您的 TestConfiguration 类并将其导入您的测试类。

@RunWith(SpringRunner.class)
@SpringBootTest
@Import(MyTestConfiguration.class)
public class MyTests {

    @Test
    public void exampleTest() {
        ...
    }

}

https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-testing.html#boot-features-testing-spring-boot-applications-detecting-config https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-testing.html#boot-features-testing-spring-boot-applications-detecting-config

Update 2: For EmbeddedMongoAutoConfiguration please refer the detailed answers here.更新 2:对于 EmbeddedMongoAutoConfiguration,请参阅此处的详细答案。

How do you configure Embedded MongDB for integration testing in a Spring Boot application? 您如何配置嵌入式 MongDB 以在 Spring Boot 应用程序中进行集成测试?

I solved it with this configuration on my test class:我在我的测试课上用这个配置解决了这个问题:

@RunWith(SpringRunner.class)
@ComponentScan({"it.app.server.dal","it.app.server.listener"})
@DataMongoTest() //mongoDB
public class ListenerTests {
   ...
}

The annotation @DataMongoTest() load my Embbedded MongoDb and with @ComponentScan i can just load the services and repositories wich i need in my test.注释@DataMongoTest()加载我的 Embedded MongoDb 并且使用@ComponentScan我可以加载我在测试中需要的服务和存储库。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM