From c6cc9a9e5ddeeab11bc64caad9f8df95ac85f5e2 Mon Sep 17 00:00:00 2001 From: Anthony Debucquoy Date: Sun, 17 Mar 2024 01:55:13 +0100 Subject: [PATCH 1/2] base test [dnw] --- backend/build.gradle.kts | 1 + .../herisson/Clyde/ClydeApplicationTests.java | 51 +++++++++++++++++-- 2 files changed, 48 insertions(+), 4 deletions(-) diff --git a/backend/build.gradle.kts b/backend/build.gradle.kts index 6557b82..6c641d0 100644 --- a/backend/build.gradle.kts +++ b/backend/build.gradle.kts @@ -31,6 +31,7 @@ dependencies { testImplementation("org.springframework.boot:spring-boot-testcontainers") testImplementation("org.testcontainers:junit-jupiter") testImplementation("org.testcontainers:postgresql") + testImplementation("io.rest-assured:rest-assured") } tasks.register("run") { diff --git a/backend/src/test/java/ovh/herisson/Clyde/ClydeApplicationTests.java b/backend/src/test/java/ovh/herisson/Clyde/ClydeApplicationTests.java index 4714cbc..55457c4 100644 --- a/backend/src/test/java/ovh/herisson/Clyde/ClydeApplicationTests.java +++ b/backend/src/test/java/ovh/herisson/Clyde/ClydeApplicationTests.java @@ -1,13 +1,56 @@ package ovh.herisson.Clyde; -import org.junit.jupiter.api.Test; -// import org.springframework.boot.test.context.SpringBootTest; +import static org.assertj.core.api.Assertions.assertThat; -// @SpringBootTest +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +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.test.context.DynamicPropertyRegistry; +import org.springframework.test.context.DynamicPropertySource; +import org.testcontainers.containers.PostgreSQLContainer; + +import io.restassured.RestAssured; + +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) class ClydeApplicationTests { + @LocalServerPort + private Integer port; + + static PostgreSQLContainer postgres = new PostgreSQLContainer<>("postgres:15-alpine"); + + @Autowired + private ClydeApplication controller; + + @BeforeAll + static void beforeAll(){ + postgres.start(); + } + + @AfterAll + static void afterAll() { + postgres.stop(); + } + + @DynamicPropertySource + static void configure(DynamicPropertyRegistry registry) { + registry.add("spring.datasource.url", postgres::getJdbcUrl); + registry.add("spring.datasource.username", postgres::getUsername); + registry.add("spring.datasource.password", postgres::getUsername); + } + + @BeforeEach + void setUp() { + RestAssured.baseURI = "http://localhost:" + port; + } + @Test - void contextLoads() { + void contextLoads() throws Exception { + assertThat(controller).isNotNull(); } } -- 2.46.0 From c91a4c916e0f5ef3d3dc67bd5d2159ca16d81920 Mon Sep 17 00:00:00 2001 From: Anthony Debucquoy Date: Sun, 17 Mar 2024 10:50:40 +0100 Subject: [PATCH 2/2] Base test that function There is an error just after the test succes because the app tries to close the db twice... but at least it works. i'll try to work around that later :p --- backend/build.gradle.kts | 1 + .../herisson/Clyde/ClydeApplicationTests.java | 38 +++++++++++++------ 2 files changed, 27 insertions(+), 12 deletions(-) diff --git a/backend/build.gradle.kts b/backend/build.gradle.kts index 6c641d0..79690f2 100644 --- a/backend/build.gradle.kts +++ b/backend/build.gradle.kts @@ -32,6 +32,7 @@ dependencies { testImplementation("org.testcontainers:junit-jupiter") testImplementation("org.testcontainers:postgresql") testImplementation("io.rest-assured:rest-assured") + testImplementation("org.hamcrest:hamcrest") } tasks.register("run") { diff --git a/backend/src/test/java/ovh/herisson/Clyde/ClydeApplicationTests.java b/backend/src/test/java/ovh/herisson/Clyde/ClydeApplicationTests.java index 55457c4..de0e9aa 100644 --- a/backend/src/test/java/ovh/herisson/Clyde/ClydeApplicationTests.java +++ b/backend/src/test/java/ovh/herisson/Clyde/ClydeApplicationTests.java @@ -9,19 +9,35 @@ import org.junit.jupiter.api.Test; 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.test.context.DynamicPropertyRegistry; -import org.springframework.test.context.DynamicPropertySource; +import org.springframework.boot.testcontainers.service.connection.ServiceConnection; +import org.springframework.web.service.annotation.GetExchange; import org.testcontainers.containers.PostgreSQLContainer; +import org.testcontainers.junit.jupiter.Container; +import org.testcontainers.junit.jupiter.Testcontainers; import io.restassured.RestAssured; +import com.github.dockerjava.api.model.ExposedPort; +import com.github.dockerjava.api.model.HostConfig; +import com.github.dockerjava.api.model.PortBinding; +import com.github.dockerjava.api.model.Ports; + +@Testcontainers @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) class ClydeApplicationTests { @LocalServerPort private Integer port; - static PostgreSQLContainer postgres = new PostgreSQLContainer<>("postgres:15-alpine"); + @Container + @ServiceConnection + static PostgreSQLContainer postgres = new PostgreSQLContainer<>("postgres:alpine") + .withDatabaseName("clyde") + .withUsername("devel") + .withPassword("devel") + .withCreateContainerCmdModifier(cmd -> cmd.withHostConfig( + new HostConfig().withPortBindings(new PortBinding(Ports.Binding.bindPort(5432), new ExposedPort(5432))) + )); @Autowired private ClydeApplication controller; @@ -31,26 +47,24 @@ class ClydeApplicationTests { postgres.start(); } + @AfterAll static void afterAll() { postgres.stop(); } - - @DynamicPropertySource - static void configure(DynamicPropertyRegistry registry) { - registry.add("spring.datasource.url", postgres::getJdbcUrl); - registry.add("spring.datasource.username", postgres::getUsername); - registry.add("spring.datasource.password", postgres::getUsername); - } - @BeforeEach void setUp() { RestAssured.baseURI = "http://localhost:" + port; } @Test - void contextLoads() throws Exception { + void contextLoads(){ assertThat(controller).isNotNull(); } + + @Test + void enableMock(){ + RestAssured.get("/ping").then().statusCode(200); + } } -- 2.46.0