2012-02-09 109 views

回答

7

使用@Parameters註釋的data()方法的類型是List<Object[]>,因此您可以放入任何對象。

要通過在,例如,一個Money對象,您的陣列被轉換爲一個列表將是:

{{新錢(26, 「CHF」)}, {新錢(12 ,「USD」)}}

測試類的構造函數應該接受一個Money對象作爲參數。

0

最近我開始zohhak項目。它可以讓你寫:

@TestWith({ 
    "25 USD, 7", 
    "38 GBP, 2", 
    "null, 0" 
}) 
public void testMethod(Money money, int anotherParameter) { 
    ... 
} 
0

使用對象也可以使用Junit @Parameters

例子: -

@RunWith(Parameterized.class) 
public class TestParameter { 

@Parameter(value=0) 
public int expected; 

@Parameter(value=1) 
public int first; 

@Parameter(value=2) 
public int second; 
private Calculator myCalculator; 


@Parameters(name = "Test : {index} : add({1}+{2})= Expecting {0}")//name will be shared among all tests 
public static Collection addNumbers() { 
    return Arrays.asList(new Integer[][] { { 3, 2, 1 }, { 5, 2, 3 }, { 9, 8, 1 }, { 200, 50, 150 } }); 
} 
@Test 
public void testAddWithParameters() { 
    myCalculator = new Calculator(); 
    System.out.println(first + " & " + second + " Expected = " + expected); 
    assertEquals(expected, myCalculator.Add(first, second)); 
} 

}