3
votes

I've been bing/google (boogle) searching and I have found a few hits (mostly on stackoverflow) and I have tried to apply the suggested solutions with no luck.

I'm using Java 8 and DBUnit 2.5.1 and mysql-connector-java 5.1.36 and mysql (Ver 14.14 Distrib 5.6.15, for Win64 (x86_64)).

I'm reading an XML file previously created by DBUnit that defines several tables one of which is called "trip". My instance of mysql has an empty database called "dbunit" and my expectation is that my code will read the XML and populate the database "dbunit" with the tables and the table rows with the data in the XML files.

Here is a fragment of my datafile that was created by dbunit from the mysql database "pocketguide3rded" (based on the book SQL Pocket Guide 3rd Edition).

<?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>

Here is my code.

    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;  
}

I'm getting this exception on the call to CLEAN_INSERT.execute.

Here is the output from my attempt to run a junit test.

JUnit version 4.11

.ERROR DatabaseDataSet - Table 'trip' not found in tableMap=org.dbunit.dataset.OrderedTableNameMap[_tableNames=[], _tableMap={}, _caseSensitiveTableNames=true]

E

Time: 0.464

There was 1 failure:

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

org.dbunit.dataset.NoSuchTableException: trip at org.dbunit.database.DatabaseDataSet.getTableMetaData(DatabaseDataSet.java:288) at org.dbunit.operation.DeleteAllOperation.execute(DeleteAllOperation.java:109) at org.dbunit.operation.CompositeOperation.execute(CompositeOperation.java:79) at com.heintze.demos.wikidot_com_demoimportexport.DemoDBUnit.fullDatabaseImport(DemoDBUnit.java:265) at 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) at java.lang.reflect.Method.invoke(Method.java:483) at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44) at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17) at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229) at org.junit.runners.ParentRunner.run(ParentRunner.java:309) at org.junit.runners.Suite.runChild(Suite.java:127) at org.junit.runners.Suite.runChild(Suite.java:26) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229) at org.junit.runners.ParentRunner.run(ParentRunner.java:309) at org.junit.runner.JUnitCore.run(JUnitCore.java:160) at org.junit.runner.JUnitCore.run(JUnitCore.java:138) at org.junit.runner.JUnitCore.run(JUnitCore.java:117) at org.junit.runner.JUnitCore.runMain(JUnitCore.java:96) at org.junit.runner.JUnitCore.runMainAndExit(JUnitCore.java:47) at org.junit.runner.JUnitCore.main(JUnitCore.java:40)

FAILURES!!! Tests run: 1, Failures: 1

/cygdrive/c/Users/siegfried/Documents/spring-tool-suite-workspaces/DBUnitDemo/wikidot-com-demoimportexport/src/test/java/com/heintze/demos/wikidot_com_demoimportexport

1

1 Answers

1
votes

The problem is that DBUnit just fills the tables with data. It does no actually create the tables. I remedied this by overriding the getSetupOperation like this:

@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();
    }

Here, in the getSetupOperation I get a direct connection to my hsql test database. With that connection, I can call the createTables method which actually creates my table. This is done in this overridden method because this way I am sure that the tables are always created before the