我刚开始学习JUnit。我试着在别处寻找这个答案,但我找不到它。假设我写的期待异常的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。 – imrandy85