Skip to the content.

Table of Contents

About

Mongomery Spring - wrapper for Spring and Spring Boot tests that gives and annotation based configuration. There is also an example of Spring tests and Spring Boot tests

Install

Mongomery Spring is deployed to Maven Central so setup is pretty simple.

For Maven projects add

<dependency>
    <groupId>com.github.borsch</groupId>
    <artifactId>mongomery-spring</artifactId>
    <version>X.X.X</version>
</dependency>

For Gradle projects add

compile group: 'com.github.borsch', name: 'mongomery-spring', version: 'X.X.X'

Latest version can be found here

Usage

Basic example

@RunWith(SpringJUnit4ClassRunner.class)
@TestExecutionListeners({ 
    // .. other listeners ..
    MongomeryExecutionListener.class 
})
class MyTestClass {

    @DatabaseMongoSetup("databaseSetup.json")           // pre-setup MongoDB state
    @ExpectedMongoDatabase("expectedAfterSave.json")    // asserts MongoDB state after test
    public void test() {
        // .. test code ..
    }

}

Embedded MongoDB server

You can also setup tests to run against embedded database

<dependency>
    <groupId>de.flapdoodle.embed</groupId>
    <artifactId>de.flapdoodle.embed.mongo</artifactId>
    <version>version</version>
    <scope>test</scope>
</dependency>
@Bean
MongoClient mongoClient() {
    try {
        MongodForTestsFactory factory = MongodForTestsFactory.with(Version.Main.V3_3);
        return factory.newMongo();
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

TimeZone

For Embedded database work of LocalDateTime & LocalDate highly depends on TimeZone. To set UTC timezone do the following for tests

TimeZone.setDefault(TimeZone.getTimeZone("UTC"));