tonitch/back/test #139

Closed
tonitch wants to merge 2 commits from tonitch/back/test into master
2 changed files with 27 additions and 12 deletions
Showing only changes of commit c91a4c916e - Show all commits

View File

@ -32,6 +32,7 @@ dependencies {
testImplementation("org.testcontainers:junit-jupiter") testImplementation("org.testcontainers:junit-jupiter")
testImplementation("org.testcontainers:postgresql") testImplementation("org.testcontainers:postgresql")
testImplementation("io.rest-assured:rest-assured") testImplementation("io.rest-assured:rest-assured")
testImplementation("org.hamcrest:hamcrest")
} }
tasks.register("run") { tasks.register("run") {

View File

@ -9,19 +9,35 @@ import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.web.server.LocalServerPort; import org.springframework.boot.test.web.server.LocalServerPort;
import org.springframework.test.context.DynamicPropertyRegistry; import org.springframework.boot.testcontainers.service.connection.ServiceConnection;
import org.springframework.test.context.DynamicPropertySource; import org.springframework.web.service.annotation.GetExchange;
import org.testcontainers.containers.PostgreSQLContainer; import org.testcontainers.containers.PostgreSQLContainer;
import org.testcontainers.junit.jupiter.Container;
import org.testcontainers.junit.jupiter.Testcontainers;
import io.restassured.RestAssured; 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) @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
class ClydeApplicationTests { class ClydeApplicationTests {
@LocalServerPort @LocalServerPort
private Integer port; 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 @Autowired
private ClydeApplication controller; private ClydeApplication controller;
@ -31,26 +47,24 @@ class ClydeApplicationTests {
postgres.start(); postgres.start();
} }
@AfterAll @AfterAll
static void afterAll() { static void afterAll() {
postgres.stop(); 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 @BeforeEach
void setUp() { void setUp() {
RestAssured.baseURI = "http://localhost:" + port; RestAssured.baseURI = "http://localhost:" + port;
} }
@Test @Test
void contextLoads() throws Exception { void contextLoads(){
assertThat(controller).isNotNull(); assertThat(controller).isNotNull();
} }
@Test
void enableMock(){
RestAssured.get("/ping").then().statusCode(200);
}
} }