Integration Testing with Java and MongoDB Using Testcontainers
For the complete MongoDB documentation index, see www.mongodb.com/docs/llms.txt
Integration Testing with Java and MongoDB Using Testcontainers
Overview
Testcontainers is a Java library that uses Docker containers to simplify integration testing. It creates temporary database instances that run during your tests and removes them automatically when tests finish.
This tutorial shows you how to use Testcontainers with MongoDB in both standard Java and Spring Boot applications.
Integration Testing and Testcontainers
Integration testing checks how different parts of your application work together. Unlike unit tests that validate individual pieces of code, integration tests verify that your code correctly interacts with external systems, such as databases or APIs.
Testcontainers performs the following steps when running tests:
-
Before tests: Creates the container, loads the database, and creates collections
-
During tests: Runs your test code
-
After tests: Removes the container and frees resources
Tests that use Testcontainers run the same way on any computer and any operating system, and each test uses its own database instance. You can also switch between different database versions when necessary.
Prerequisites
Before you begin, install the following:
-
Java version 22. To download, see the Oracle website.
-
Maven version 3.9.6. To download, see the Maven website.
-
Docker version 26.0.0. To download, see the Docker website.
You must also add the following dependencies to your application's pom.xml file:
<dependencyManagement>
<dependencies>
<!-- MongoDB Java Driver BOM -->
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongodb-driver-bom</artifactId>
<type>pom</type>
<scope>import</scope>
</dependency>
<!-- Testcontainers BOM -->
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>testcontainers-bom</artifactId>
<type>pom</type>
<scope>import</scope>
</dependency>
<!-- JUnit BOM -->
<dependency>
<groupId>org.junit</groupId>
<artifactId>junit-bom</artifactId>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
Use Testcontainers with Java
The following example tests a query that finds movies with IMDb ratings above 7.0. To view the complete code, see the GitHub repository.
- Create the Container
The following code creates a MongoDB container. The @Container annotation starts the container before tests and stops it after tests finish:
java
@Container
private static final MongoDBContainer mongoDBContainer = new MongoDBContainer("mongo:7.0.0");
- Add Test Data
The following code connects to the container and adds sample movies:
```java @BeforeAll public void setup() { String uri = mongoDBContainer.getConnectionString(); mongoClient = MongoClients.create(uri); database = mongoClient.getDatabase("testdb"); collection = database.getCollection("testCollection");
// Insert sample data
Document doc1 = new Document("title", "Inception").append("imdb", new Document("rating", 8.8));
Document doc2 = new Document("title", "The Room").append("imdb", new Document("rating", 3.7));
Document doc3 = new Document("title", "The Dark Knight").append("imdb", new Document("rating", 9.0));
collection.insertMany(List.of(doc1, doc2, doc3));
} ```
- Write the Test
The following code runs a query for movies with an imdb.rating field value greater than 7 and checks the results:
```java
@Test
void testMoviesWithHighRating() {
List
assertEquals(2, resultDocuments.size());
for (Document doc : resultDocuments) {
assertTrue(doc.containsKey("title"));
assertFalse(doc.containsKey("_id"));
}
} ```
This test verifies the following:
-
The query returns exactly 2 documents
-
Each document has a
titlefield -
Each document excludes the
_idfield
Use Testcontainers with Spring Boot
The following example uses code from the Spring Boot MongoDB Aggregations repository to test a findAll() method.
- Create the Container
The following code creates a MongoDB container:
java
@Container
public static MongoDBContainer mongoDBContainer = new MongoDBContainer(DockerImageName.parse("mongo:7.0.0"));
- Configure Spring Boot
The following code configures Spring Boot to use the test database:
```java @DynamicPropertySource static void setProperties(DynamicPropertyRegistry registry) { registry.add("spring.data.mongodb.uri", mongoDBContainer::getReplicaSetUrl); registry.add("spring.data.mongodb.database", () -> "testdb"); }
@BeforeAll public static void setUpAll() { String mongoUri = mongoDBContainer.getConnectionString(); mongoClient = MongoClients.create(mongoUri); }
@BeforeEach public void setUp() { salesRepository.save(createMockSales()); }
private Sales createMockSales() {
Item item1 = new Item("Item1", Arrays.asList("Tag1", "Tag2"), new BigDecimal("19.99"), 2);
Item item2 = new Item("Item2", Arrays.asList("Tag3", "Tag4"), new BigDecimal("29.99"), 1);
List
Customer customer = new Customer("Male", 30, "customer@example.com", 5);
return new Sales(new ObjectId(), new Date(), items, "Store1", customer, true, "Online");
} ```
- Write the Test
The following code tests the findAll() method:
java
@Test
public void testFindAll() {
List<Sales> salesList = salesRepository.findAll();
assertThat(salesList).isNotEmpty();
assertThat(salesList.size()).isEqualTo(1);
Sales sales = salesList.get(0);
assertThat(sales.getStoreLocation()).isEqualTo("Store1");
assertThat(sales.getCustomer().getEmail()).isEqualTo("customer@example.com");
assertThat(sales.getItems()).hasSize(2);
}
This test verifies the following:
-
The query returns exactly 2 documents
-
Each document has a
titlefield -
Each document excludes the
_idfield
