2011-08-04 70 views
0

我將非常感謝使用dbunit的一些幫助。 我使用postgresql9作爲數據庫。關於dbunit中的列類型以及爲數據集創建dtd的困惑

我使用hibernate的hbm2ddl工具創建了表格簿。

我想創建用於測試一些xmldatasets與dbunit.Using螞蟻DBUnit的任務我出口值從數據庫到 initialdataset.xml,我清理插入到數據庫之前每test.Deleting一些行,我創建了一個expecteddataset.xml。如果我要將從db 創建的表與從expecteddataset.xml創建的表進行比較,我想我需要定義一個dtd.I,使用以下代碼創建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"); 

創建的DTD如下

... 
<!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給出就像是這個 - expecteddataset xml

我的Postgres數據庫表 '書' 的形式

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) 

的令我困惑的是,publish_date字段(它是postgres中的日期類型),book_id(bigint類型),價格(實際類型) 也被視爲CDATA.How可以由字段的字段類型的字段類型的數據庫從哪個 有字段變化爲長,日期等檢索表?

在testcode我試圖

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

這將導致AssertionFailedError異常。

堆棧跟蹤是

junit.framework.AssertionFailedError: 

expected:<[email protected]> 

but was:<[email protected]> 

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) 

有什麼不對的,我在這裏做什麼?我必須提供有關列類型爲表元素的一些信息? 如果有人能幫我解決這個問題,那就太好了。

DbUnitUtils類來創建數據集

class DbUnitUtils { 
    public static IDatabaseConnection createConnection(){ 
     ... 
    } 
    public static IDataSet createDataSet(String file) throws DataSetException, IOException{ 
     return new FlatXmlDataSet(new File("data/dbunit/"+file)); 
    } 
} 

PS: 我用DbUnit-2.2.2和2.4.8版本,同樣results..So試過,那一定是,我失去了一些東西至關重要正確運行dbunit

回答

1

該錯誤是因爲junit Assert不知道Dbunit類(ITable等)的相等性,並且在assertEquals()中失敗。我應該使用assertEquals() from dbunit's Assertion class

這樣一個簡單的基本事實忽略了......讓我幾天的悲傷

0

您正在比較對象本身而不是內容。你可能想看看EasyMock這樣的東西,它真的適合這種測試。另外EasyMock將幫助打破實際使用數據庫的依賴性。