BAEL-6952: reuse testcontainers

This commit is contained in:
emanueltrandafir1993 2023-10-02 01:54:54 +02:00
parent c30b23a140
commit 88a19c651e
4 changed files with 55 additions and 2 deletions

View File

@ -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);
}
}

View File

@ -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;

View File

@ -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.

View File

@ -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;