Confusion about column types in dbunit and creating dtd for datasets
I would appreciate some help with using dbunit. I use postgresql9 as db.
I created table book using hibernate's hbm2ddl tool.
I wanted to create some xmldatasets for testing with dbunit.Using the ant DBUnit task I exported values from db to a initialdataset.xml ,which I clean-insert to db before every test.Deleting some rows ,I created an expecteddataset.xml. If I am to compare a table created from db with a table created from expecteddataset.xml ,I think I need to define a dtd.I used the following code to create dtd.
public static void createDTD(String dtdFileName) throws FileNotFoundException...{
IDatabaseConnection connection = DbUnitUtils.createConnection();
FlatDtdDataSet.write(connection.createDataSet(),new FileWriter("data/dbunit/"+dtdFileName));
connection.close();
}
...
createDTD("myschema.dtd");
The created dtd is given below
...
<!ELEMENT book EMPTY>
<!ATTLIST book
book_id CDATA #REQUIRED
isbn CDATA #REQUIRED
book_name CDATA #REQUIRED
publish_date CDATA #IMPLIED
price CDATA #REQUIRED
description CDATA #IMPLIED
publisher_id CDATA #IMPLIED
author_id CDATA #IMPLIED
>
...
expecteddataset.xml is like this-expecteddataset xml
My postgres db table 'book' is of the form
Column | Type | Modifiers
--------------+------------------------+-----------
book_id | bigint | not null
isbn | character varying(255) | not null
book_name | character varying(255) | not null
publish_date | date |
price | real | not null
description | character varying(255) |
publisher_id | bigint |
author_id | bigint |
Indexes:
"book_pkey" PRIMARY KEY, btree (book_id)
"book_isbn_key" UNIQUE, btree (isbn)
Foreign-key constraints:
"fk1f32e959a9fc15" FOREIGN KEY (author_id) REFERENCES author(author_id)
"fk1f32e9b6bbf81f" FOREIGN KEY (publisher_id) REFERENCES publisher(publisher_id)
What confuses me is that the publish_date field(which is a date type in postgres) , book_id( bigint type),price(real type) are also treated as CDATA.How can a table made from fields which are String types equal to a table retrieved from db which have fields varying as Long,Date etc?
In testcode I tried
removeSomeRowsFromBookTable();
ITable actualBookTable = connection.createQueryTable("book", "select BOOK_ID,ISBN,...from BOOK");
IDataSet expectedDataSet = DbUnitUtils.createDataSet("expecteddataset.xml.xml");
ITable expectedBookTable = expectedDataSet.getTable("book");
Assert.assertEquals(expectedBookTable, actualBookTable);
This causes AssertionFailedError.
The stacktrace is
junit.framework.AssertionFailedError:
expected:<org.dbunit.dataset.DefaultTable@12d8ecd>
but was:<org.dbunit.database.CachedResultSetTable@1fa5e5e>
at junit.framework.Assert.fail(Assert.java:47)
at junit.framework.Assert.failNotEquals(Assert.java:282)
at junit.framework.Assert.assertEquals(Assert.java:64)
at junit.framework.Assert.assertEquals(Assert.java:71)
at myapp.test.cascades.HibernateCascadeTests.testCascading(Unknown Source)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:76)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:673)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:846)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1170)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:125)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109)
at org.testng.TestRunner.runWorkers(TestRunner.java:1147)
at org.testng.TestRunner.privateRun(TestRunner.java:749)
at org.testng.TestRunner.run(TestRunner.java:600)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:317)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:312)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:274)
at org.testng.SuiteRunner.run(SuiteRunner.java:223)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1039)
at org.testng.TestNG.runSuitesLocally(TestNG.java:964)
at org.testng.TestNG.run(TestNG.java:900)
at org.testng.TestNG.privateMain(TestNG.java:1182)
at org.testng.TestNG.main(TestNG.java:1146)
Is there something wrong in what I am doing here?Do I have to provide some info about the column types for the table elements? If someone can help me solve this,it would be nice.
DbUnitUtils class to create datasets
class DbUnitUtils {
public static IDatabaseConnection createConnection(){开发者_StackOverflow
...
}
public static IDataSet createDataSet(String file) throws DataSetException, IOException{
return new FlatXmlDataSet(new File("data/dbunit/"+file));
}
}
p.s: I tried this with dbunit-2.2.2 and 2.4.8 versions with same results..So, it must be that I am missing something vital to running dbunit properly
The error was because junit Assert
does not know about equality of Dbunit classes(ITable
etc) and was failing in assertEquals()..I should have used assertEquals() from dbunit's Assertion class
.
such a simple overlook of basic facts..caused me couple of days ' grief
You're comparing the objects themselves rather than the contents. You might want to look at something like EasyMock which is really geared towards this kind of testing. Also EasyMock will help break the dependency of actually using a database.
精彩评论