Auto-configured Data MongoDB Tests
You can use @DataMongoTest
to test MongoDB applications.
By default, it configures a MongoTemplate
, scans for @Document
classes, and configures Spring Data MongoDB repositories.
Regular @Component
and @ConfigurationProperties
beans are not scanned when the @DataMongoTest
annotation is used.
@EnableConfigurationProperties
can be used to include @ConfigurationProperties
beans.
(For more about using MongoDB with Spring Boot, see "MongoDB".)
A list of the auto-configuration settings that are enabled by @DataMongoTest can be found in the appendix.
|
The following class shows the @DataMongoTest
annotation in use:
-
Java
-
Kotlin
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.data.mongo.DataMongoTest;
import org.springframework.data.mongodb.core.MongoTemplate;
@DataMongoTest
class MyDataMongoDbTests {
@Autowired
private MongoTemplate mongoTemplate;
// ...
}
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.autoconfigure.data.mongo.DataMongoTest
import org.springframework.data.mongodb.core.MongoTemplate
@DataMongoTest
class MyDataMongoDbTests(@Autowired val mongoTemplate: MongoTemplate) {
// ...
}