Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Unit testing 使用非原语参数的参数化JUnit测试?_Unit Testing_Junit_Parameterized - Fatal编程技术网

Unit testing 使用非原语参数的参数化JUnit测试?

Unit testing 使用非原语参数的参数化JUnit测试?,unit-testing,junit,parameterized,Unit Testing,Junit,Parameterized,使用参数运行JUnit测试是一种很好的可能性,其中使用不同的数据多次执行相同的测试方法,如下所述: 不幸的是,似乎只能使用基本参数或字符串,而不能使用对象。是否有已知的解决方法?使用注释的data()方法的类型是List,因此可以放入任何对象 要传入(例如,Money对象),要转换为列表的数组将是: {{新货币(26,“瑞士法郎”)}, {新货币(12美元)} 测试类的构造函数应该使用Money对象作为参数。使用JUnitParams代替 最近我开始了这个项目。它可以让你写: @TestWith

使用参数运行JUnit测试是一种很好的可能性,其中使用不同的数据多次执行相同的测试方法,如下所述:

不幸的是,似乎只能使用基本参数或字符串,而不能使用对象。是否有已知的解决方法?

使用注释的
data()
方法的类型是
List
,因此可以放入任何对象

要传入(例如,
Money
对象),要转换为列表的数组将是:

{{新货币(26,“瑞士法郎”)}, {新货币(12美元)}


测试类的构造函数应该使用Money对象作为参数。

使用JUnitParams代替

最近我开始了这个项目。它可以让你写:

@TestWith({
“25美元,7美元”,
“38英镑,2英镑”,
空,0
})
public void testMethod(Money Money,int其他参数){
...
}

使用对象也可以使用Junit
@参数

示例:-

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

}

链接不再可用您是否找到解决此问题的方法?