测试容器 oracle DB

Testcontainers oracle DB

我正在尝试使用 Testcontainers 和 Oracle-xe 数据库设置集成测试。 我收到以下错误:

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

我的测试扩展:

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

测试:

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

来自关于 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.

换句话说,应该使用带有 tc: 前缀的 ContainerDatabaseDriver 和 JDBC URL 或带有 getJdbcUrl() 和原始驱动程序(或让系统为您检测驱动程序)。

因此,如果您将其设为普通的 Oracle 驱动程序: spring.datasource.driver-class-name=org.testcontainers.jdbc.ContainerDatabaseDriver,应该可以。