【发布时间】:2017-08-28 00:02:22
【问题描述】:
我想测试异常的返回码。这是我的生产代码:
class A {
try {
something...
}
catch (Exception e)
{
throw new MyExceptionClass(INTERNAL_ERROR_CODE, e);
}
}
以及对应的异常:
class MyExceptionClass extends ... {
private errorCode;
public MyExceptionClass(int errorCode){
this.errorCode = errorCode;
}
public getErrorCode(){
return this.errorCode;
}
}
我的单元测试:
public class AUnitTests{
@Rule
public ExpectedException thrown= ExpectedException.none();
@Test (expected = MyExceptionClass.class,
public void whenRunningSomething_shouldThrowMyExceptionWithInternalErrorCode() throws Exception {
thrown.expect(MyExceptionClass.class);
??? expected return code INTERNAL_ERROR_CODE ???
something();
}
}
【问题讨论】:
-
我正在寻找一个很好的方法来做到这一点。 Try/catch 没问题,但这意味着更多的代码行。在我看来,这很难读......
-
请检查我的回答,希望这种方法对你有帮助
标签: java junit junit4 junit-rule