java – 使用Mockito处理异常
发布时间:2020-12-14 05:49:09 所属栏目:Java 来源:网络整理
导读:我在单元测试中使用Mockito.我有一个方法 public Status getResponse(Request requset) throws DataException{} DataException是我自己定义的一个继承自Exception类的. 在我的测试用例中 static{when(process.getResponse(any(Request.class))). thenReturn(
我在单元测试中使用Mockito.我有一个方法
public Status getResponse(Request requset) throws DataException{ } DataException是我自己定义的一个继承自Exception类的. 在我的测试用例中 static{ when(process.getResponse(any(Request.class))). thenReturn(new Status("Success")); } 它给出了一个错误,Unhandled Exception:DataException Mockito有没有办法在不添加try / catch的情况下处理这个问题? 解决方法
将此添加到您的测试方法:
@Test(expected=DataException.class) 或使用此: then(caughtException()).isInstanceOf(DataException.class); 对于静态块,除了try-catch之外别无他法. 一种方法是将DataException更改为RuntimeException. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |