L. CWI
L. CWI

Reputation: 962

How to use TestContainers + Spring Boot + oracle-xe

I try to use Test Containers with Oracle-XE module and Spring Boot and so far, when I launch my test, I am confronted to exception :

Caused by: java.lang.IllegalArgumentException: JDBC URL matches jdbc:tc: prefix but the database or tag name could not be identified

In my src/test/application.properties, I declared the url datatasource as :

spring.datasource.url=jdbc:tc:oracle-xe://somehostname:someport/databasename?TC_INITSCRIPT=schema-test.sql

To indicate the docker image to pull for oracle-xe, I created the file testcontainers.properties in src/test/resources :

oracle.container.image=oracleinanutshell/oracle-xe-11g:1.0.0

Do you have any idea how to make this work ?

It works flawlessly with MySQL, with the datasource url :

spring.datasource.url=jdbc:tc:mysql:5.6.23://somehostname:someport/databasename?TC_INITSCRIPT=schema-test.sql

Upvotes: 5

Views: 11163

Answers (3)

lumizic
lumizic

Reputation: 1

In my case I was getting errors because I had not set correctly driver-class-name The solution from @magiccrafter worked for me! that is, make sure to add:

driver-class-name: org.testcontainers.jdbc.ContainerDatabaseDriver

(I wish I could upvote or comment instead but I don't have enough stackoverflow points yet)

Upvotes: 0

magiccrafter
magiccrafter

Reputation: 5474

It is possible to launch the Oracle XE container via the JDBC URL. All you have to do is to set the following config:

spring:
  datasource:
    driver-class-name: org.testcontainers.jdbc.ContainerDatabaseDriver
    url: jdbc:tc:oracle:21-slim-faststart:///test?TC_INITSCRIPT=db/init.sql

In the example above the init.sql script must be on the classpath.

More documentation on this approach can be found here: https://java.testcontainers.org/modules/databases/jdbc/

Upvotes: 1

MC Ninjava
MC Ninjava

Reputation: 244

You can make a test configuration class that redefine datasource bean with oracle xe container configuration.

public class OracleIT  {

    @ClassRule
    public static OracleContainer oracleContainer = new OracleContainer();

    @BeforeAll
    public static void startup() {
        oracleContainer.start();
    }

    @TestConfiguration
        static class OracleTestConfiguration {

            @Bean
            DataSource dataSource() {
                HikariConfig hikariConfig = new HikariConfig();
                hikariConfig.setJdbcUrl(oracleContainer.getJdbcUrl());
                hikariConfig.setUsername(oracleContainer.getUsername());
                hikariConfig.setPassword(oracleContainer.getPassword());

                return new HikariDataSource(hikariConfig);
            }
      }

}

Upvotes: 7

Related Questions