【发布时间】:2012-10-15 21:22:15
【问题描述】:
我刚刚开始学习 JUnit。我试着在别处寻找这个答案,但我找不到。假设我编写了一个 JUnit 测试方法,它需要一个异常,如下所示:(数学是我编写的类的一个实例)
@Test(expected = NumberFormatException.class)
public void testAdd_String() {
System.out.println("testing add number to string");
double result = math.add("lakd", "2");
}
现在我要测试的方法:
public double add(String operand1, String operand2) {
int num1 = Integer.parseInt(operand1);
int num2 = Integer.parseInt(operand2);
return num1 + num2;
}
当我在方法头上有throws NumberFormatException 时和它不存在时,我的测试都通过了。这是预期的行为还是 JUnit?有没有办法通过 JUnit 测试来确保 throws 子句存在于我的方法中?
【问题讨论】:
-
我在 netbeans 中使用 JUnit4。
标签: unit-testing testing exception-handling junit