使用dbunit和hikari的Spring 2.0单元测试

时间:2018-11-16 22:25:20

标签: java spring-boot hikaricp dbunit spring-test-dbunit

我有一个Spring Boot 2.0 Web服务,该服务在SQL数据库上执行基本的Crud操作。要测试数据库,我想使用DBUnit。我的Test类具有以下注释:

@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest
@ActiveProfiles("dbunit")
@TestExecutionListeners({DependencyInjectionTestExecutionListener.class,
        DirtiesContextTestExecutionListener.class,
        TransactionalTestExecutionListener.class,
        DbUnitTestExecutionListener.class})
@DatabaseSetup("classpath:dbunit/myDataset.xml")
public class MyDbunitTest

在我的application.yml中,我具有以下配置:

...other properties...
---
spring:
  profiles: dbunit
  datasource:
    platform: dbunit
    url: jdbc:h2:mem:dbtest-local
    hikari:
      driver-class-name: org.h2.Driver
      schema: classpath:dbunit/schema-dbunit.sql

当我开始测试时,Spring正常启动,并且活动配置文件是dbunit。 hikari连接池无法启动,表示找不到“ classpath:dbunit / schema-dbunit.sql”:

[           main]o.s.jdbc.datasource.DataSourceUtils      : Fetching JDBC Connection from DataSource
[           main]com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Starting...
[           main] com.zaxxer.hikari.pool.HikariPool        : HikariPool-1 - Exception during pool initialization.

org.h2.jdbc.JdbcSQLException: Schema "classpath:dbunit/schema-dbunit.sql" not found [90079-196]
    at org.h2.message.DbException.getJdbcSQLException(DbException.java:345) ~[h2-1.4.196.jar:1.4.196]
    at org.h2.message.DbException.get(DbException.java:179) ~[h2-1.4.196.jar:1.4.196]
    at org.h2.message.DbException.get(DbException.java:155) ~[h2-1.4.196.jar:1.4.196]
    at org.h2.engine.Database.getSchema(Database.java:1755) ~[h2-1.4.196.jar:1.4.196]

使用Spring Boot 1.5,查找该文件从来没有任何问题。升级到Spring Boot 2.0时,文件名或位置未更改。我的项目设置是:

SRC
+Main
|  + Java
|  + Resources
+Test
  +Java
  | + MyDbunitTest.java
  +Resources
    +dbunit
    |  + schema-dbunit.sql
    |  + myDataset.xml
    + application.yml
    + bootstrap.yml

我的文件是否位于错误的位置,还是Hikari希望文件遵循命名对流?我需要更改一些内容以将文件添加到类路径吗?

0 个答案:

没有答案