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 classstatic final classBuilder 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 TypeMethodDescriptionvoidbeforeAll(org.junit.jupiter.api.extension.ExtensionContext context) voidbeforeEach(org.junit.jupiter.api.extension.ExtensionContext context) builder(WithEmbeddedMongo wEmbeddedMongo) Create aWithMongoData.WithMongoDataBuilderfor the extension.resolveParameter(org.junit.jupiter.api.extension.ParameterContext parameterContext, org.junit.jupiter.api.extension.ExtensionContext extensionContext) booleansupportsParameter(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, waitMethods 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:
beforeAllin interfaceorg.junit.jupiter.api.extension.BeforeAllCallback
-
beforeEach
public void beforeEach(org.junit.jupiter.api.extension.ExtensionContext context) - Specified by:
beforeEachin 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:
supportsParameterin 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:
resolveParameterin interfaceorg.junit.jupiter.api.extension.ParameterResolver
-
builder
Create aWithMongoData.WithMongoDataBuilderfor the extension.- Parameters:
wEmbeddedMongo- Embedded mongo database.- Returns:
WithMongoData.WithMongoDataBuilder.
-