我有一个处理异常的方法:测试 “处理的异常” 的JUnit
public boolean exampleMethod(){
try{
Integer temp=null;
temp.equals(null);
return
}catch(Exception e){
e.printStackTrace();
}
}
我想测试
public void test_exampleMethod(){}
我已经试过
@Rule
public ExpectedException expectedException=ExpectedException.none();
public void test_exampleMethod(){
expectedException.expect(JsonParseException.class);
exampleMethod();
}
但是,这并不因为异常是在里面处理的。
我也试过
@Test(expected=JsonParseException.class)
但同样的问题......在处理异常
我知道我可以做
assertTrue(if(exampleMethod()))
,但它仍然会打印出堆栈跟踪到日志。我宁愿干净的日志...任何建议?
你的例子并不完整,你需要在你的方法结束时返回声明 – AlexWien