2012-07-26 240 views
0
void insertAllRecords(){ 
    insertRecord("tom", 100); 
    insertRecord("jake", 200); 
    insertRecord("tim", 300); 
    insertRecord("andy", 400); 
    insertRecord("mike", 500); 
} 

我正在寫JUnit測試具有測試數據庫的東西。它從一個空的數據庫開始。 我想測試不同的東西,但我不想有相同的參數重複insertRecord設置JUnit測試數據庫

是否有一種簡單的方法來指定要執行的行?例如,我只想將tom和andy添加到@test函數之一中。

我知道每個測試方法,數據庫啓動爲0 我希望能夠到:

    在種皮
  • ,與湯姆和傑克設立分貝。
  • 在testB中,設置db和andy 和mike。
  • 在testC中,使用它們設置db。

但我不想在每個測試中放入單獨的插入語句。我想能夠用一些參數調用insertAllRecords。

謝謝

+1

在集成測試中,您應該在測試完成後回滾數據庫記錄。這樣,你不必擔心重複。 – 2012-07-26 02:57:32

+0

我可能有每個列有幾個數組,並有一個循環來添加我想要的。有沒有更好的辦法? – 2012-07-26 03:08:49

+0

你爲什麼不嘲笑你的數據?像mockito ... – aviad 2012-07-26 03:36:11

回答

0

據我所知,JUnit不支持你想要的行爲。您可能想要看看TestNG,它允許使用@BeforeMethod@AfterMethod註釋進行測試特定設置。並且它還允許使用參數化數據使用@Parameter@DataProvider註釋來獲得更靈活的測試數據。

0

轉換您的JUnit類以一個抽象類與抽象方法abstract void insertAllRecords();

然後在案件繼承這個類,你所需要的。例如,

class TestWithTomClass extends AbstractTestClass{ 
    public void insertAllRecords(){ 
     insertRecord("tom", 100); 
    } 

    @Test 
    public void myTestWithTom(){ 
     .... 
    }