当一个被测类中有异常时,如何处理?
如:一个原始的被测类;
public class UserExceptionDemo {
public int age;
public String name;
public void setAge(int age){
if(age<0||age>100){
throw new IllegalArgumentException("ageError!");
}
System.out.println(age);
}
}
junit测试类应该这么写:
public class UserExceptionDemoTest {
@Test(expected = IllegalArgumentException.class)
public void testSetAge() {
UserExceptionDemo user = new UserExceptionDemo();
user.setAge(-1);
}
}