org.dbunit.dataset.NoSuchTableException错误DatabaseDataSet - 表' trip'找不到

时间:2015-08-14 17:04:36

标签: java mysql dbunit

我一直在搜索/谷歌(博客)搜索,我发现了一些点击(主要是在stackoverflow上),我试图应用建议的解决方案,没有运气。

我使用Java 8和DBUnit 2.5.1以及mysql-connector-java 5.1.36和mysql(Ver 14.14 Distrib 5.6.15,for Win64(x86_64))。

我正在读取之前由DBUnit创建的XML文件,该文件定义了几个表,其中一个表被称为" trip"。我的mysql实例有一个名为" dbunit"的空数据库。我的期望是我的代码将读取XML并填充数据库" dbunit"表格和表格行以及XML文件中的数据。

这是我的数据文件的一个片段,它是由dbunit从mysql数据库" pocketguide3rded" (基于SQL Pocket Guide第3版)。

<?xml version='1.0' encoding='UTF-8'?>
<dataset>
  <trip name="M-28" stop="1" parent_stop="3"/>
  <trip name="M-28" stop="3"/>
  <trip name="M-28" stop="8" parent_stop="1"/>
  <trip name="M-28" stop="9" parent_stop="8"/>
  <trip name="M-28" stop="10" parent_stop="9"/>
  <trip name="M-28" stop="11" parent_stop="10"/>
  <trip name="Munising" stop="1"/>
  <trip name="Munising" stop="2" parent_stop="1"/>
  <trip name="Munising" stop="3" parent_stop="4"/>
  <trip name="Munising" stop="4" parent_stop="6"/>
  <trip name="Munising" stop="5" parent_stop="3"/>
  <trip name="Munising" stop="6" parent_stop="2"/>
  <trip name="US-2" stop="11" parent_stop="12"/>
  <trip name="US-2" stop="12" parent_stop="14"/>
  <trip name="US-2" stop="13" parent_stop="11"/>
  <trip name="US-2" stop="14"/>
</dataset>

这是我的代码。

    public static void fullDatabaseImport(File file) throws     ClassNotFoundException, 
                                                            DatabaseUnitException, 
                                                            IOException,
                                                            SQLException {
    IDatabaseConnection connection = getConnection();
    IDataSet dataSet = new FlatXmlDataSet(file, true);

    DatabaseOperation.CLEAN_INSERT.execute(connection, dataSet);
    //DatabaseOperation.INSERT.execute(connection, dataSet);
}

public static IDatabaseConnection getConnection() throws     ClassNotFoundException, 
                                                            DatabaseUnitException, 
                                                            SQLException {
    // database connection
    Class driverClass = Class.forName(_driverClass);
    Connection jdbcConnection = DriverManager.getConnection(_jdbcConnection, username, password);
    IDatabaseConnection dbConn = new DatabaseConnection(jdbcConnection/*,"dbunit"*/);
    dbConn.getConfig().setProperty(DatabaseConfig.PROPERTY_DATATYPE_FACTORY, new MySqlDataTypeFactory());
    dbConn.getConfig().setProperty(DatabaseConfig.PROPERTY_METADATA_HANDLER, new MySqlMetadataHandler());
    dbConn.getConfig().setProperty(DatabaseConfig.FEATURE_CASE_SENSITIVE_TABLE_NAMES, Boolean.TRUE);

    return dbConn;  
}

我在调用CLEAN_INSERT.execute时遇到此异常。

以下是我尝试运行junit测试的输出。

JUnit版本4.11

.ERROR DatabaseDataSet - 表&#39; trip&#39;在tableMap = org.dbunit.dataset.OrderedTableNameMap中找不到[_tableNames = [],_ tableMap = {},_ caseSensitiveTableNames = true]

电子

时间:0.464

有1次失败:

1)test5_Test(com.heintze.demos.wikidot_com_demoimportexport.DemoDBUnit)

org.dbunit.dataset.NoSuchTableException:trip     在Org.dbunit.operation.DeleteAllOperation.exe的org.dbunit.database.DatabaseDataSet.getTableMetaData(DatabaseDataSet.java:288)(DeleteAllOperation.java:109)     在org.dbunit.operation.CompositeOperation.execute(CompositeOperation.java:79)     在com.heintze.demos.wikidot_com_demoimportexport.DemoDBUnit.fullDatabaseImport(DemoDBUnit.java:265)     在com.heintze.demos.wikidot_com_demoimportexport.DemoDBUnit.test5_Test(DemoDBUnit.java:249)     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)     at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)     at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)     在java.lang.reflect.Method.invoke(Method.java:483)     在org.junit.runners.model.FrameworkMethod $ 1.runReflectiveCall(FrameworkMethod.java:47)     在org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)     在org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)     在org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)     在org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)     在org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)     在org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)     在org.junit.runners.ParentRunner $ 3.run(ParentRunner.java:238)     在org.junit.runners.ParentRunner $ 1.schedule(ParentRunner.java:63)     在org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)     在org.junit.runners.ParentRunner.access $ 000(ParentRunner.java:53)     在org.junit.runners.ParentRunner $ 2.evaluate(ParentRunner.java:229)     在org.junit.runners.ParentRunner.run(ParentRunner.java:309)     在org.junit.runners.Suite.runChild(Suite.java:127)     在org.junit.runners.Suite.runChild(Suite.java:26)     在org.junit.runners.ParentRunner $ 3.run(ParentRunner.java:238)     在org.junit.runners.ParentRunner $ 1.schedule(ParentRunner.java:63)     在org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)     在org.junit.runners.ParentRunner.access $ 000(ParentRunner.java:53)     在org.junit.runners.ParentRunner $ 2.evaluate(ParentRunner.java:229)     在org.junit.runners.ParentRunner.run(ParentRunner.java:309)     在org.junit.runner.JUnitCore.run(JUnitCore.java:160)     在org.junit.runner.JUnitCore.run(JUnitCore.java:138)     在org.junit.runner.JUnitCore.run(JUnitCore.java:117)     在org.junit.runner.JUnitCore.runMain(JUnitCore.java:96)     在org.junit.runner.JUnitCore.runMainAndExit(JUnitCore.java:47)     在org.junit.runner.JUnitCore.main(JUnitCore.java:40)

FAILURES !!! 测试运行:1,失败:1

/ cygdrive / C /用户/齐格弗里德/文档/弹簧工具套件的工作区/ DBUnitDemo / wikidot-COM-demoimportexport / SRC /测试/ JAVA / COM / heintze /演示/ wikidot_com_demoimportexport

1 个答案:

答案 0 :(得分:0)

问题是DBUnit只是用数据填充表。它实际上没有创建表。我通过重写getSetupOperation来解决这个问题:

@Override
    protected DatabaseOperation getSetUpOperation() throws Exception
    {
        dbConnection = DriverManager.getConnection("jdbc:hsqldb:mem:tests", "sa", "");
        createTables();
        return DatabaseOperation.REFRESH;
    }

    private void createTables() throws Exception {
        PreparedStatement statement = dbConnection
                .prepareStatement("CREATE TABLE digidauthsessions ( "
                        + "id INTEGER IDENTITY NOT NULL PRIMARY KEY,"
                        + "request_token VARCHAR(50) NOT NULL,"
                        + "response_token VARCHAR(50) NOT NULL,"
                        + "relatienr VARCHAR(50) NOT NULL,"
                        + "digidnameid VARCHAR(1000) NOT NULL" + ")");
        statement.execute();
        statement.close();
    }

这里,在getSetupOperation中,我得到了与hsql测试数据库的直接连接。通过该连接,我可以调用实际创建表的createTables方法。这是在这个重写的方法中完成的,因为这样我确信表总是在

之前创建