springspring-bootoracle-xetestcontainers

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

Solution

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