SignGuestbookServlet extends HttpServlet { @Override public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException { Greeting greeting; UserService userService = UserServiceFactory.getUserService(); User user = userService.getCurrentUser(); String guestbookName = req.getParameter("guestbookName"); String content = req.getParameter("content"); if (user != null) { greeting = new Greeting(guestbookName, content, user.getUserId(), user.getEmail()); } else { greeting = new Greeting(guestbookName, content); } greeting.save(); resp.sendRedirect("/guestbook.jsp?guestbookName=" + guestbookName); }  @Override void doPost(HttpServletRequest req, HttpServletResponse resp);  }
@Test public void doPost_userNotLoggedIn() throws Exception { String testBook = "default"; when(mockRequest.getParameter("guestbookName")).thenReturn(testBook); String testGreeting = "beep!"; when(mockRequest.getParameter("content")).thenReturn(testGreeting); signGuestbookServlet.doPost(mockRequest, mockResponse); Guestbook guestbook = new Guestbook(testBook); List<Greeting> greetings = guestbook.getGreetings(); assertTrue(greetings.size() == 1); assertTrue(greetings.get(0).content.equals(testGreeting)); }