views:

159

answers:

1

I'm using dBUnit and am trying to assert a row of my pre-defined FlatXmlDataSet with the actual DB dataset. The problem is the getValue for my Verification Code field returns a string when that data originates from the FlatXmlDataSet and as an Integer when its from the actual live database. This is true with any data type that isn't a String. I could write something to convert everything to a string before compare but is there a better way?

My code:

 IDataSet initialSet = setupDBWithData("test.xml", true);
 ITable initialTable = initialSet.getTable("USER");

 String response = doTestSendVerificationPin();
 assertOKResponse(response);

 ITable userTable = _databaseTester.getConnection().createTable("USER");

 // Make sure old user row is left unchanged
 assertRowsEqual(initialTable, 0, userTable, 0);

... (further down)

protected void assertRowsEqual(ITable expected, int rowExpected, ITable actual, int rowActual) throws Exception
{
 ITableMetaData metaData = expected.getTableMetaData();
 Column [] cols = metaData.getColumns();

 for (int i = 0; i < cols.length; i++)
 {
  String colName = cols[i].getColumnName();
  assertEquals(expected.getValue(rowExpected, colName), actual.getValue(rowActual, colName));
 }

}

This the test.xml that I slurp in:

<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE dataset SYSTEM "../Tests/testdata/common/some.dtd"> 

<dataset>
  <USER ID="132" FIRST_NAME="Joe" LAST_NAME="Bob" VERIFICATION_CODE="1869" />
</dataset>
+1  A: 

Sorry if this doesn't answer your question exactly, but this seems to me to be a sort of unusual way to use DBUnit. Usually you use DBUnit to load data into the DB and then use SQL to query the database and compare that with some other known result. I haven't seen anyone try to compare the DBUnit IDataSet as part of an assertion - I don't think DBUnit is intended to be used this way.

Ken Liu
Actually I am doing just that -- sorry I forgot to add something in my code -- it should be clearer now. Basically I 1. pre-populate my data with pre-canned stuff from a data set. 2. Query the real database after my operation (_databaseTester.getConnection().createTable("USER") does this), and 3. assert that my data has not changed in any adverse way compared to my original pre-populated data.
Ish
Hmm...not sure. I was under the impression that DBUnit did not know the types of the dataset until it does the insert (using DB table metadata) but the getTableMetaData() method leads me to think otherwise. You might have to peek at the source code for this one.
Ken Liu