简体   繁体   English

Mockito:捕获HttpServletResponse#sendError()

[英]Mockito: capturing HttpServletResponse#sendError()

Can I use Mockito to capture what was passed to the HttpServletResponse#sendError() method? 我可以使用Mockito捕获传递给HttpServletResponse#sendError()方法的内容吗? I can't figure out how to do that. 我不知道该怎么做。

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.

相关问题 HttpServletResponse#sendError()是否会抛出IOException吗? - Does HttpServletResponse#sendError() ever throw an IOException? Spring异常处理 - @ControllerAdvice无法处理HttpServletResponse#sendError() - Spring Exception Handling - @ControllerAdvice cannot handle HttpServletResponse#sendError() 重写HttpServletResponse sendError或setStatus - Override HttpServletResponse sendError or setStatus 在HttpServletResponse.sendError之后的过滤器中需要返回 - Return Needed in Filter After HttpServletResponse.sendError HttpServletResponse.sendError()不会重定向到错误页面 - HttpServletResponse.sendError() does not redirect to error page 我如何从HttpServletResponse.sendError检索味精 - how can i retrieve the msg from HttpServletResponse.sendError JBoss如何实现HttpServletResponse setStatus()与sendError() - How does JBoss implement HttpServletResponse setStatus() vs sendError() 如何将消息从 HttpServletResponse.sendError(status, msg) 传递给控制器​​? - How to pass the msg from HttpServletResponse.sendError(status, msg) to controller? HttpServletResponse .sendError 在缺少 CSRF 令牌时返回空响应正文 - HttpServletResponse .sendError returns empty response body when missing CSRF token Mockito和HttpServletResponse-将输出写入文本文件 - Mockito and HttpServletResponse - write output to textfile
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM