我正在尝试使用 Testcontainers 和 Oracle-xe 数据库设置集成测试。我收到以下错误:
application.properties 条目: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);
}
}