Mockito:捕获 HttpServletResponse#sendError()
我可以使用 Mockito 捕获传递给 HttpServletResponse#sendError()
方法的内容吗?我不知道该怎么做。
Can I use Mockito to capture what was passed to the HttpServletResponse#sendError()
method? I can't figure out how to do that.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您应该使用 Mockito 上的 verify 方法来执行此操作。通常,嘲笑 HttpResponse 并不是一种愉快的体验。
作为
sendError
的参数,您可以传递mockito匹配器,它可以对您需要的参数进行任何检查。You should use the verify method on Mockito to do this. Usually mocking HttpResponse isn't a pleasant experience though.
As arguments to
sendError
you can then pass mockito matchers, which can do any check on the argument that you need.我认为发帖者想知道如何检索传递给该方法的参数。您可以使用:
I think the poster wanted to know, how to retrieve the arguments that were passed to the method. You can use:
您可能需要查看Mockito 间谍 (第 13 章)。对于无法模拟的对象,有时可以检查其内部结构并以这种方式存根某些方法。
如果您可以发布一些示例代码,我可以看一下。
You might want to look at Mockito spies (chapter 13). For objects you can't mock, you can sometimes examine their internals and stub certain methods this way.
If you can post some sample code, I can take a look at it.