Last active
November 2, 2015 05:45
-
-
Save shangoyanyi/1ee7f7bb8491e74e442b to your computer and use it in GitHub Desktop.
Mockito - A Simple Servlet Tester
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* this work's fine on mockito 1.9.5 | |
*/ | |
@Test | |
public void testServlet() throws Exception { | |
HttpServletRequest request = mock(HttpServletRequest.class); | |
HttpServletResponse response = mock(HttpServletResponse.class); | |
when(request.getParameter("username")).thenReturn("me"); | |
when(request.getParameter("password")).thenReturn("secret"); | |
PrintWriter writer = new PrintWriter("somefile.txt"); | |
when(response.getWriter()).thenReturn(writer); | |
new MyServlet().doPost(request, response); | |
verify(request, atLeast(1)).getParameter("username"); // only if you want to verify username was called... | |
writer.flush(); // it may not have been flushed yet... | |
assertTrue(FileUtils.readFileToString(new File("somefile.txt"), "UTF-8").contains("My Expected String")); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Github
mockito on Github
Older Version
download mockito-all-1.9.5.jar on google code