2016-07-16 45 views
0

最近我開始使用DbUnit,我正在嘗試編寫一個非常簡單的集成測試,只是爲了填充3行的表。閱讀DbUnit Getting Started Guide,它告訴我創建一個數據集文件。我的數據集xml文件看起來就像這樣:JUnit + DbUnit - 擴展時未找到測試DatabaseTestCase

<dataset> 
    <notaFiscal cliente="Cliente 1" valor="26.5" data='2016-04-04'/> 
    <notaFiscal cliente="Cliente 2" valor="30.5" data='2016-05-01'/> 
    <notaFiscal cliente="Cliente 3" valor="28.2" data='2015-08-11'/> 
</dataset> 

然後,我要創建延伸DBTestCase測試類並實現我的測試方法(與@Test註釋,就像任何其他JUnit測試案例)。我創建的類如下:

public class GerenciadorNFTest extends DBTestCase { 

    private GerenciadorNotaFiscal gerenciador = new GerenciadorNotaFiscal(); 

    public GerenciadorNFTest(String name) 
    { 
     super(name); 
     // PBJDT is an abbreviation of PropertiesBasedJdbcDatabaseTester 
     // just for a better visualization 
     System.setProperty(PBJDT.DBUNIT_DRIVER_CLASS, 
      "org.postgresql.Driver"); 
     System.setProperty(PBJDT.DBUNIT_CONNECTION_URL, 
      "jdbc:postgresql://localhost:5432/dbunit"); 
     System.setProperty(PBJDT.DBUNIT_USERNAME, "postgres"); 
     System.setProperty(PBJDT.DBUNIT_PASSWORD, "123456"); 
    } 


    protected IDataSet getDataSet() throws Exception { 
     IDataSet dataSet = new FlatXmlDataSetBuilder().build(
      new FileInputStream("notas_fiscais.xml")); 
     return dataSet; 
    } 

    @Test 
    public void geraPedido() { 
     Pedido p = new Pedido("Diogo", 26d, 5); 
     gerenciador.gera(p); 
     NotaFiscal notaFiscal = gerenciador.recupera("Diogo"); 
     Assert.assertEquals(notaFiscal.getCliente(), "Diogo"); 
    } 

} 

在那之後,我試圖運行測試用例,但得到了以下錯誤:

junit.framework.AssertionFailedError: No tests found in teste.GerenciadorNFTest 

    at junit.framework.Assert.fail(Assert.java:57) 
    at junit.framework.TestCase.fail(TestCase.java:227) 

如果我試圖刪除extend DBTestCase,JUnit的識別測試情況下,並正常運行,但延長它沒有。我試圖清理並重新編譯,但沒有奏效。我也嘗試在我使用的IDE之外運行測試(Intellij Idea),但是我又沒有成功。

有沒有人經歷過同樣的問題? 非常感謝您提前。任何幫助將不勝感激。

回答

2

有可能是原因的JUnit 3 vs 4 runner差異(您沒有提到JUnit和dbUnit版本,也沒有提及如何管理它們)。不同的工具有不同的運行默認要求(例如,Maven默認只運行類作爲類名後綴爲「Test」的測試)。

請注意,它不需要擴展一個dbUnit類(我不),不這樣做應該消除遇到的問題。只是往後你提到的頁面是兩個部分介紹如何:

並結合兩者是我多年來所做的事情 - 擁有自己的父類測試類,然後使用DI(或實例化)所需的DBTestCase(通常爲PrepAndExpectedTestCase)。

+0

謝謝!它使用了一個IDatabaseTester。 :) –