This project is used to test MongoDB repositories. It provides extensions to use an embedded Mongo database:
- WithEmbeddedMongo initializes the embbeded Mongo database.
- WithMongoData inserts test data into the database.
This extension starts an embedded MongoDB.
@RegisterExtension
static final WithEmbeddedMongo wMongo = WithEmbeddedMongo
.builder()
.setDatabaseName("my_database")
.build();
With this extension, MongoClient
, ReactiveMongoDatabaseFactory
and ReactiveMongoTemplate
can be injected as parameters.
@BeforeEach
void setUp(MongoClient mongoClient,
ReactiveMongoDatabaseFactory factory,
ReactiveMongoTemplate mongoTemplate) {
// (...)
}
This extension resets the content of the collections before each test method. The data of a collection can be defined by implementing MongoDataSet.
public class MyElementDataSet implements MongoDataSet<MyElement> {
@Override
public List<MyElement> documents() {
// List the objects to be inserted in the collection
}
}
Each data set can be associated with a specific collection with the extension.
private static final WithEmbeddedMongo wMongo = WithEmbeddedMongo
.builder()
.build();
private static final WithMongoData wMongoData = WithMongoData
.builder(wMongo)
.addDataset("my_element_collection", new MyElementDataSet())
.build();
@RegisterExtension
static final ChainedExtension wExtensions = ChainedExtension
.outer(wMongo)
.append(wMongoData)
.register();
If you have lot of tests and you don't want the all database was reset on each test. You can use the db Tracker.
@Test
void should_have_read_data(WithMongoData.Tracker tracker) {
tracker.skipNextSampleLoad();
// You read only test code
}
The next test will not drop and reload the DataSets.
Optionally, a specific mapper can be used to convert objects to Mongo Documents by including the extension WithObjectMapper.
private static final WithEmbeddedMongo wMongo = WithEmbeddedMongo
.builder()
.build();
private static final WithObjectMapper wObjectMapper = WithObjectMapper
.builder()
.addModule(new JavaTimeModule())
.build();
private static final WithMongoData wMongoData = WithMongoData
.builder(wMongo)
.withObjectMapper(wObjectMapper)
.addDataset("my_element_collection", new MyElementDataSet())
.build();
@RegisterExtension
static final ChainedExtension wExtensions = ChainedExtension
.outer(wMongo)
.append(wObjectMapper)
.append(wMongoData)
.register();