为什么自定义JDBC驱动程序在与Spring的集成测试中不起作用?

发布于 2025-02-13 11:17:59 字数 2045 浏览 1 评论 0原文

我正在制作自定义JDBC驱动程序,我需要(使用Spring-Boot)进行测试。我在另一个项目中添加了依赖性(我使用Maven)此驱动程序罐,并且在主程序中工作正常。但是,当我尝试进行集成测试(在单元测试中,在drivermanager本身中注册的驱动程序本身)不会加载驱动程序,因此Spring-Boot无法用名称“ DataSource”创建BEAN。

我还尝试编写单元测试以直接使用驱动程序,但是驱动程序无法加载资源中包含的属性文件(文件路径为src/main/resource/resources/driver.properties)。在这种情况下,驱动程序本身在drivermanager中注册。

我该如何做到这一点:

  1. 驱动程序可以在单元测试期间从资源中看到文件? 固定
  2. 在与Spring的集成测试中,驱动程序启动寄存器本身?

注意:JDBC使用服务提供商机制加载驱动程序,因此我在Resources中也有java.sql.sql.driver文件。 .sql.Driver )。

< README还链接到项目,我正在尝试测试驱动程序

测试代码:

@SpringBootTest
public class ApplicationSmokeTest {

    @Test
    public void contextLoad(){
        
        assertTrue(true);

    }

}

在主要程序中,驱动程序已加载,并与它们一起使用。

依赖性:

<dependencies>
        <dependency>
            <groupId>org.mock.jdbc</groupId>
            <artifactId>HttpDriver</artifactId>
            <version>1.0</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>5.3.3</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
</dependencies>

I'm making custom JDBC driver and I need to test it (with Spring-boot). I added a dependency (I use Maven) this driver JAR to another project and it's work fine in the main program. But when I'm trying to make integration tests (in unit tests the driver registered in DriverManager by itself) driver doesn't load so Spring-boot can't create bean with name 'dataSource'.

I also tried to write unit tests to use the driver directly, but the driver cannot load the properties file that is contained in the resources (file path is src/main/resources/driver.properties). In this case the driver registered in DriverManager by itself.

How can I make it so that:

  1. The driver can see the file from resources during unit tests? FIXED
  2. The driver start register itself during integration tests with Spring?

Note: JDBC use the service provider mechanism to load drivers, so I also have the java.sql.Driver file in resources.(src/main/resources/META-INF/services/java.sql.Driver).

Link for the JDBC driver code. Readme have also link to project, where I'm trying to test driver

Code of test:

@SpringBootTest
public class ApplicationSmokeTest {

    @Test
    public void contextLoad(){
        
        assertTrue(true);

    }

}

In main program driver is loaded and spring work with them fine.

Dependencies:

<dependencies>
        <dependency>
            <groupId>org.mock.jdbc</groupId>
            <artifactId>HttpDriver</artifactId>
            <version>1.0</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>5.3.3</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
</dependencies>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文