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?

1
  • I know this is not what your question is about, but take a look at testContainers : testcontainers.org it is the solution i use for my test (mainly integration testing) , a tiny tutorial here : areguig.github.io/… Commented Oct 10, 2017 at 8:10

3 Answers 3

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;
}
}**
Sign up to request clarification or add additional context in comments.

Comments

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?

3 Comments

I don't want change my main Application class just for the unit test.
@Import(MyTestsConfiguration.class) is good solution, but i found a system that just avoid load useless class for my test.
The proble is that i don't know how to be my MyTestsConfiguration.class, becouse i want activate the EmbeddedMongoAutoConfiguration.class
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.

1 Comment

This solution doesn't work if i want test a Controller that access to mongodb.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.