【问题标题】:How to UnitTest JdbcDAOs in Spring-boot application如何在 Spring-boot 应用程序中对 Jdbc DAO 进行单元测试
【发布时间】:2018-11-27 18:23:25
【问题描述】:

我们的团队刚刚继承了一个 Spring-boot 应用程序 (R1.4.3),它具有以下 pom.xml(摘录):

<groupId>SpreadSheetUploadWeb</groupId>
    <artifactId>SpreadSheetUploadWeb</artifactId>
    <packaging>jar</packaging>
    <version>1.0</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.4.3.RELEASE</version>
    </parent>

    <properties>
        <java.version>1.8</java.version>
        <spring.version>4.3.9.RELEASE</spring.version>
        <spring.boot.version>1.4.3.RELEASE</spring.boot.version>
        <db2.driver.version>10.5.9</db2.driver.version>
        <spring.batch.version>3.0.8.RELEASE</spring.batch.version>
        <thymeleaf.version>2.1.5.RELEASE</thymeleaf.version>
        <tomcat.version>8.5.6</tomcat.version>
        <poi.version>3.15</poi.version>
        <jackson.version>1.9.10</jackson.version>
    </properties>

    <dependencies>
        <!-- spring dependencies -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
            <version>${spring.boot.version}</version><!--$NO-MVN-MAN-VER$ -->
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot</artifactId>
            <version>${spring.boot.version}</version><!--$NO-MVN-MAN-VER$ -->
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${spring.version}</version><!--$NO-MVN-MAN-VER$ -->
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring.version}</version><!--$NO-MVN-MAN-VER$ -->
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-aop -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
            <version>${spring.version}</version><!--$NO-MVN-MAN-VER$ -->
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-beans -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>${spring.version}</version><!--$NO-MVN-MAN-VER$ -->
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-expression -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-expression</artifactId>
            <version>${spring.version}</version><!--$NO-MVN-MAN-VER$ -->
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-autoconfigure -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-autoconfigure</artifactId>
            <version>${spring.boot.version}</version><!--$NO-MVN-MAN-VER$ -->
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-logging -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-logging</artifactId>
            <version>${spring.boot.version}</version><!--$NO-MVN-MAN-VER$ -->
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
            <version>${spring.boot.version}</version><!--$NO-MVN-MAN-VER$ -->
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>${spring.boot.version}</version><!--$NO-MVN-MAN-VER$ -->
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <optional>true</optional>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>${spring.version}</version><!--$NO-MVN-MAN-VER$ -->
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-tx -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
            <version>${spring.version}</version><!--$NO-MVN-MAN-VER$ -->
        </dependency>
        <!-- Spring Test -->
        <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>${spring.version}</version><!--$NO-MVN-MAN-VER$ -->
            <scope>test</scope>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-test-autoconfigure -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-test-autoconfigure</artifactId>
            <version>${spring.boot.version}</version><!--$NO-MVN-MAN-VER$ -->
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>${spring.version}</version><!--$NO-MVN-MAN-VER$ -->
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${spring.version}</version><!--$NO-MVN-MAN-VER$ -->
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-tomcat -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <version>${spring.boot.version}</version><!--$NO-MVN-MAN-VER$ -->
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.thymeleaf/thymeleaf-spring4 -->
        <dependency>
            <groupId>org.thymeleaf</groupId>
            <artifactId>thymeleaf-spring4</artifactId>
            <version>${thymeleaf.version}</version><!--$NO-MVN-MAN-VER$ -->
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.thymeleaf/thymeleaf -->
        <dependency>
            <groupId>org.thymeleaf</groupId>
            <artifactId>thymeleaf</artifactId>
            <version>${thymeleaf.version}</version><!--$NO-MVN-MAN-VER$ -->
        </dependency>

        <!-- Spring Batch dependencies -->
        <dependency>
            <groupId>org.springframework.batch</groupId>
            <artifactId>spring-batch-core</artifactId>
            <version>${spring.batch.version}</version><!--$NO-MVN-MAN-VER$ -->
        </dependency>
        <dependency>
            <groupId>org.springframework.batch</groupId>
            <artifactId>spring-batch-infrastructure</artifactId>
            <version>${spring.batch.version}</version><!--$NO-MVN-MAN-VER$ -->
        </dependency>
        <dependency>
            <groupId>org.springframework.batch</groupId>
            <artifactId>spring-batch-test</artifactId>
            <version>${spring.batch.version}</version><!--$NO-MVN-MAN-VER$ -->
            <scope>test</scope>
        </dependency>

我的 application.properties:

spring.http.multipart.max-file-size=200MB
spring.http.multipart.max-request-size=1000MB

spring.thymeleaf.mode=LEGACYHTML5 spring.thymeleaf.cache=false

spring.datasource.initialization-mode=never
spring.jpa.hibernate.ddl-auto= none

spring.datasource.url=jdbc:db2://localhost:50000/bluecost
spring.datasource.username=db2admin
spring.datasource.password=db2admin
spring.datasource.driver-class-name=com.ibm.db2.jcc.DB2Driver

所有单元测试用例都使用与数据库的 JDBC 连接,这需要 DB 启动并运行(使用正确的模式)才能使测试用例工作,这很痛苦。我们如何将测试上下文的实现换成另一种类型的数据库——比如 HSQLDB?

我在文档中读到 S-boot 可以自动配置 HSQLDB,我们所要做的就是将其指定为依赖项,但是如何告诉它在正确运行应用程序时使用 HSQLDB 运行单元测试用例使用 DB2?!

更新:我在 pom.xml 中添加了一个 hsqldb 依赖项,重新启动了应用程序,但没有发现任何区别...

【问题讨论】:

  • 将此作为编辑添加到您的帖子末尾,这样就不太可能有人错过您的评论。
  • 我担心没人能帮我解决这个问题......

标签: spring-boot spring-jdbc spring-boot-test


【解决方案1】:

我通过阅读文档找到了自己的答案。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-07-21
    • 1970-01-01
    • 2015-11-05
    • 2017-01-30
    • 1970-01-01
    • 2012-05-14
    • 2016-06-05
    • 2016-12-17
    相关资源
    最近更新 更多