diff --git a/compose.yaml b/compose.yaml new file mode 100644 index 0000000..95cc70b --- /dev/null +++ b/compose.yaml @@ -0,0 +1,14 @@ +version: '3' + +services: + library-db: + image: postgres:11 + container_name: postgresql + environment: + POSTGRES_DB: library + POSTGRES_USER: test + POSTGRES_PASSWORD: test + ports: + - "5432:5432" + labels: + org.springframework.boot.ignore: true \ No newline at end of file diff --git a/pom.xml b/pom.xml index 394978b..b716f7b 100644 --- a/pom.xml +++ b/pom.xml @@ -14,7 +14,7 @@ org.springframework.boot spring-boot-starter-parent - 3.0.10 + 3.1.3 @@ -76,8 +76,8 @@ org.springframework.boot - spring-boot-properties-migrator - runtime + spring-boot-docker-compose + true org.springframework.cloud @@ -136,6 +136,11 @@ spring-boot-starter-test test + + org.springframework.boot + spring-boot-testcontainers + test + org.springframework.restdocs spring-restdocs-mockmvc @@ -171,7 +176,7 @@ org.testcontainers postgresql - 1.18.3 + ${testcontainers.version} test diff --git a/src/main/resources/application.yaml b/src/main/resources/application.yaml index 67605cc..26c588c 100644 --- a/src/main/resources/application.yaml +++ b/src/main/resources/application.yaml @@ -24,6 +24,10 @@ server: enabled: true spring: + docker: + compose: + lifecycle-management: none + enabled: false sql: init: mode: never diff --git a/src/test/java/com/xpinjection/library/LocalLibraryApplication.java b/src/test/java/com/xpinjection/library/LocalLibraryApplication.java index cca32cd..d01d187 100644 --- a/src/test/java/com/xpinjection/library/LocalLibraryApplication.java +++ b/src/test/java/com/xpinjection/library/LocalLibraryApplication.java @@ -2,14 +2,22 @@ import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.boot.context.metrics.buffering.BufferingApplicationStartup; +import org.springframework.boot.test.context.TestConfiguration; +import org.springframework.boot.testcontainers.context.ImportTestcontainers; public class LocalLibraryApplication { public static void main(String[] args) { new SpringApplicationBuilder(LibraryApplication.class) - .initializers(new StandaloneApplicationContextInitializer()) + .sources(RuntimeDependenciesConfiguration.class) + //.initializers(new StandaloneApplicationContextInitializer()) .applicationStartup(new BufferingApplicationStartup(2048)) //.applicationStartup(new FlightRecorderApplicationStartup()) .profiles("dev") .run(args); } + + @TestConfiguration + @ImportTestcontainers(RuntimeDependencies.class) + public static class RuntimeDependenciesConfiguration { + } } diff --git a/src/test/java/com/xpinjection/library/RuntimeDependencies.java b/src/test/java/com/xpinjection/library/RuntimeDependencies.java new file mode 100644 index 0000000..0f45078 --- /dev/null +++ b/src/test/java/com/xpinjection/library/RuntimeDependencies.java @@ -0,0 +1,12 @@ +package com.xpinjection.library; + +import org.springframework.boot.testcontainers.service.connection.ServiceConnection; +import org.testcontainers.containers.PostgreSQLContainer; + +public interface RuntimeDependencies { + @ServiceConnection + PostgreSQLContainer POSTGRE_SQL = new PostgreSQLContainer<>("postgres:11") + .withDatabaseName("library") + .withUsername("test") + .withPassword("test"); +} diff --git a/src/test/java/com/xpinjection/library/adaptors/api/AbstractApiTest.java b/src/test/java/com/xpinjection/library/adaptors/api/AbstractApiTest.java index 7a2c7a0..0dc26da 100644 --- a/src/test/java/com/xpinjection/library/adaptors/api/AbstractApiTest.java +++ b/src/test/java/com/xpinjection/library/adaptors/api/AbstractApiTest.java @@ -3,6 +3,7 @@ import com.github.database.rider.spring.api.DBRider; import com.github.viclovsky.swagger.coverage.FileSystemOutputWriter; import com.github.viclovsky.swagger.coverage.SwaggerCoverageV3RestAssured; +import com.xpinjection.library.RuntimeDependencies; import io.restassured.RestAssured; import io.restassured.specification.RequestSpecification; import lombok.extern.slf4j.Slf4j; @@ -11,15 +12,16 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.web.server.LocalServerPort; +import org.springframework.boot.testcontainers.context.ImportTestcontainers; import org.springframework.test.context.ActiveProfiles; import java.nio.file.Path; @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) -//@ContextConfiguration(initializers = StandaloneApplicationContextInitializer.class) @DBRider @Slf4j @ActiveProfiles("test") +@ImportTestcontainers(RuntimeDependencies.class) public abstract class AbstractApiTest { private static final ApiReports REPORTS = ApiReports.builder() .coveragePath(Path.of("target", "api-coverage")) diff --git a/src/test/java/com/xpinjection/library/adaptors/persistence/AbstractDaoTest.java b/src/test/java/com/xpinjection/library/adaptors/persistence/AbstractDaoTest.java index 0e72a3c..ff94d11 100644 --- a/src/test/java/com/xpinjection/library/adaptors/persistence/AbstractDaoTest.java +++ b/src/test/java/com/xpinjection/library/adaptors/persistence/AbstractDaoTest.java @@ -3,11 +3,13 @@ import com.github.database.rider.core.api.dataset.DataSetFormat; import com.github.database.rider.core.api.exporter.ExportDataSet; import com.github.database.rider.spring.api.DBRider; +import com.xpinjection.library.RuntimeDependencies; import org.apache.commons.lang3.StringUtils; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; import org.springframework.boot.test.autoconfigure.orm.jpa.TestEntityManager; +import org.springframework.boot.testcontainers.context.ImportTestcontainers; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.test.context.ActiveProfiles; @@ -20,6 +22,7 @@ @DataJpaTest @DBRider @ActiveProfiles("test") +@ImportTestcontainers(RuntimeDependencies.class) public abstract class AbstractDaoTest { private static long ID = 1000; diff --git a/src/test/java/com/xpinjection/library/adaptors/ui/BookUITest.java b/src/test/java/com/xpinjection/library/adaptors/ui/BookUITest.java index 4cea7de..67ca198 100644 --- a/src/test/java/com/xpinjection/library/adaptors/ui/BookUITest.java +++ b/src/test/java/com/xpinjection/library/adaptors/ui/BookUITest.java @@ -5,6 +5,7 @@ import com.gargoylesoftware.htmlunit.html.HtmlPage; import com.github.database.rider.core.api.dataset.DataSet; import com.github.database.rider.core.api.dataset.SeedStrategy; +import com.xpinjection.library.RuntimeDependencies; import org.apache.commons.lang3.StringUtils; import org.apache.http.HttpStatus; import org.hamcrest.Matchers; @@ -12,6 +13,7 @@ import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.testcontainers.context.ImportTestcontainers; import org.springframework.http.MediaType; import org.springframework.test.context.ActiveProfiles; import org.springframework.test.web.servlet.htmlunit.MockMvcWebClientBuilder; @@ -30,6 +32,7 @@ */ @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT) @ActiveProfiles("test") +@ImportTestcontainers(RuntimeDependencies.class) public class BookUITest { @Autowired private WebApplicationContext context; diff --git a/src/test/resources/application-test.yaml b/src/test/resources/application-test.yaml index a567b1d..1e1d66a 100644 --- a/src/test/resources/application-test.yaml +++ b/src/test/resources/application-test.yaml @@ -6,11 +6,6 @@ spring: enabled: false freemarker: cache: false - datasource: - url: jdbc:tc:postgresql:11:///library?TC_REUSABLE=true - username: test - password: test - driver-class-name: org.testcontainers.jdbc.ContainerDatabaseDriver jpa: show-sql: true test: