单元测试数据库时遇到多个模式的问题

时间:2015-05-13 16:04:47

标签: java hsqldb dbunit

我正在使用HypserSQL DB和DBUnit进行单元测试。不幸的是,我的应用程序连接到多个架构(不能改变它)。每当模式名称发挥作用时,我都会得到org.dbunit.dataset.NoSuchTableException

当我在databaseSchema.sql中使用'CREATE SCHEMA'时,数据集似乎插入得很好;但是,当我的DAO试图拉出引用我的模式的东西时,我得到了例外。

当我在databaseSchema.sql中使用'CREATE USER'时,加载数据集时会抛出异常。

我的测试类的相关部分

@Before
public void setUp() throws Exception {
    IDatabaseConnection iDBConn = getConnection();

    //Enable
    String qualifiedTableNames = "http://www.dbunit.org/features/qualifiedTableNames";
    iDBConn.getConfig().setProperty(DatabaseConfig.FEATURE_QUALIFIED_TABLE_NAMES, true);

    initSchema();
    insertDataSet(iDBConn);
    target = new OracleDAO(jdbcConnection, null);
}

protected IDatabaseConnection getConnection() throws Exception {
    Class.forName("org.hsqldb.jdbcDriver");
    jdbcConnection = DriverManager.getConnection("jdbc:hsqldb:mem:fdr", "sa", "");

    return new DatabaseConnection(jdbcConnection);
}

protected void initSchema() throws Exception {
    FileReader fileReader = new FileReader(new File(this.getClass().getResource("/databaseSchema.sql").getFile()));
    BufferedReader br = new BufferedReader(fileReader);
    StringBuffer sb = new StringBuffer();
    while (br.ready()) {
        sb.append(br.readLine());
    }

    Statement stmt = jdbcConnection.createStatement() ; 
    String[] queries = sb.toString().split(";");
    for (String query : queries) {
        stmt.execute(query);
    }
}

protected IDataSet insertDataSet(IDatabaseConnection iDBConn) throws Exception {
    loadedDataSet = new FlatXmlDataSet(this.getClass().getClassLoader().getResourceAsStream("dataset.xml"));
    DatabaseOperation.CLEAN_INSERT.execute(iDBConn, loadedDataSet);
    return loadedDataSet;
}

databaseSchema.sql

SET DATABASE SQL SYNTAX ORA TRUE;

CREATE SCHEMA SCHEMA_A;
--Also tried CREATE USER SCHEMA_A pwd;

CREATE TABLE if not exists SCHEMA_A.TBL_1
(   "ID" NUMBER, 
    "OTHER_ID" NUMBER(10,0)
);

dataset.xml

<?xml version="1.0" encoding="UTF-8"?>
<dataset>      
    <SCHEMA_A.TBL_1 ID="1" other_id="1" />
</dataset>

1 个答案:

答案 0 :(得分:1)

更新dbUnit的maven依赖项修复了此问题。现在我可以处理多个模式。

<dependency>
    <groupId>org.dbunit</groupId>
    <artifactId>dbunit</artifactId>
    <version>2.5.1</version>
    <scope>test</scope>
</dependency>