I have created a spring boot app with the following setup:
pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.4</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com</groupId>
<artifactId>ticket</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>ticket</name>
<description>ticketing microservice</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-envers</artifactId>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>7.0.1.Final</version>
</dependency>
<dependency>
<groupId>com.github.cliftonlabs</groupId>
<artifactId>json-simple</artifactId>
<version>3.1.1</version>
</dependency>
<!-- test -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bootstrap</artifactId>
<version>3.0.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>testcontainers</artifactId>
<version>1.14.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.playtika.testcontainers</groupId>
<artifactId>embedded-postgresql</artifactId>
<version>1.43</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
bootstrap-test.properties:
embedded.postgresql.enabled=true
embedded.postgresql.dockerImage=postgres
embedded.postgresql.user=docker
embedded.postgresql.password=docker
embedded.postgresql.schema=test_db
embedded.containers.forceShutdown=true
application-test.properties
spring.datasource.url=jdbc:postgresql://${embedded.postgresql.host}:${embedded.postgresql.port}/${embedded.postgresql.schema}
spring.datasource.username=${embedded.postgresql.user}
spring.datasource.password=${embedded.postgresql.password}
spring.jpa.hibernate.ddl-auto=create
My simple test class:
@AutoConfigureMockMvc
@ActiveProfiles("test")
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
public class TicketControllerTest {
@LocalServerPort
private int port;
@Autowired
private TestRestTemplate restTemplate;
@Test
public void getInfoTest() throws Exception {
assertThat(this.restTemplate.getForObject("http://localhost:" + port + "/tickets/info",
String.class)).contains("ghix-ticket");
}
@Test
public void postTicket() throws Exception {
UserTicket rt = new UserTicket();
rt.setDetails("Details test");
rt.setSubject("subject test");
rt.setCreatedBy("1");
rt.setCategory("Verify Death");
rt.setType("Document Verification");
rt.setRole("2025");
TkmTickets responseT = this.restTemplate.postForObject("http://localhost:" + port + "/tickets/",
rt, TkmTickets.class);
System.out.println(responseT.getId());
assertThat(responseT.getDescription()).isEqualTo("Details test");
}
}
Things look good on startup:
10:23:46.877 [main] DEBUG org.springframework.core.env.StandardEnvironment - Activating profiles [test]
10:23:46.881 [main] DEBUG org.springframework.test.context.support.TestPropertySourceUtils - Adding inlined properties to environment: {spring.jmx.enabled=false, org.springframework.boot.test.context.SpringBootTestContextBootstrapper=true, server.port=0}
2021-04-07 10:23:47.419 INFO 27556 --- [ main] EmbeddedPostgreSQLBootstrapConfiguration : Starting postgresql server. Docker image: postgres
2021-04-07 10:23:47.469 INFO 27556 --- [ main] o.t.d.DockerClientProviderStrategy : Loaded org.testcontainers.dockerclient.UnixSocketClientProviderStrategy from ~/.testcontainers.properties, will try it first
2021-04-07 10:23:47.833 INFO 27556 --- [ main] o.t.d.UnixSocketClientProviderStrategy : Accessing docker with local Unix socket
2021-04-07 10:23:47.833 INFO 27556 --- [ main] o.t.d.DockerClientProviderStrategy : Found Docker environment with local Unix socket (unix:///var/run/docker.sock)
2021-04-07 10:23:47.949 INFO 27556 --- [ main] org.testcontainers.DockerClientFactory : Docker host IP address is localhost
2021-04-07 10:23:47.991 INFO 27556 --- [ main] org.testcontainers.DockerClientFactory : Connected to docker:
Server Version: 20.10.3
API Version: 1.41
Operating System: Docker Desktop
Total Memory: 1989 MB
2021-04-07 10:23:48.250 INFO 27556 --- [ main] o.t.utility.RegistryAuthLocator : Credential helper/store (docker-credential-desktop) does not have credentials for quay.io
2021-04-07 10:23:48.758 INFO 27556 --- [ main] org.testcontainers.DockerClientFactory : Ryuk started - will monitor and terminate Testcontainers containers on JVM exit
2021-04-07 10:23:48.758 INFO 27556 --- [ main] org.testcontainers.DockerClientFactory : Checking the system...
2021-04-07 10:23:48.759 INFO 27556 --- [ main] org.testcontainers.DockerClientFactory : ✔︎ Docker server version should be at least 1.6.0
2021-04-07 10:23:48.865 INFO 27556 --- [ main] org.testcontainers.DockerClientFactory : ✔︎ Docker environment should have more than 2GB free disk space
2021-04-07 10:23:48.873 INFO 27556 --- [ main] [postgres:latest] : Creating container for image: postgres:latest
2021-04-07 10:23:49.006 INFO 27556 --- [ main] [postgres:latest] : Starting container with ID: 131caba247444daf5c6cc0cff99e8c3ebfc658d53c36d771aeaa2ba6e9bad4c6
2021-04-07 10:23:49.312 INFO 27556 --- [ main] [postgres:latest] : Container postgres:latest is starting: 131caba247444daf5c6cc0cff99e8c3ebfc658d53c36d771aeaa2ba6e9bad4c6
2021-04-07 10:23:50.425 INFO 27556 --- [ main] [postgres:latest] : Container postgres:latest started in PT2.966109S
2021-04-07 10:23:50.426 INFO 27556 --- [ main] EmbeddedPostgreSQLBootstrapConfiguration : Started postgresql server. Connection details: {embedded.postgresql.port=55070, embedded.postgresql.host=localhost, embedded.postgresql.schema=test_db, embedded.postgresql.user=docker, embedded.postgresql.password=docker}, JDBC connection url: jdbc:postgresql://localhost:55070/test_db
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.4.4)
2021-04-07 10:23:50.550 INFO 27556 --- [ main] c.g.ticket.TicketControllerTest : The following profiles are active: test
2021-04-07 10:23:51.041 INFO 27556 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data JPA repositories in DEFAULT mode.
2021-04-07 10:23:51.087 INFO 27556 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 39 ms. Found 2 JPA repository interfaces.
2021-04-07 10:23:51.173 INFO 27556 --- [ main] o.s.cloud.context.scope.GenericScope : BeanFactory id=648ef21c-f385-3b5a-a247-511006c4b8be
2021-04-07 10:23:51.199 INFO 27556 --- [ main] o.s.c.a.ConfigurationClassEnhancer : @Bean method EmbeddedPostgreSQLDependenciesAutoConfiguration.datasourceDependencyPostProcessor is non-static and returns an object assignable to Spring's BeanFactoryPostProcessor interface. This will result in a failure to process annotations such as @Autowired, @Resource and @PostConstruct within the method's declaring @Configuration class. Add the 'static' modifier to this method to avoid these container lifecycle issues; see @Bean javadoc for complete details.
2021-04-07 10:23:51.564 INFO 27556 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 0 (http)
2021-04-07 10:23:51.574 INFO 27556 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2021-04-07 10:23:51.575 INFO 27556 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.44]
2021-04-07 10:23:52.114 INFO 27556 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2021-04-07 10:23:52.114 INFO 27556 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1560 ms
2021-04-07 10:23:52.320 INFO 27556 --- [ main] o.hibernate.jpa.internal.util.LogHelper : HHH000204: Processing PersistenceUnitInfo [name: default]
2021-04-07 10:23:52.384 INFO 27556 --- [ main] org.hibernate.Version : HHH000412: Hibernate ORM core version 5.4.29.Final
2021-04-07 10:23:52.524 INFO 27556 --- [ main] o.hibernate.annotations.common.Version : HCANN000001: Hibernate Commons Annotations {5.1.2.Final}
2021-04-07 10:23:52.619 INFO 27556 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Starting...
2021-04-07 10:23:52.738 INFO 27556 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Start completed.
However shorty after that I see it start to tell me it cannot find the tables in the DB:
2021-04-07 10:23:52.778 INFO 27556 --- [ main] org.hibernate.dialect.Dialect : HHH000400: Using dialect: org.hibernate.dialect.PostgreSQL10Dialect
2021-04-07 10:23:52.868 INFO 27556 --- [ main] o.h.e.boot.internal.EnversServiceImpl : Envers integration enabled? : true
2021-04-07 10:23:54.110 WARN 27556 --- [ main] o.h.engine.jdbc.spi.SqlExceptionHelper : SQL Warning Code: 0, SQLState: 00000
2021-04-07 10:23:54.111 WARN 27556 --- [ main] o.h.engine.jdbc.spi.SqlExceptionHelper : relation "tkm_ticket_tasks" does not exist, skipping
2021-04-07 10:23:54.112 WARN 27556 --- [ main] o.h.engine.jdbc.spi.SqlExceptionHelper : SQL Warning Code: 0, SQLState: 00000
2021-04-07 10:23:54.113 WARN 27556 --- [ main] o.h.engine.jdbc.spi.SqlExceptionHelper : relation "tkm_ticket_tasks" does not exist, skipping
2021-04-07 10:23:54.114 WARN 27556 --- [ main] o.h.engine.jdbc.spi.SqlExceptionHelper : SQL Warning Code: 0, SQLState: 00000
2021-04-07 10:23:54.114 WARN 27556 --- [ main] o.h.engine.jdbc.spi.SqlExceptionHelper : relation "tkm_ticket_tasks_aud" does not exist, skipping
2021-04-07 10:23:54.116 WARN 27556 --- [ main] o.h.engine.jdbc.spi.SqlExceptionHelper : SQL Warning Code: 0, SQLState: 00000
2021-04-07 10:23:54.116 WARN 27556 --- [ main] o.h.engine.jdbc.spi.SqlExceptionHelper : relation "tkm_tickets" does not exist, skipping
2021-04-07 10:23:54.117 WARN 27556 --- [ main] o.h.engine.jdbc.spi.SqlExceptionHelper : SQL Warning Code: 0, SQLState: 00000
2021-04-07 10:23:54.117 WARN 27556 --- [ main] o.h.engine.jdbc.spi.SqlExceptionHelper : relation "tkm_tickets" does not exist, skipping
2021-04-07 10:23:54.118 WARN 27556 --- [ main] o.h.engine.jdbc.spi.SqlExceptionHelper : SQL Warning Code: 0, SQLState: 00000
2021-04-07 10:23:54.118 WARN 27556 --- [ main] o.h.engine.jdbc.spi.SqlExceptionHelper : relation "tkm_tickets_aud" does not exist, skipping
2021-04-07 10:23:54.120 WARN 27556 --- [ main] o.h.engine.jdbc.spi.SqlExceptionHelper : SQL Warning Code: 0, SQLState: 00000
2021-04-07 10:23:54.120 WARN 27556 --- [ main] o.h.engine.jdbc.spi.SqlExceptionHelper : relation "tkm_workflows" does not exist, skipping
I have a look at the db when debugging and sure enough no tables exist on that postgres container, am I missing something in terms of setup, my understanding was that:
spring.jpa.hibernate.ddl-auto=create
Along with the data in data.sql will be all the setup required to create and populate my tables within the DB container.
Here is my dockerFile:
FROM openjdk:8
ADD target/ghix-ticket-0.0.1-SNAPSHOT.jar ghix-ticket-0.0.1-SNAPSHOT.jar
EXPOSE 8080
ENTRYPOINT ["java", "-jar", "ghix-ticket-0.0.1-SNAPSHOT.jar"]
and my dockercompose:
version: "3"
services:
db:
image: postgres
#network_mode: bridge
container_name: db
volumes:
- postgres-data:/var/lib/postgresql/data
expose:
- 5432
ports:
- 5432:5432
environment:
- POSTGRES_PASSWORD=docker
- POSTGRES_USER=docker
- POSTGRES_DB=docker
restart: unless-stopped
# This is my rest api app*****************************************
ghix-ticket:
image: ghix-ticket
#network_mode: bridge
container_name: ghix-ticket
expose:
- 8080
ports:
- 8081:8080
environment:
SPRING_DATASOURCE_URL: jdbc:postgresql://db/docker
SPRING_DATASOURCE_USERNAME: docker
SPRING_DATASOURCE_PASSWORD: docker
depends_on:
- db
volumes:
- /Users/mcerlane_w/dev/:/var/tmp
restart: unless-stopped
volumes:
postgres-data:
However this is coming from when I am running my tests from IDE or when I run via:
mvn test -Dspring-boot.run.profiles=test
Log from Postgres container:
The files belonging to this database system will be owned by user
"postgres".
This user must also own the server process.
The database cluster will be initialized with locale "en_US.utf8".
The default database encoding has accordingly been set to "UTF8".
The default text search configuration will be set to "english".
Data page checksums are disabled.
fixing permissions on existing directory /var/lib/postgresql/data ... ok
creating subdirectories ... ok
selecting dynamic shared memory implementation ... posix
selecting default max_connections ... 100
selecting default shared_buffers ... 128MB
selecting default time zone ... Etc/UTC
creating configuration files ... ok
running bootstrap script ... ok
performing post-bootstrap initialization ... ok
syncing data to disk ... ok
initdb: warning: enabling "trust" authentication for local connections
You can change this by editing pg_hba.conf or using the option -A, or
--auth-local and --auth-host, the next time you run initdb.
Success. You can now start the database server using:
pg_ctl -D /var/lib/postgresql/data -l logfile start
waiting for server to start....2021-04-07 17:55:15.130 UTC [48] LOG: starting PostgreSQL 13.2 (Debian 13.2-1.pgdg100+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 8.3.0-6) 8.3.0, 64-bit
2021-04-07 17:55:15.131 UTC [48] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
2021-04-07 17:55:15.132 UTC [49] LOG: database system was shut down at 2021-04-07 17:55:14 UTC
2021-04-07 17:55:15.134 UTC [48] LOG: database system is ready to accept connections
done
server started
CREATE DATABASE
/usr/local/bin/docker-entrypoint.sh: ignoring /docker-entrypoint-initdb.d/*
2021-04-07 17:55:15.339 UTC [48] LOG: received fast shutdown request
2021-04-07 17:55:15.339 UTC [48] LOG: aborting any active transactions
waiting for server to shut down....2021-04-07 17:55:15.342 UTC [48] LOG: background worker "logical replication launcher" (PID 55) exited with exit code 1
2021-04-07 17:55:15.342 UTC [50] LOG: shutting down
2021-04-07 17:55:15.347 UTC [48] LOG: database system is shut down
done
server stopped
PostgreSQL init process complete; ready for start up.
2021-04-07 17:55:15.459 UTC [1] LOG: starting PostgreSQL 13.2 (Debian 13.2-1.pgdg100+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 8.3.0-6) 8.3.0, 64-bit
2021-04-07 17:55:15.459 UTC [1] LOG: listening on IPv4 address "0.0.0.0", port 5432
2021-04-07 17:55:15.460 UTC [1] LOG: listening on IPv6 address "::", port 5432
2021-04-07 17:55:15.460 UTC [1] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
2021-04-07 17:55:15.462 UTC [76] LOG: database system was shut down at 2021-04-07 17:55:15 UTC
2021-04-07 17:55:15.466 UTC [1] LOG: database system is ready to accept connections