2011-08-04 7 views
0

dbunitの使用に関する助けに感謝します。 postgresql9をdbとして使用します。dbunitのカラム型についての混乱とデータセット用のdtdの作成

私はhibernateのhbm2ddlツールを使用してテーブルブックを作成しました。私は私がきれいに挿入し、すべてのいくつかの行をtest.Deleting前に、DBへ initialdataset.xmlにDBから値をエクスポートアリDBUnitのタスクをdbunit.Usingとテストのためのいくつかのxmldatasetsを作成したい

、私が作成しましたexpecteddataset.xml。 db から作成したテーブルとexpecteddataset.xmlから作成したテーブルを比較する場合は、dtdを定義する必要があると思います。次のコードを使用して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はexpecteddataset.xmlマイPostgresのDBテーブルの本は '形式

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) 

であるthis- expecteddataset xml

のようなものです

... 
<!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 
> 
... 

以下に示します。私に混乱を招くのは、publish_dateフィールド(postgresの日付タイプ)、book_id(bigintタイプ)、 は、CDATAとして扱われます。テーブルからdbから検索されたテーブルと等しいString型のフィールドからテーブルを作成することはできますか? には、Long、Dateなどのフィールドがありますか? testcodeで

私はこれがAssertionFailedErrorを引き起こし

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

を試してみました。

スタックトレースが

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) 

ですが、私はここでやっているものに何か問題はありますか?私はテーブル要素の列タイプに関するいくつかの情報を提供する必要がありますか? 誰かが私にこれを解決するのを手伝ってもらえればいいですね。データセット

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

を作成する

DbUnitUtilsクラスPS: 私は同じresults..SoとDbUnitを-2.2.2と2.4.8のバージョンでこれを試してみました、それは私がに不可欠な何かが欠けていますということでなければなりません

答えて

1

エラーでした。

このような簡単な基本的な事実の見落としは、私に数日間の悲しみをもたらしました。

0

あなたは内容ではなくオブジェクトそのものを比較しています。この種のテストに実際に対応しているEasyMockのようなものを見たいかもしれません。また、EasyMockは実際にデータベースを使用する依存関係を解消するのに役立ちます。 junit AssertはDbUnitをクラス(ITableなど)の平等について知っていないと..私はassertEquals() from dbunit's Assertion classを使用している必要があります()のassertEqualsに失敗したため

関連する問題