-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Description
Hello,
I am trying to use testcontainers for integration tests with postgres in a multi module project.
Here is the scenario:
Module A is a library and contains the services, repositories and entities (I am using JPA). The entitymanager factory and datasource are configured in a @configuration class.
Module B is a spring boot application that contains the controller and a dependency to module A to call the services/connect to database.
From module A I am using mockmvc to test the whole functionality (from controller to service).
I tried two approaches:
- Using jdbc containers (https://www.testcontainers.org/modules/databases/jdbc/)
With this approach TWO containers are created for one test. First container is removed immediately after initializing and a new one is created when Module B is hit.
[Test worker] docker[postgres:latest] : Starting container with ID: d32556d8b2f355feea8ed61a66611db54e2199d48aa20eaca6e1f068ee5bd573
[Test worker] docker[postgres:latest] : Container postgres:latest is starting: d32556d8b2f355feea8ed61a66611db54e2199d48aa20eaca6e1f068ee5bd573
[...]
[ Test worker] o.t.containers.output.WaitingConsumer : STDERR: 2020-09-26 11:04:55.529 UTC [1] LOG: database system is ready to accept connections
[ Test worker] org.testcontainers.ext.ScriptUtils : Executing database script from file:src/test/resources/integration/init_database.sql
[...]
[ Test worker] org.testcontainers.ext.ScriptUtils : Executed database script from file:src/test/resources/integration/init_database.sql in 245 ms.
[ Test worker] o.testcontainers.utility.ResourceReaper : Removed container and associated volume(s): postgres:latest
[...]
[ Test worker] docker[postgres:latest] : Creating container for image: postgres:latest
[ Test worker] o.t.utility.RegistryAuthLocator : Looking up auth config for image: postgres:latest at registry: index.docker.io
[ Test worker] o.t.utility.RegistryAuthLocator : No matching Auth Configs - falling back to defaultAuthConfig [null]
[ Test worker] o.t.d.a.AuthDelegatingDockerClientConfig : Effective auth config [null]
[ Test worker] docker[postgres:latest] : Starting container with ID: 5e9a660c0df6bd93c139bc05ef986a1360ceefdd7ede8c1438d28512978abf47
[ Test worker] docker[postgres:latest] : Container postgres:latest is starting: 5e9a660c0df6bd93c139bc05ef986a1360ceefdd7ede8c1438d28512978abf47
- Using static containers
With this approach only one container gets created but when module B is hit it tries to connect to the real database, not to the containers. So it seems that the spring datasource properties are not overridden.
Caused by: org.postgresql.util.PSQLException: Connection to localhost:5432 refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections. at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:285) at org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:49) at org.postgresql.jdbc.PgConnection.<init>(PgConnection.java:217) at org.postgresql.Driver.makeConnection(Driver.java:458) at org.postgresql.Driver.connect(Driver.java:260) at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:677) at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:189) at org.springframework.jdbc.datasource.DriverManagerDataSource.getConnectionFromDriverManager(DriverManagerDataSource.java:155) at org.springframework.jdbc.datasource.DriverManagerDataSource.getConnectionFromDriver(DriverManagerDataSource.java:146) at org.springframework.jdbc.datasource.AbstractDriverBasedDataSource.getConnectionFromDriver(AbstractDriverBasedDataSource.java:205) at org.springframework.jdbc.datasource.AbstractDriverBasedDataSource.getConnection(AbstractDriverBasedDataSource.java:169) at org.hibernate.engine.jdbc.connections.internal.DatasourceConnectionProviderImpl.getConnection(DatasourceConnectionProviderImpl.java:122) at org.hibernate.internal.NonContextualJdbcConnectionAccess.obtainConnection(NonContextualJdbcConnectionAccess.java:38) at org.hibernate.resource.jdbc.internal.LogicalConnectionManagedImpl.acquireConnectionIfNeeded(LogicalConnectionManagedImpl.java:104)
Can you please point me to a possible solution to this?