From eb56d0f32d552396422914cb780fc40e813e917a Mon Sep 17 00:00:00 2001 From: Alex Castells Date: Thu, 26 Nov 2020 23:36:43 +0100 Subject: [PATCH 1/2] lifecycle gestionado con @Testcontainers pero... :( --- build.gradle | 1 + .../AbstractIntegrationTest.java | 38 +++++++++++++++++++ .../infrastructure/ApplicationTest.java | 4 +- 3 files changed, 40 insertions(+), 3 deletions(-) create mode 100644 src/test/java/com/github/alextremp/testcontainersdemo/infrastructure/AbstractIntegrationTest.java diff --git a/build.gradle b/build.gradle index 8f8be4c..519c347 100644 --- a/build.gradle +++ b/build.gradle @@ -37,6 +37,7 @@ dependencies { // test testImplementation 'org.springframework.boot:spring-boot-starter-test' testImplementation 'org.testcontainers:testcontainers:1.15.0' + testImplementation 'org.testcontainers:junit-jupiter:1.15.0' testImplementation 'org.awaitility:awaitility:3.1.5' } diff --git a/src/test/java/com/github/alextremp/testcontainersdemo/infrastructure/AbstractIntegrationTest.java b/src/test/java/com/github/alextremp/testcontainersdemo/infrastructure/AbstractIntegrationTest.java new file mode 100644 index 0000000..f1144c1 --- /dev/null +++ b/src/test/java/com/github/alextremp/testcontainersdemo/infrastructure/AbstractIntegrationTest.java @@ -0,0 +1,38 @@ +package com.github.alextremp.testcontainersdemo.infrastructure; + +import java.io.File; +import org.junit.jupiter.api.BeforeAll; +import org.springframework.boot.test.context.SpringBootTest; +import org.testcontainers.containers.DockerComposeContainer; +import org.testcontainers.junit.jupiter.Container; +import org.testcontainers.junit.jupiter.Testcontainers; + +import static java.lang.String.valueOf; +import static java.lang.System.setProperty; + +@SpringBootTest +@Testcontainers +abstract class AbstractIntegrationTest { + + private static final String MESSAGE_STORE_DB_SERVICE = "message-store-db"; + private static final Integer MESSAGE_STORE_DB_PORT = 5432; + private static final String SPRING_MESSAGE_STORE_DB_PORT = "message-store-db.port"; + + private static final String MQ_SERVER_SERVICE = "mq-server"; + private static final Integer MQ_SERVER_PORT = 5672; + private static final String SPRING_MQ_SERVER_PORT = "mq-server.port"; + + + @Container + private static DockerComposeContainer dockerServices = + new DockerComposeContainer<>(new File("docker-compose.yml")) + .withLocalCompose(true) + .withExposedService(MESSAGE_STORE_DB_SERVICE, MESSAGE_STORE_DB_PORT) + .withExposedService(MQ_SERVER_SERVICE, MQ_SERVER_PORT); + + @BeforeAll + public static void beforeAll() { + setProperty(SPRING_MESSAGE_STORE_DB_PORT, valueOf(dockerServices.getServicePort(MESSAGE_STORE_DB_SERVICE, MESSAGE_STORE_DB_PORT))); + setProperty(SPRING_MQ_SERVER_PORT, valueOf(dockerServices.getServicePort(MQ_SERVER_SERVICE, MQ_SERVER_PORT))); + } +} diff --git a/src/test/java/com/github/alextremp/testcontainersdemo/infrastructure/ApplicationTest.java b/src/test/java/com/github/alextremp/testcontainersdemo/infrastructure/ApplicationTest.java index c27dbfe..6cb1460 100644 --- a/src/test/java/com/github/alextremp/testcontainersdemo/infrastructure/ApplicationTest.java +++ b/src/test/java/com/github/alextremp/testcontainersdemo/infrastructure/ApplicationTest.java @@ -6,15 +6,13 @@ import org.springframework.amqp.rabbit.core.RabbitTemplate; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; -import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.jdbc.Sql; import static java.lang.String.format; import static org.awaitility.Awaitility.await; -@SpringBootTest @Sql("/fixtures/message-store-db/clean.sql") -class ApplicationTest { +class ApplicationTest extends AbstractIntegrationTest { @Autowired private RabbitTemplate rabbitTemplate; From 0d2eb39f0321ab765419af9aeaea5150b55c1606 Mon Sep 17 00:00:00 2001 From: Alex Castells Date: Tue, 22 Dec 2020 09:22:09 +0100 Subject: [PATCH 2/2] Update README.md --- README.md | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index e26d20b..387449d 100644 --- a/README.md +++ b/README.md @@ -1,9 +1,15 @@ # TestContainers en el contexto de SpringBootTest -> Link al post: +Este proyecto es la base del post [Integrando TestContainers en el contexto de Spring en nuestros tests](https://dev.to/adevintaspain/integrando-testcontainers-en-el-contexto-de-spring-en-nuestros-tests-5b7d) para jugar con distintas formas de integrar TestContainers en el flujo de ejecución de tests con [Junit5](https://junit.org/junit5/docs/current/user-guide/) para un servicio desarrollado con [Spring Boot](https://spring.io/projects/spring-boot), dejando al final que sea Spring quien haga el trabajo por nosotros, aprovechándonos de las características del ciclo de vida del ApplicationContext durante la ejecución de los tests, focalizado en la asignación de puertos dinámicos para las infraestructuras externas levantadas localmente para los tests. +Como referencia a las pruebas comentadas en el post, se puede consultar los cambios que aplicarían en cada caso en estas PR: -1. Antes de integrar test containers, ya se puede probar el proyecto usando docker-compose +> - :confused: _[TestContainers integrados con JUnit](https://github.com/alextremp/testcontainers-springboot-demo/pull/3/files)_ +> - :rocket: _[TestContainers singleton gestionados manualmente](https://github.com/alextremp/testcontainers-springboot-demo/pull/2/files)_ +> - :heart_eyes: _[TestContainers gestionados por Spring](https://github.com/alextremp/testcontainers-springboot-demo/pull/1/files)_ + + +La rama master es operativa pero sin TestContainers, por lo que para poder ejecutar los tests debe levantarse las infraestructuras externas con docker compose: ``` docker-compose up -d