I have an HttpServlet that I’m trying to write tests for and have mocked the ServletOutputStream: ServletOutputStream outputStream = mock(ServletOutputStream.class);. I need to get what’s written to outputStream in the servlet that used println. ie, in the servlet, I have code like:
ServletOutputStream out = response.getOutpuStream();
out.println("foo");
So, I would want to test for “foo.” I’ve tried to say when(outputStream.println()).then..., but this doesn’t work since println() is a void method. I’ve also investigated HttpServletResponseWrappers, but have been unable to find a solution there. Ideas? I’m using Mockito.
If you want to see if “foo” is passed to the println method you could do: