[英]Mockito: How to verify a specific exception was thrown from catching another exception
例子:
public T f1() {
try{
Object o = new Object();
o.someFunc() // i want to mock this function call to throw an exception (ex) Exception1)
}
catch (Exception1 e) {
throw new Exception2()
}
我将如何在 mockito 中执行此操作并在强制抛出异常 1 后验证我是否获得了 Exception2?
您可以使用when
和thenThrow
来·默克Exeption1
从someFunc
when(someService.someFunction("param1", "param12")).thenThrow( new Exception1());
然后f1()
应该捕获异常并抛出Exception2
您可以断言异常类名称如下,
assertEquals(mvcResult.getResolvedException().getClass().getName(), "org.springframework.web.bind.MissingServletRequestParameterException");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.