Package fr.ght1pc9kc.testy.mongo
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
,org.junit.jupiter.api.extension.TestInstantiationAwareExtension
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();
...
}
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic class
static final class
Builder for the extension classWithMongoData
.Nested classes/interfaces inherited from interface org.junit.jupiter.api.extension.TestInstantiationAwareExtension
org.junit.jupiter.api.extension.TestInstantiationAwareExtension.ExtensionContextScope
-
Method Summary
Modifier and TypeMethodDescriptionvoid
beforeAll
(org.junit.jupiter.api.extension.ExtensionContext context) void
beforeEach
(org.junit.jupiter.api.extension.ExtensionContext context) builder
(WithEmbeddedMongo wEmbeddedMongo) Create aWithMongoData.WithMongoDataBuilder
for the extension.resolveParameter
(org.junit.jupiter.api.extension.ParameterContext parameterContext, org.junit.jupiter.api.extension.ExtensionContext extensionContext) boolean
supportsParameter
(org.junit.jupiter.api.extension.ParameterContext parameterContext, org.junit.jupiter.api.extension.ExtensionContext context) Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface org.junit.jupiter.api.extension.TestInstantiationAwareExtension
getTestInstantiationExtensionContextScope
-
Method Details
-
beforeAll
public void beforeAll(org.junit.jupiter.api.extension.ExtensionContext context) - Specified by:
beforeAll
in interfaceorg.junit.jupiter.api.extension.BeforeAllCallback
-
beforeEach
public void beforeEach(org.junit.jupiter.api.extension.ExtensionContext context) - Specified by:
beforeEach
in interfaceorg.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 interfaceorg.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 interfaceorg.junit.jupiter.api.extension.ParameterResolver
-
builder
Create aWithMongoData.WithMongoDataBuilder
for the extension.- Parameters:
wEmbeddedMongo
- Embedded mongo database.- Returns:
WithMongoData.WithMongoDataBuilder
.
-