Class WithMongoData

java.lang.Object
fr.ght1pc9kc.testy.mongo.WithMongoData
All Implemented Interfaces:
org.junit.jupiter.api.extension.BeforeAllCallback, org.junit.jupiter.api.extension.BeforeEachCallback, org.junit.jupiter.api.extension.Extension, org.junit.jupiter.api.extension.ParameterResolver

public final class WithMongoData extends Object implements org.junit.jupiter.api.extension.BeforeEachCallback, org.junit.jupiter.api.extension.BeforeAllCallback, org.junit.jupiter.api.extension.ParameterResolver
Extension allowing to initialize a mongo database with data.

Example of use with model:


 @Builder
 @Value
 public class User {

     private final String id;
     private final String login;
     private final String firstName;
     private final String lastName;
     private final String password;

 }
 

Implementation of the data set:


 public class UserDataSet implements MongoDataSet<User> {
     @Override
     public List<User> documents() {
         final User user = User.builder()
                 .id("generated-id")
                 .firstName("Obiwan")
                 .lastName("Kenobi")
                 .login("okenobi")
                 .password("110812f67fa1e1f0117f6f3d70241c1a42a7b07711a93c2477cc516d9042f9db")
                 .build();
         return ImmutableList.of(user);
     }
 }
 

Use of the extension. Before each test, the mongo test database will contain the documents of the data set:

 public class UserMongoRepositoryTest {

     private static final String USER_COLLECTION = "user";

     private static final WithEmbeddedMongo WITH_EMBEDDED_MONGO = WithEmbeddedMongo.builder()
             .build();
     private static final WithObjectMapper WITH_OBJECT_MAPPER = WithObjectMapper.builder()
             .addMixin(User.class, UserMongoMixin.class)
             .build();
     private static final WithMongoData WITH_MONGO_DATA = WithMongoData.builder(WITH_EMBEDDED_MONGO)
             .withObjectMapper(WITH_OBJECT_MAPPER)
             .addDataset(USER_COLLECTION, new UserDataSet())
             .build();

     @RegisterExtension
     static final ChainedExtension CHAIN = ChainedExtension.outer(WITH_EMBEDDED_MONGO)
             .append(WITH_OBJECT_MAPPER)
             .append(WITH_MONGO_DATA)
             .register();

     // (...)
 }
 

Database Tracker

For performance reasons, the lib provides a WithMongoData.Tracker that lets the extension know that the database has not been modified, and that it is therefore not necessary to recreate all the collections.


 @Test
  void should_read_data(WithMongoData.Tracker tracker) {
      tracker.skipNextSampleLoad();
      ...
  }
 
  • Method Details

    • beforeAll

      public void beforeAll(org.junit.jupiter.api.extension.ExtensionContext context)
      Specified by:
      beforeAll in interface org.junit.jupiter.api.extension.BeforeAllCallback
    • beforeEach

      public void beforeEach(org.junit.jupiter.api.extension.ExtensionContext context)
      Specified by:
      beforeEach in interface org.junit.jupiter.api.extension.BeforeEachCallback
    • supportsParameter

      public boolean supportsParameter(org.junit.jupiter.api.extension.ParameterContext parameterContext, org.junit.jupiter.api.extension.ExtensionContext context)
      Specified by:
      supportsParameter in interface org.junit.jupiter.api.extension.ParameterResolver
    • resolveParameter

      public Object resolveParameter(org.junit.jupiter.api.extension.ParameterContext parameterContext, org.junit.jupiter.api.extension.ExtensionContext extensionContext)
      Specified by:
      resolveParameter in interface org.junit.jupiter.api.extension.ParameterResolver
    • builder

      public static WithMongoData.WithMongoDataBuilder builder(WithEmbeddedMongo wEmbeddedMongo)
      Create a WithMongoData.WithMongoDataBuilder for the extension.
      Parameters:
      wEmbeddedMongo - Embedded mongo database.
      Returns:
      WithMongoData.WithMongoDataBuilder.