Add Testcontainers

This commit is contained in:
2025-01-29 21:47:00 +01:00
parent ac1959a0d6
commit cf9c642665
3 changed files with 48 additions and 0 deletions

15
pom.xml
View File

@ -85,6 +85,21 @@
<artifactId>spring-boot-starter-test</artifactId> <artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>testcontainers</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>postgresql</artifactId>
<scope>test</scope>
</dependency>
</dependencies> </dependencies>
<build> <build>

View File

@ -1,8 +1,11 @@
package ch.dlmw.swisssignchallenge; package ch.dlmw.swisssignchallenge;
import ch.dlmw.swisssignchallenge.config.TestConfig;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.annotation.Import;
@Import(TestConfig.class)
@SpringBootTest @SpringBootTest
class SwisssignChallengeApplicationTests { class SwisssignChallengeApplicationTests {

View File

@ -0,0 +1,30 @@
package ch.dlmw.swisssignchallenge.config;
import javax.sql.DataSource;
import org.springframework.boot.test.context.TestConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.testcontainers.containers.PostgreSQLContainer;
@TestConfiguration
public class TestConfig {
private static final PostgreSQLContainer<?> POSTGRES_CONTAINER = new PostgreSQLContainer<>("postgres:latest")
.withDatabaseName("testdb")
.withUsername("testuser")
.withPassword("testpass");
static {
POSTGRES_CONTAINER.start();
}
@Bean
public DataSource dataSource() {
var dataSource = new DriverManagerDataSource();
dataSource.setUrl(POSTGRES_CONTAINER.getJdbcUrl());
dataSource.setUsername(POSTGRES_CONTAINER.getUsername());
dataSource.setPassword(POSTGRES_CONTAINER.getPassword());
dataSource.setDriverClassName("org.postgresql.Driver");
return dataSource;
}
}