DbUnit Oracle中的NoSuchTableException

时间:2013-07-14 16:52:08

标签: java junit dbunit

我正在尝试将dbunit与oracle一起使用,并且测试用例如下所示。当我尝试运行以下代码时,它给了我。我不知道我在哪里错了。有人可以帮忙吗

org.dbunit.dataset.NoSuchTableException:INVOICE_STATUS_TYPE     at org.dbunit.database.DatabaseDataSet.getTableMetaData(DatabaseDataSet.java:278)     在org.dbunit.operation.DeleteAllOperation.execute(DeleteAllOperation.java:109)     在org.dbunit.operation.CompositeOperation.execute(CompositeOperation.java:79)                                    

import junit.framework.Assert;
import org.dbunit.DBTestCase;
import org.dbunit.PropertiesBasedJdbcDatabaseTester;
import org.dbunit.database.DatabaseConfig;
import org.dbunit.dataset.IDataSet;
import org.dbunit.dataset.xml.FlatXmlDataSet;
import org.junit.Test;


public class MyTest extends DBTestCase{

private FlatXmlDataSet loadedDataSet;
public MyTest() {

    super();
    System.out.println("TEST TEST");
    System.setProperty( PropertiesBasedJdbcDatabaseTester.DBUNIT_DRIVER_CLASS, "oracle.jdbc.OracleDriver" );
    System.setProperty( PropertiesBasedJdbcDatabaseTester.DBUNIT_CONNECTION_URL, "jdbc:oracle:thin:@(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(Host=vp1.qa.nsc.com)(PORT=2186))(CONNECT_DATA=(SERVICE_NAME=FADBBD1E)))" );
    System.setProperty( PropertiesBasedJdbcDatabaseTester.DBUNIT_USERNAME, "nscdba" );
    System.setProperty( PropertiesBasedJdbcDatabaseTester.DBUNIT_PASSWORD, "dscdbapwd" );
    System.setProperty( PropertiesBasedJdbcDatabaseTester.DBUNIT_SCHEMA, "INVOICE" );
}


@Override
protected IDataSet getDataSet() throws Exception {

    //return new FlatXmlDataSetBuilder().build(new FileInputStream("dataset.xml"));

    loadedDataSet = new FlatXmlDataSet(this.getClass().getClassLoader().getResourceAsStream("dataset.xml"));
    System.out.println(loadedDataSet.getTableNames().length);
    return loadedDataSet;
}

@Test
public void testStatus() {
    System.out.println("Hey...");
    Assert.assertTrue( true );
}

protected void setUpDatabaseConfig(DatabaseConfig config) {
    config.setProperty( DatabaseConfig.FEATURE_QUALIFIED_TABLE_NAMES, true );
}
}

2 个答案:

答案 0 :(得分:2)

使用Oracle,确保数据库凭据全部为大写,即:

System.setProperty( PropertiesBasedJdbcDatabaseTester.DBUNIT_USERNAME, "MYUSERNAME" );
System.setProperty( PropertiesBasedJdbcDatabaseTester.DBUNIT_PASSWORD, "MYPASSWORD" );
System.setProperty( PropertiesBasedJdbcDatabaseTester.DBUNIT_SCHEMA, "INVOICE" );

答案 1 :(得分:0)

你在哪里调用“org.dbunit.operation.DeleteAllOperation.execute()”?

如果该表存在,请查看如何获得dbunit连接。

private IDatabaseConnection getConnectionUsing(Connection con,
        String username) throws SQLException, DatabaseUnitException {
    // oracle schema name is the user name
    IDatabaseConnection connection = new DatabaseConnection(con,
            username.toUpperCase());
    DatabaseConfig config = connection.getConfig();
    // oracle 10g
    config.setProperty(DatabaseConfig.PROPERTY_DATATYPE_FACTORY,
            new Oracle10DataTypeFactory());
    // receycle bin
    config.setProperty(
            DatabaseConfig.FEATURE_SKIP_ORACLE_RECYCLEBIN_TABLES,
            Boolean.TRUE);
    return connection;
}

private IDatabaseConnection getConnectionUsing(Connection con)
        throws DatabaseUnitException {
    return new DatabaseConnection(con);
}

如果针对oracle运行测试,则需要将db用户名作为模式名称提供。如果您对hsqldb运行测试,请不要提供用户名。

希望这有帮助。