BAEL-6952: reuse testcontainers
This commit is contained in:
parent
c30b23a140
commit
88a19c651e
|
@ -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);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -1,4 +1,4 @@
|
||||||
package com.baeldung.testcontainers;
|
package com.baeldung.testcontainers.support;
|
||||||
|
|
||||||
import static io.restassured.RestAssured.when;
|
import static io.restassured.RestAssured.when;
|
||||||
import static org.hamcrest.Matchers.hasItems;
|
import static org.hamcrest.Matchers.hasItems;
|
|
@ -7,6 +7,7 @@ import org.springframework.boot.testcontainers.service.connection.ServiceConnect
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
import org.testcontainers.containers.MongoDBContainer;
|
import org.testcontainers.containers.MongoDBContainer;
|
||||||
import org.testcontainers.utility.DockerImageName;
|
import org.testcontainers.utility.DockerImageName;
|
||||||
|
import com.baeldung.testcontainers.Application;
|
||||||
|
|
||||||
|
|
||||||
// Testcontainers require a valid docker installation.
|
// Testcontainers require a valid docker installation.
|
|
@ -1,4 +1,4 @@
|
||||||
package com.baeldung.testcontainers;
|
package com.baeldung.testcontainers.support;
|
||||||
|
|
||||||
import static io.restassured.RestAssured.when;
|
import static io.restassured.RestAssured.when;
|
||||||
import static org.hamcrest.Matchers.hasItems;
|
import static org.hamcrest.Matchers.hasItems;
|
Loading…
Reference in New Issue