Skip to content

Latest commit

 

History

History
100 lines (79 loc) · 3.15 KB

README.md

File metadata and controls

100 lines (79 loc) · 3.15 KB

testy-mongo-box

This project is used to test MongoDB repositories. It provides extensions to use an embedded Mongo database:

WithEmbeddedMongo

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) {
    // (...)
}

WithMongoData

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();

Performances enhancement with dbTracker

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.

Using custom Object Mapper

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();