【问题标题】:How to use TestContainers + Spring Boot + oracle-xe如何使用 TestContainers + Spring Boot + oracle-xe
【发布时间】:2019-12-10 00:23:57
【问题描述】:

我尝试使用带有 Oracle-XE 模块和 Spring Boot 的测试容器,到目前为止,当我启动测试时,我遇到了异常:

Caused by: java.lang.IllegalArgumentException: JDBC URL matches jdbc:tc: prefix but the database or tag name could not be identified

在我的src/test/application.properties 中,我将 url 数据源声明为:

spring.datasource.url=jdbc:tc:oracle-xe://somehostname:someport/databasename?TC_INITSCRIPT=schema-test.sql

为了指示要为 oracle-xe 拉取的 docker 映像,我在 src/test/resources 中创建了文件 testcontainers.properties

oracle.container.image=oracleinanutshell/oracle-xe-11g:1.0.0

您知道如何完成这项工作吗?

它与 MySQL 完美配合,使用数据源 url:

spring.datasource.url=jdbc:tc:mysql:5.6.23://somehostname:someport/databasename?TC_INITSCRIPT=schema-test.sql

【问题讨论】:

    标签: spring spring-boot oracle-xe testcontainers


    【解决方案1】:

    您可以创建一个测试配置类,使用 oracle xe 容器配置重新定义数据源 bean。

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

    【讨论】:

    • 谢谢!这样可行。但是是否可以仅通过 application.properties 中的配置(如 MySQL)使其工作?
    • 相信在容器启动之前你不可能知道所有的oracle配置
    猜你喜欢
    • 2019-10-21
    • 2019-04-22
    • 2021-03-03
    • 2022-12-20
    • 2020-01-22
    • 2021-03-11
    • 1970-01-01
    • 2017-12-30
    • 2022-06-14
    相关资源
    最近更新 更多