0

I'm trying to do something simple---what I assumed would be the most simple thing to do with DBUnit---and I admit I'm lost.

I just want to load a DBUnit database state into an in-memory H2 database so I can run tests on the H2 database.

In Maven I manually use dbunit-maven-plugin with PostgresqlDataTypeFactory to generate dbunit.xml from an existing database. But this flat file XML database indicates no column types whatsoever. Should the DBUnit database dump in XML contain type information?

Following the online instructions, I try to create a JUnit test to simply load that XML file:

@BeforeClass
public static void setupDBUnit() throws ClassNotFoundException, SQLException, DatabaseUnitException {
  Class.forName("org.h2.Driver");
  Connection jdbcConnection = DriverManager.getConnection("jdbc:h2:mem:foo");
  IDatabaseConnection connection = new DatabaseConnection(jdbcConnection);
  IDataSet dataSet=new FlatXmlDataSetBuilder().build(FooTest.class.getResourceAsStream("dbunit.xml"));
  try {
    DatabaseOperation.CLEAN_INSERT.execute(connection, dataSet);
  } finally {
    connection.close();
  }
}

I put in a fake test and run the test. I get a lot of debug statements; it seems like it's reading something, although I see no values I recognize:

09:55:29.802 [main] DEBUG o.dbunit.dataset.OrderedTableNameMap - getTableName(tableName=value) - start
09:55:29.802 [main] DEBUG o.dbunit.dataset.OrderedTableNameMap - getTableName(tableName=value) - end - result=VALUE
09:55:29.802 [main] DEBUG o.dbunit.dataset.OrderedTableNameMap - getTableName(tableName=VALUE) - start
09:55:29.802 [main] DEBUG o.dbunit.dataset.OrderedTableNameMap - getTableName(tableName=VALUE) - end - result=VALUE
09:55:29.802 [main] DEBUG org.dbunit.dataset.CachedDataSet - startTable(metaData=tableName=value, columns=[], keys=[]) - start
09:55:29.802 [main] DEBUG o.dbunit.dataset.xml.FlatXmlProducer - endElement(uri=, localName=, qName=value) - start
09:55:29.802 [main] DEBUG o.dbunit.dataset.xml.FlatXmlProducer - startElement(uri=, localName=, qName=value, attributes=com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser$AttributesProxy@63e2203c) - start
09:55:29.802 [main] DEBUG o.dbunit.dataset.OrderedTableNameMap - getLastTableName() - start
09:55:29.802 [main] DEBUG o.dbunit.dataset.OrderedTableNameMap - getTableName(tableName=value) - start
09:55:29.802 [main] DEBUG o.dbunit.dataset.OrderedTableNameMap - getTableName(tableName=value) - end - result=VALUE
...

Then it starts initializing the data, but there are null values that are obviously incorrect:

09:55:29.851 [main] DEBUG org.dbunit.database.DatabaseDataSet - getTableMetaData(tableName=null) - start
09:55:29.851 [main] DEBUG org.dbunit.database.DatabaseDataSet - initialize() - start
09:55:29.851 [main] DEBUG org.dbunit.database.DatabaseDataSet - Initializing the data set from the database...
09:55:29.876 [main] DEBUG org.dbunit.database.DatabaseDataSet - 
    database product name=H2
    database version=1.4.184 (2014-12-19)
    database major version=1
    database minor version=4
    jdbc driver name=H2 JDBC Driver
    jdbc driver version=1.4.184 (2014-12-19)
    jdbc driver major version=1
    jdbc driver minor version=4

09:55:29.876 [main] DEBUG org.dbunit.database.DatabaseDataSet - metadata resultset=rs0: org.h2.result.LocalResult@27f723 columns: 11 rows: 0 pos: -1
09:55:29.876 [main] DEBUG o.dbunit.dataset.OrderedTableNameMap - getTableName(tableName=null) - start
09:55:29.876 [main] DEBUG o.dbunit.dataset.OrderedTableNameMap - getTableName(tableName=null) - end - result=NULL
09:55:29.876 [main] ERROR org.dbunit.database.DatabaseDataSet - Table 'null' not found in tableMap=org.dbunit.dataset.OrderedTableNameMap[_tableNames=[], _tableMap={}, _caseSensitiveTableNames=false]
09:55:29.876 [main] DEBUG o.d.d.s.AbstractBatchStatement - close() - start
09:55:29.876 [main] DEBUG o.dbunit.database.DatabaseConnection - close() - start

Why am I getting an error Table 'null' not found in tableMap?

Garret Wilson
  • 18,219
  • 30
  • 144
  • 272

1 Answers1

0

As far as I can tell, apparently DBUnit does not save and load the database schema (i.e. no column type information is included), making it far less useful than I had assumed. Maybe that was the source of the Table 'null'... error, as well---I don't know.

Now I have to figure out how to easily dump my PostgreSQL schema and get it into H2 for in-memory testing. Maybe I can use Runscript for this, if I can tease out my PostgreSQL schema initialization statement. In any case, DBUnit wasn't as easy a dump/load testing tool as I had hoped.

Community
  • 1
  • 1
Garret Wilson
  • 18,219
  • 30
  • 144
  • 272