From 88a19c651eed770b69eb751570be46bd6ce60cf7 Mon Sep 17 00:00:00 2001 From: emanueltrandafir1993 Date: Mon, 2 Oct 2023 01:54:54 +0200 Subject: [PATCH] BAEL-6952: reuse testcontainers --- .../reuse/ReusableContainersLiveTest.java | 52 +++++++++++++++++++ .../DynamicPropertiesLiveTest.java | 2 +- .../{ => support}/LocalDevApplication.java | 1 + .../ServiceConnectionLiveTest.java | 2 +- 4 files changed, 55 insertions(+), 2 deletions(-) create mode 100644 spring-boot-modules/spring-boot-3-testcontainers/src/test/java/com/baeldung/testcontainers/reuse/ReusableContainersLiveTest.java rename spring-boot-modules/spring-boot-3-testcontainers/src/test/java/com/baeldung/testcontainers/{ => support}/DynamicPropertiesLiveTest.java (98%) rename spring-boot-modules/spring-boot-3-testcontainers/src/test/java/com/baeldung/testcontainers/{ => support}/LocalDevApplication.java (95%) rename spring-boot-modules/spring-boot-3-testcontainers/src/test/java/com/baeldung/testcontainers/{ => support}/ServiceConnectionLiveTest.java (97%) diff --git a/spring-boot-modules/spring-boot-3-testcontainers/src/test/java/com/baeldung/testcontainers/reuse/ReusableContainersLiveTest.java b/spring-boot-modules/spring-boot-3-testcontainers/src/test/java/com/baeldung/testcontainers/reuse/ReusableContainersLiveTest.java new file mode 100644 index 0000000000..4a96160e32 --- /dev/null +++ b/spring-boot-modules/spring-boot-3-testcontainers/src/test/java/com/baeldung/testcontainers/reuse/ReusableContainersLiveTest.java @@ -0,0 +1,52 @@ +package com.baeldung.testcontainers.reuse; + +import static org.assertj.core.api.Assertions.assertThat; + +import java.util.List; + +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.DynamicPropertyRegistry; +import org.springframework.test.context.DynamicPropertySource; +import org.testcontainers.containers.MongoDBContainer; +import org.testcontainers.utility.DockerImageName; + +import com.baeldung.testcontainers.support.MiddleEarthCharacter; +import com.baeldung.testcontainers.support.MiddleEarthCharactersRepository; + +@SpringBootTest +class ReusableContainersLiveTest { + + static MongoDBContainer mongoDBContainer = new MongoDBContainer(DockerImageName.parse("mongo:4.0.10")) + .withReuse(true); + + @BeforeAll + static void beforeAll() { + mongoDBContainer.start(); + } + + @DynamicPropertySource + static void setProperties(DynamicPropertyRegistry registry) { + registry.add("spring.data.mongodb.uri", mongoDBContainer::getReplicaSetUrl); + } + + @Autowired + private MiddleEarthCharactersRepository repository; + + @Test + void whenRunningMultipleTimes_thenContainerShouldBeReused_andTestShouldFail() { + assertThat(repository.findAll()) + .isEmpty(); + + repository.saveAll(List.of( + new MiddleEarthCharacter("Frodo", "hobbit"), + new MiddleEarthCharacter("Samwise", "hobbit")) + ); + + assertThat(repository.findAll()) + .hasSize(2); + } + +} diff --git a/spring-boot-modules/spring-boot-3-testcontainers/src/test/java/com/baeldung/testcontainers/DynamicPropertiesLiveTest.java b/spring-boot-modules/spring-boot-3-testcontainers/src/test/java/com/baeldung/testcontainers/support/DynamicPropertiesLiveTest.java similarity index 98% rename from spring-boot-modules/spring-boot-3-testcontainers/src/test/java/com/baeldung/testcontainers/DynamicPropertiesLiveTest.java rename to spring-boot-modules/spring-boot-3-testcontainers/src/test/java/com/baeldung/testcontainers/support/DynamicPropertiesLiveTest.java index 2633f227d4..d2511286e3 100644 --- a/spring-boot-modules/spring-boot-3-testcontainers/src/test/java/com/baeldung/testcontainers/DynamicPropertiesLiveTest.java +++ b/spring-boot-modules/spring-boot-3-testcontainers/src/test/java/com/baeldung/testcontainers/support/DynamicPropertiesLiveTest.java @@ -1,4 +1,4 @@ -package com.baeldung.testcontainers; +package com.baeldung.testcontainers.support; import static io.restassured.RestAssured.when; import static org.hamcrest.Matchers.hasItems; diff --git a/spring-boot-modules/spring-boot-3-testcontainers/src/test/java/com/baeldung/testcontainers/LocalDevApplication.java b/spring-boot-modules/spring-boot-3-testcontainers/src/test/java/com/baeldung/testcontainers/support/LocalDevApplication.java similarity index 95% rename from spring-boot-modules/spring-boot-3-testcontainers/src/test/java/com/baeldung/testcontainers/LocalDevApplication.java rename to spring-boot-modules/spring-boot-3-testcontainers/src/test/java/com/baeldung/testcontainers/support/LocalDevApplication.java index 1b6fe32c97..0b49fba26b 100644 --- a/spring-boot-modules/spring-boot-3-testcontainers/src/test/java/com/baeldung/testcontainers/LocalDevApplication.java +++ b/spring-boot-modules/spring-boot-3-testcontainers/src/test/java/com/baeldung/testcontainers/support/LocalDevApplication.java @@ -7,6 +7,7 @@ import org.springframework.boot.testcontainers.service.connection.ServiceConnect import org.springframework.context.annotation.Bean; import org.testcontainers.containers.MongoDBContainer; import org.testcontainers.utility.DockerImageName; +import com.baeldung.testcontainers.Application; // Testcontainers require a valid docker installation. diff --git a/spring-boot-modules/spring-boot-3-testcontainers/src/test/java/com/baeldung/testcontainers/ServiceConnectionLiveTest.java b/spring-boot-modules/spring-boot-3-testcontainers/src/test/java/com/baeldung/testcontainers/support/ServiceConnectionLiveTest.java similarity index 97% rename from spring-boot-modules/spring-boot-3-testcontainers/src/test/java/com/baeldung/testcontainers/ServiceConnectionLiveTest.java rename to spring-boot-modules/spring-boot-3-testcontainers/src/test/java/com/baeldung/testcontainers/support/ServiceConnectionLiveTest.java index 51b69c44b3..a93c136e1c 100644 --- a/spring-boot-modules/spring-boot-3-testcontainers/src/test/java/com/baeldung/testcontainers/ServiceConnectionLiveTest.java +++ b/spring-boot-modules/spring-boot-3-testcontainers/src/test/java/com/baeldung/testcontainers/support/ServiceConnectionLiveTest.java @@ -1,4 +1,4 @@ -package com.baeldung.testcontainers; +package com.baeldung.testcontainers.support; import static io.restassured.RestAssured.when; import static org.hamcrest.Matchers.hasItems;