feat: add integration tests for ApartmentRepository using Testcontainers with MongoDB

This commit is contained in:
nessar authored and Nessar committed 2026-08-17 14:18:15 +02:00
1 parent 01fa812089
commit f876639c02
2 files changed
+86

No files matched your search

+16
View File
@@ -81,6 +81,11 @@
<artifactId>spring-security-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>testcontainers-mongodb</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
@@ -107,6 +112,17 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<artifactId>maven-failsafe-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
@@ -0,0 +1,70 @@
package fr.nessar.lmnporganizer.backend.integration;
import fr.nessar.lmnporganizer.backend.model.Apartment;
import fr.nessar.lmnporganizer.backend.repository.ApartmentRepository;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.autoconfigure.AutoConfigurationPackage;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.mongodb.repository.config.EnableMongoRepositories;
import org.springframework.test.context.DynamicPropertyRegistry;
import org.springframework.test.context.DynamicPropertySource;
import org.testcontainers.mongodb.MongoDBContainer;
import static org.assertj.core.api.Assertions.assertThat;
@SpringBootTest(
classes = ApartmentPersistenceIT.MongoTestApplication.class,
webEnvironment = SpringBootTest.WebEnvironment.NONE
)
class ApartmentPersistenceIT {
private static final MongoDBContainer mongoDb = startMongoDb();
@Autowired
private ApartmentRepository apartmentRepository;
@BeforeEach
@AfterEach
void clearApartments() {
apartmentRepository.deleteAll();
}
@DynamicPropertySource
static void configureMongoDb(DynamicPropertyRegistry registry) {
if (mongoDb != null) {
registry.add("spring.mongodb.uri", () -> mongoDb.getConnectionString() + "/lmnp-integration");
}
}
@Test
void savesAndLoadsAnApartmentWithMongoDB() {
Apartment saved = apartmentRepository.save(new Apartment(null, "default-landlord", "Studio République",
"12 rue de la République, Lyon", 150000, "Deuxième étage"));
Apartment apartment = apartmentRepository.findById(saved.getId()).orElseThrow();
assertThat(apartment.getBailleurId()).isEqualTo("default-landlord");
assertThat(apartment.getAddress()).isEqualTo("12 rue de la République, Lyon");
}
private static MongoDBContainer startMongoDb() {
if (System.getenv("SPRING_MONGODB_URI") != null) {
return null;
}
MongoDBContainer container = new MongoDBContainer("mongo:8.0");
container.start();
return container;
}
@SpringBootConfiguration
@EnableAutoConfiguration
@AutoConfigurationPackage(basePackageClasses = Apartment.class)
@EnableMongoRepositories(basePackageClasses = ApartmentRepository.class)
static class MongoTestApplication {
}
}