【发布时间】:2016-04-13 12:34:04
【问题描述】:
我无法运行我的测试,因为测试在此语句decorator.decorate(new EncoderColumnDecorator()) 中出现红色波浪形错误行,要求我使用 try/catch 或添加 throws。
这是错误信息。
当我已经有一个“预期”属性时,为什么我必须放置 try/catch 或 throws 异常
我的单元测试:
@Test(expected=DecoratorException.class)
public void testDecorate_exception() {
decorator.decorate(new EncoderColumnDecorator()); -----Error in this line
}
待测方法
@Override
public String decorate(Object arg0) throws DecoratorException {
try{
//some code
}
}catch(Exception e){
throw new DecoratorException();
}
return arg0;
}
}
【问题讨论】:
-
该方法抛出异常,要求任何使用它的方法也抛出/尝试...捕获它。这包括 JUnit 方法。
标签: java unit-testing exception junit junit4