[英]Mockito: capturing HttpServletResponse#sendError()
You should use the verify method on Mockito to do this. 您应该在Mockito上使用verify方法来执行此操作。 Usually mocking HttpResponse isn't a pleasant experience though. 通常,嘲笑HttpResponse并不是一种愉快的体验。
mockResponse = mock(HttpSR→);
//…
verify(mockResponse, times(1)).sendError(..);
As arguments to sendError
you can then pass mockito matchers, which can do any check on the argument that you need. 然后,作为sendError
参数,您可以传递模仿匹配器,该匹配器可以对所需的参数进行任何检查。
I think the poster wanted to know, how to retrieve the arguments that were passed to the method. 我认为发布者想知道,如何检索传递给该方法的参数。 You can use: 您可以使用:
// given
HttpServletResponse response = mock(HttpServletResponse.class);
ArgumentCaptor<Integer> intArg = ArgumentCaptor.forClass(Integer.class);
ArgumentCaptor<String> stringArg = ArgumentCaptor.forClass(String.class);
doNothing().when(response).sendError(intArg.capture(), stringArg.capture());
// when (do your test here)
response.sendError(404, "Not found");
// then (do your assertions here, I just print out the values)
System.out.println(intArg.getValue());
System.err.println(stringArg.getValue());
You might want to look at Mockito spies (chapter 13). 您可能需要查看Mockito间谍 (第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. 如果您可以发布一些示例代码,我可以看一下。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.