I want to test my servlet with mockito. I also want to know what a server is. So, if the servlet writes something like this:
HttpServletResponse.getWriter().println("xyz");
I want to write it to a text file. I created a layout for the HttpServletResponse and told Mockito that it should return my custom PrintWriter if HttpServletResponse.getWriter () is called:
HttpServletResponse resp = mock(HttpServletResponse.class); PrintWriter writer = new PrintWriter("somefile.txt"); when(resp.getWriter()).thenReturn(writer);
A text file is created, but it is empty. How can I make this work?
Edit:
@Jonathan: Actually it's true, mocking a writer, this is a much cleaner solution. Decided so.
StringWriter sw = new StringWriter(); PrintWriter pw =new PrintWriter(sw); when(resp.getWriter()).thenReturn(pw);
Then I can just check the contents of StringWriter and not deal with files at all.
source share