在Junit 4
参数化测试中,如果我在测试类中有 3 个测试并且我想为特定测试使用不同的参数,我应该怎么做?
可以说 may 3rd test 检查是否抛出了特定的异常。所以我只需要将错误的参数传递给那个测试。
@RunWith(Parameterized.class)
public class EvenNumberProcessorTest {
private int num1;
private int num2;
public int EvenNumberProcessorTest(int num1, int num2) {
this.num1 = num1;
this.num2 = num2;
}
@Parameterized.Parameters
public static Collection<Integer[]> numbers() {
return Arrays.asList(new Integer[][] {
{4, 2},
{5, 2}
});
}
@Test
public void testCheckDivision() {
Assert.asserEquals(true, EvenNumberProcessor.checkDivision(num1, num2));
}
@Test(expected = MyException.class)
public void testCheckDivisionFail() {
DivisionProcessor.checkDivision(3, 0);
}
}
public boolean checkDivision(int num1, num2) {
boolean result = false;
try {
if (num1 % num2 == 0) {
result = true;
} else {
result = false;
}
} catch (ArithmeticException e) {
throw new MyException("You can not use 0 to divide");
}
return result;
}