/spring-mongodb-junit5

Spring extension to verify mongo db queries made in test method

Primary LanguageJavaApache License 2.0Apache-2.0

Spring Mongodb Junit5 Extension

The extension allows to verify number of executed queries to mongo db during a spring-boot test execution.

Installation

Add the dependency in your pom.xml

<dependency>
    <groupId>io.github.vkn</groupId>
    <artifactId>spring-mongodb-junit5</artifactId>
    <version>0.0.2</version>
    <scope>test</scope>
</dependency>

Usage

To get started, annotate your test class with @ExtendWith(MongoDbUnitExtension.class) and a test method with @MongoDbQueryTest. See BookControllerTest in integration-tests module and java docs of @MongoDbQueryTest for more information.

Example test:

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@ExtendWith(MongoDbUnitExtension.class)
class BookControllerTest {

    @LocalServerPort
    private int port;

    @BeforeEach
    void setUp() {
        RestAssured.port = port;
        TestUtils.insertBooks("/books");
    }

    @AfterEach
    void tearDown() {
        TestUtils.deleteBooks("/books");
    }

    @Test
    @MongoDbQueryTest(exactly = 1, collection = "my-collection")
    public void exactly() {
        given()
                .when().get("/books")
                .then()
                .statusCode(200)
                .body(not(empty()));
    }
}

Repeated annotations are also supported:

@Test
@MongoDbQueryTest(exactly = 1, commandName = "find")
@MongoDbQueryTest(exactly = 0, commandName = "delete")
public void exactly() {
    given()
            .when().get("/books")
            .then()
            .statusCode(200)
            .body(not(empty()));
}

Contributing

Contributions are welcome! If you have suggestions for improvements or encounter any issues, please feel free to open an issue or submit a pull request.

License

This project is licensed under the Apache Licence 2.0. See the LICENSE file for more details.