Add slide test annotation for MongoDB

This commits adds a slice test infrastructure for MongoDB, similar to
what `@DataJpaTest` does with JPA.

By default, an embedded Mongod process is used if available.

See gh-7600
This commit is contained in:
Michael J. Simons
2016-12-08 12:13:54 +01:00
committed by Stephane Nicoll
parent b19d31e067
commit 8648520876
15 changed files with 578 additions and 1 deletions

View File

@@ -1,5 +1,5 @@
= Spring Boot Reference Guide
Phillip Webb; Dave Syer; Josh Long; Stéphane Nicoll; Rob Winch; Andy Wilkinson; Marcel Overdijk; Christian Dupuis; Sébastien Deleuze
Phillip Webb; Dave Syer; Josh Long; Stéphane Nicoll; Rob Winch; Andy Wilkinson; Marcel Overdijk; Christian Dupuis; Sébastien Deleuze; Michael Simons
:doctype: book
:toc:
:toclevels: 4

View File

@@ -5685,6 +5685,44 @@ A list of the auto-configuration that is enabled by `@JdbcTest` can be
[[boot-features-testing-spring-boot-applications-testing-autoconfigured-mongo-test]]
==== Auto-configured Data MongoDB tests
`@DataMongoTest` is similar to `@DataJpaTest` but for Spring Data MongoDB tests.
By default it will configure an in-memory Mongod if `de.flapdoodle.embed:de.flapdoodle.embed.mongo`
is available, configure a `MongoTemplate`, scan for `@Document` classes and
configure Spring Data MongoDB repositories. Regular `@Component` beans will not
be loaded into the `ApplicationContext`:
[source,java,indent=0]
----
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.data.mongo.DataMongoTest;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@DataMongoTest
public class ExampleDataMongoTests {
@Autowired
private MongoTemplate mongoTemplate;
// ...Your autowired Mongo repositories
}
----
The If you want to use `@DataMongoTest` but not the embedded Mongod even if you have `de.flapdoodle.embed:de.flapdoodle.embed.mongo` on the class path, exclude its auto-configuration like so:
[source,java,indent=0]
----
@DataMongoTest(excludeAutoConfiguration = EmbeddedMongoAutoConfiguration.class)
----
A list of the auto-configuration that is enabled by `@DataMongoTest` can be
<<appendix-test-auto-configuration#test-auto-configuration,found in the appendix>>.
[[boot-features-testing-spring-boot-applications-testing-autoconfigured-rest-client]]
==== Auto-configured REST clients
The `@RestClientTest` annotation can be used if you want to test REST clients. By default