How to use TestContainers + Spring Boot + oracle-xe
Asked Answered
M

3

5

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
Messeigneurs answered 1/8, 2019 at 11:52 Comment(0)
S
7

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);
            }
      }

}
Sagesagebrush answered 1/8, 2019 at 13:42 Comment(2)
Thank you ! That works. But is it possible to make it work only by configuration in application.properties (like MySQL) ?Messeigneurs
I believe that you cannot know all oracle configuration before the container startsSagesagebrush
A
1

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/

Albertype answered 10/5 at 14:31 Comment(0)
C
0

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)

Calaboose answered 13/6 at 5:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.