【发布时间】:2018-02-02 13:42:59
【问题描述】:
我有这个方法:
public int addInt(int x, int y){
try{
if(x<1 || y<1){
throw new InvalidValueExeption();
}
} catch(InvalidValueExeption i){
System.out.println(i);
}
return x+y;
}
InvalidValueExeption 是一个自定义异常。所以我想测试一下:
@Test
public void test(){
AddClass a = new AddClass();
boolean thrown = false;
try{
a.addInt(-2, 3);
} catch(InvalidValueException e){
thrown=true;
}
assertTrue(thrown);
}
我无法运行此测试,因为它显示 Exception exception.InvalidValueException is never thrown in the corresponding try block。
我做错了什么?
【问题讨论】:
-
如果你捕捉到这个异常,
addInt方法如何抛出InvalidValueException异常?
标签: java exception testing junit