2

I have a Spring Boot project, version 1.5.4, with a MongoDb configuration class:

@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).

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. If i remove the MongoConfig class, all test work well.

How can i exclude it only in my test execution?

sintetico82
  • 483
  • 1
  • 9
  • 24
  • I know this is not what your question is about, but take a look at testContainers : https://www.testcontainers.org/ it is the solution i use for my test (mainly integration testing) , a tiny tutorial here : https://areguig.github.io/test-springboot-apps-using-testContainers-and-spock/ – Akli REGUIG Oct 10 '17 at 08:10

3 Answers3

2

Exclude the MongoDB autoconfiguration by using below annotation over your test 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. This will get picked up during application start up

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

Please refer the answers here. It has two ways of excluding configurations.

Spring boot: apply @Configuration to certain package only

Update 1:

Alternatively, the most efficient way that I can think of is to use Spring profiles and load the profile for the tests

Define your TestConfiguration class and import it your test class.

@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

Update 2: For EmbeddedMongoAutoConfiguration please refer the detailed answers here.

How do you configure Embedded MongDB for integration testing in a Spring Boot application?

zeagord
  • 2,257
  • 3
  • 17
  • 24
0

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.

sintetico82
  • 483
  • 1
  • 9
  • 24