UsersServlet extends HttpServlet { @Override public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException { UserService userService = UserServiceFactory.getUserService(); String thisUrl = req.getRequestURI(); resp.setContentType("text/html"); if (req.getUserPrincipal() != null) { resp.getWriter() .println( "<p>Hello, " + req.getUserPrincipal().getName() + "! You can <a href=\"" + userService.createLogoutURL(thisUrl) + "\">sign out</a>.</p>"); } else { resp.getWriter() .println( "<p>Please <a href=\"" + userService.createLoginURL(thisUrl) + "\">sign in</a>.</p>"); } }  @Override void doGet(HttpServletRequest req, HttpServletResponse resp);  }
@Test public void doGet_userNotLoggedIn_writesResponse() throws Exception { servletUnderTest.doGet(mockRequestNotLoggedIn, mockResponse); assertWithMessage("UsersServlet response") .that(responseWriter.toString()) .contains("<p>Please <a href="); assertWithMessage("UsersServlet response") .that(responseWriter.toString()) .contains("sign in</a>.</p>"); }
@Test public void doGet_userLoggedIn_writesResponse() throws Exception { servletUnderTest.doGet(mockRequestLoggedIn, mockResponse); assertWithMessage("UsersServlet response") .that(responseWriter.toString()) .contains("<p>Hello, " + FAKE_NAME + "!"); assertWithMessage("UsersServlet response").that(responseWriter.toString()).contains("sign out"); }