The extension allows to verify number of executed queries to mongo db during a spring-boot test execution.
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>
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()));
}
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.
This project is licensed under the Apache Licence 2.0. See the LICENSE file for more details.