Reputation: 14178
Can I use Mockito to capture what was passed to the HttpServletResponse#sendError()
method? I can't figure out how to do that.
Upvotes: 2
Views: 3682
Reputation: 2724
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());
Upvotes: 3
Reputation: 26161
You should use the verify method on Mockito to do this. Usually mocking HttpResponse isn't a pleasant experience though.
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.
Upvotes: 7
Reputation: 14184
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.
Upvotes: 0