springoracletestcontainersoracle-xetestcontainers-junit5

Testcontainers oracle DB


I'm trying to setup a integration test Using Testcontainers and Oracle-xe database. I'm getting the error below:

application.properties entry:spring.datasource.driver-class-name=org.testcontainers.jdbc.ContainerDatabaseDriver

Driver org.testcontainers.jdbc.ContainerDatabaseDriver claims to not accept jdbcUrl, jdbc:oracle:thin:@localhost:55802/xepdb1

My Test extension:

public class OracleDBContainerExtension implements AfterAllCallback, BeforeAllCallback {
      private OracleContainer container;

      @Override
      public void beforeAll(ExtensionContext context) {
        // gvenzl/oracle-xe:18.4.0-slim
        container = new OracleContainer();
        container.start();
        container.waitingFor(
            Wait.forListeningPort().withStartupTimeout(Duration.ofSeconds(180L)));
        System.setProperty("spring.datasource.url", container.getJdbcUrl());
        System.setProperty("spring.datasource.password", container.getPassword());
        System.setProperty("spring.datasource.username", container.getUsername());
      }

      @Override
      public void afterAll(ExtensionContext context) {
        container.stop();
      }
    }

test:

    @Testcontainers
    @SpringBootTest
    @ExtendWith(OracleDBContainerExtension.class)
    public class HeroRepositoryTest {
      @Autowired
      private HeroRepository repositoryUnderTest;

      @Test
      public void shouldReturnHeroesSuccessfully() {
        System.out.println("junit version: " + Version.id());
        List<Hero> heroes = repositoryUnderTest.allHeros();
        assertThat(heroes).hasSize(1);
        repositoryUnderTest.addHero(new Hero("bb", "bb"));
        Collection<Hero> heroesAfter = repositoryUnderTest.allHeros();
        assertThat(heroesAfter).hasSize(2);
      }
    }

Solution

  • From the docs about Testcontainers JDBC support --

    If you're using the JDBC URL support, there is no need to instantiate an instance of the container - Testcontainers will do it automagically.

    In other words, one should either use the ContainerDatabaseDriver and the JDBC URL with tc: prefix or a container instance with getJdbcUrl() and the original driver (or let the system detect the driver for you).

    So if you make this a normal Oracle driver: spring.datasource.driver-class-name=org.testcontainers.jdbc.ContainerDatabaseDriver, it should work.