开发者社区 问答 正文

如何在Mockito中模拟方法调用时抛出异常?

如何在Mockito中模拟方法调用时抛出异常?

展开
收起
我是三好学生 2024-06-20 15:36:32 461 分享 版权
1 条回答
写回答
取消 提交回答
  • 你可以使用thenThrow(exceptionClass.class)方法来模拟方法调用时抛出异常。例如,要模拟contentService的deleteContent方法在调用时抛出NullPointerException,你可以写when(contentService.deleteContent(anyLong())).thenThrow(NullPointerException.class);。

    2024-06-20 15:46:41
    赞同 3 展开评论
问答地址: