2010-08-17 30 views
0

以下是測試的org.dbunit.Assertion.assertEquals工作(ITable一,ITable B)DbUnit的斷言不扔未能正確

@Test 
public void testAssertion() { 
    try { 
     //Creating actual table with 2 columns 
     DefaultTable actual = new DefaultTable("table_name", 
       new Column[] { new Column("col1", DataType.INTEGER), 
         new Column("col2", DataType.VARCHAR) }); 
     actual.addRow(new Object[] { 1, "ABCD" }); 
     actual.addRow(new Object[] { 2, "BABCD" }); 
     actual.addRow(new Object[] { 3, "CCGF" }); 

     //Creating expected table with same 2 columns 
     DefaultTable expected = new DefaultTable(expected 
       .getTableMetaData()); 
     expected.addRow(new Object[] { 1, "ABCD" }); 
     expected.addRow(new Object[] { 2, "BBCD" }); 

     // Check the actual vs expected 
     Assertion.assertEquals(actual, expected); 
     //This should return a test failure since actual & expected are different. 
     //But its not throwing any test case failure. 
    } catch (DataSetException e1) { 
     e1.printStackTrace(); 
    } catch (DatabaseUnitException e) { 
     e.printStackTrace(); 
    } 
} 

這裏既有DefaultTable的價值觀不匹配和測試案例仍然JUnit不會失敗上述測試用例。我從Eclipse運行它,它會導致0錯誤和0失敗與像如下的測試用例下的無根試驗,

testAssertion [Runner: JUnit 4] 
[+] Unrooted Tests [Runner: JUnit 4] 

我調試的DBUnit的API,並根據需要將其拋出數據匹配異常,但最後從SpringJUnit4ClassRunner返回時,它不會作爲測試用例失敗而拋出。

我想我在這裏失蹤的東西。請糾正我或讓我知道這個解決方案。 感謝advace。

+0

所有我能說的是,不斷的例子越來越簡單,直到你找到了什麼繼續。 – 2010-08-19 13:04:09

+0

@丹尼爾:感謝您的反饋。我添加了一些評論,使其更容易理解,並且我無法在這個示例中進一步簡化,並且錯誤仍然存​​在。如果它拋出任何錯誤,我可以做一些谷歌搜索,但它不會拋出任何失敗/錯誤:( – raksja 2010-08-24 06:07:35

回答

-1

斷言是DBUnit拋出AssertionError。

我發現得到確切的異常原因的最好辦法是抓住它,例如: -

try 
{ 
    Assertion.assertEquals(expectedTable, actualTable); 
} 
catch (AssertionError e) 
{ 
    logger.error("Assertion failed with error : " + e.getMessage()); 
}