IMPORTANT
You don't need this for Spring Boot >= 1.5.x, it's included now in Spring Boot itself, see: 41.3.10 Auto-configured Data MongoDB tests
datamongotest-autoconfigure-spring-boot
Provides a @DataMongoTest
for the automatic configuration of tests with Spring Boot 1.4+.
Note: I have proposed this as a PR for Spring Boot itself: Provide a @DataMongoTest similar to @DataJpaTest
Introduction
Spring Boot 1.4 brought a new feature called Auto-configured tests.
Thoses "slices" can be used when starting a full application auto-configuration is overkill for a specific tests.
The reference documentation has chapter on how to use them.
Stéphane Nicoll created a nice post on how to create your own slice, called Custom test slice with Spring Boot 1.4.
@DataMongoTest
uses his work to provide a custom test slice that works with pretty much the same way for Spring Data MongoDB as @DataJpaTest
does for Spring Data JPA.
How to use it
Add
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>eu.michael-simons</groupId>
<artifactId>datamongotest-autoconfigure-spring-boot</artifactId>
<version>0.0.1</version>
<scope>test</scope>
</dependency>
to your dependencies, create and write your MongoDB repositories in your Spring Boot Application as you did before.
Annotate your test with @DataMongoTest
and benefit from a MongoTemplate
and all your repositories:
@RunWith(SpringRunner.class)
@DataMongoTest
public class DataMongoSampleTests {
@Autowired
private MongoTemplate mongoTemplate;
@Autowired
private TweetRepository tweetRepository;
@Test
public void testStuff() {
TweetDocument tweet = new TweetDocument();
tweet.setText("Look, new @DataMongoTest!");
tweet = this.tweetRepository.save(tweet);
assertThat(tweet.getId(), notNullValue());
assertTrue(this.mongoTemplate.collectionExists("tweets"));
}
}
The automatic configuration takes your application.properties
into account. Make sure you configure another database connection for your test profile through
spring.data.mongodb.database = testdatabase
Or you might consider adding
<dependency>
<groupId>de.flapdoodle.embed</groupId>
<artifactId>de.flapdoodle.embed.mongo</artifactId>
<scope>test</scope>
</dependency>
to your test dependencies. This activates Spring Boot support for an embedded MongoDB process and you end up with an embedded Mongo connection like you do when using @DataJpaTest
where you get an H2 embedded database.
If you don't want this and but have embedded Mongo on your path, consider adding
@AutoConfigureEmbeddedTestMongod(enabled = false)
to your test as well.