3

I'm trying to use TestContainers for my integration tests. I started using it instantiating objects like:

@ClassRule
public static PostgreSQLContainer postgres = (PostgreSQLContainer) new PostgreSQLContainer()
        .withDatabaseName("producto")
        .withInitScript("init.sql");

That way my entities classes work perfectly. But when I tried to use it via JDBC URL as described here, I get the following exception

rg.postgresql.util.PSQLException: ERROR: cross-database references are not implemented: "producto.producto.driver"

I'm using Spring Boot, so I'm defining the following properties in my application.properties to take advantage of Spring Boot autoconfiguration (and not defining the container in code anymore):

spring.datasource.driver-class-name=org.testcontainers.jdbc.ContainerDatabaseDrive
spring.datasource.url=jdbc:tc:postgresql://somehostname:someport/producto?TC_INITSCRIPT=init.sql

My entity class is defined as:

@Entity
@Table(name = "driver", schema = "producto", catalog = "producto")
public class DriverEntity { }

I don't really understand why it works when defining an object, but not when using the jdbc url don't.

¿Do I need to define other properties?

Rayniery
  • 1,557
  • 2
  • 12
  • 17
  • I am also running into the same issue (different error message though). Did you find a solution for this? – chom Jun 14 '19 at 22:28

3 Answers3

3

When started from the JDBC URL, database name will be "test" (Testcontainers ignores the database name from the JDBC URL).

In your code, you hardcode the database name, this is not recommended because you might be running your app in different environments with different database names.

Try removing the database name from the annotation.

bsideup
  • 2,845
  • 17
  • 21
  • The doc says that the databasename is ignored, but this is not actually correct. `spring.datasource.url=jdbc:tc:postgresql:10.16://somehost:12345/bla?TC_REUSABLE=true` will create a DB named 'bla' instead of 'test' – Klaus Groenbaek Jun 04 '21 at 12:50
1

typo.

org.testcontainers.jdbc.ContainerDatabaseDrive should be org.testcontainers.jdbc.ContainerDatabaseDriver

Jason
  • 526
  • 7
  • 9
1

TestContainer version is 1.17.2 - Giving withDatabaseName("some-name") for DB2 Container is not working and getting "ERROR org.testcontainers.jdbc.JdbcDatabaseDelegate - Could not obtain JDBC connection" but mysql works without any issue

Code Snippet for DB2

@Container
static Db2Container container = new Db2Container("ibmcom/db2:11.5.7.0")
        .acceptLicense()
        .withUsername("db2user")
        .withPassword("db2pwd")
        .withInitScript("db2-init.sql");

Code Snippet for Mysql

@Container
    static MySQLContainer container = (MySQLContainer) new MySQLContainer("mysql:8.0.29")
            .withUsername("test-user")
            .withPassword("test-pwd")
            .withDatabaseName("test-db").withInitScript("mysql-init.sql");
Anver Sadhat
  • 3,074
  • 1
  • 25
  • 26