views:

96

answers:

2

Can I use Mockito to capture what was passed to the HttpServletResponse#sendError() method? I can't figure out how to do that.

A: 

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.

Andy
In this case I don't have a real object. I'm doing: mock(HttpServletResponse.class). In the code it calls the sendError() method. Maybe I just need to create my own mock implementation fo HttpServletResponse. I was hoping Mockito had a was to save any time sendError() was called so I could retrieve the results later on.
Dave
Looking at the Spy docs showed me how simple this is: verify(res).sendError(404);Thanks.
Dave
No problem. I have been wrestling with Mockito myself lately. It's amazing, except when it isn't.
Andy
+1  A: 

You should use the verify method on Mockito to do this. Usually mocking HttpResponse isn't a pleasant experience though.

verify(mockResponse, times(1)).sendError(..)
iwein