1.异常测试
对可能抛出的异常进行测试:
异常本身是方法签名的一部分: * public static int parseInt(String s) throws NumberFormatException
测试错误的输入是否导致特定的异常: * Integer.parseInt(null) * Integer.parseInt("") * Integer.parseInt("xyz")
2.异常测试的方法:
使用try..catch (Exception e)捕获异常,但这样会编写许多try..catch代码
expected测试异常,推荐
2.1示例1
package com.testList;
import org.junit.*;
import static org.junit.Assert.fail;
public class SequenceTest {
//使用try...catch
@Test
public void test1() throws Exception{
try {
Integer.parseInt(null);
fail("should throw java.lang.NumberFormatException");
}catch (NumberFormatException e){
System.out.println("pass");
}
}
//使用expected捕获异常
@Test(expected = NumberF