Skip to content

Commit

Permalink
feat: test containers
Browse files Browse the repository at this point in the history
  • Loading branch information
farneser committed Dec 1, 2023
1 parent fb8d105 commit 6d0d846
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 10 deletions.
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,12 @@
<artifactId>gson</artifactId>
<version>2.10.1</version>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>postgresql</artifactId>
<version>1.18.3</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package dev.farneser.tasktracker.scheduler.config;

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;
import jakarta.annotation.PreDestroy;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.testcontainers.containers.PostgreSQLContainer;

import javax.sql.DataSource;

@Configuration
public class TestContainerConfig {
@Bean
public DataSource dataSource() {
var pgContainer = new PostgreSQLContainer<>("postgres:latest")
.withDatabaseName("task-tracker")
.withUsername("postgres")
.withPassword("postgres")
.withExposedPorts(5432)
.withCreateContainerCmdModifier(cmd -> cmd.withHostConfig(
new HostConfig().withPortBindings(new PortBinding(Ports.Binding.bindPort(15432), new ExposedPort(5432)))));

pgContainer.start();

var dataSource = new DriverManagerDataSource();

dataSource.setDriverClassName("org.postgresql.Driver");
dataSource.setUrl(pgContainer.getJdbcUrl());
dataSource.setUsername(pgContainer.getUsername());
dataSource.setPassword(pgContainer.getPassword());

return dataSource;
}
}

10 changes: 0 additions & 10 deletions src/test/resources/application.properties
Original file line number Diff line number Diff line change
@@ -1,17 +1,7 @@
# postgres
spring.datasource.url=jdbc:postgresql://localhost:5432/task-tracker
spring.datasource.username=postgres
spring.datasource.password=postgres
spring.datasource.driver-class-name=org.postgresql.Driver
# jpa
spring.jpa.show-sql=true
spring.jpa.database-platform=org.hibernate.dialect.PostgreSQLDialect
spring.jpa.hibernate.ddl-auto=none
# rabbitmq
spring.rabbitmq.host=localhost
spring.rabbitmq.port=5672
spring.rabbitmq.username=rabbitmq
spring.rabbitmq.password=rabbitmq
# scheduler
# every minute
scheduler.cron=0 * * * * *
Expand Down

0 comments on commit 6d0d846

Please sign in to comment.