SignGuestbookServlet extends HttpServlet { @Override public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException { final UserService userService = UserServiceFactory.getUserService(); final User user = userService.getCurrentUser(); final String guestbookName = req.getParameter("guestbookName"); final Key guestbookKey = KeyFactory.createKey("Guestbook", guestbookName); final String content = req.getParameter("content"); final Date date = new Date(); final Entity greeting = new Entity("Greeting", guestbookKey); greeting.setProperty("user", user); greeting.setProperty("date", date); greeting.setProperty("content", content); final DatastoreService datastore = DatastoreServiceFactory.getDatastoreService(); datastore.put(greeting); resp.sendRedirect("/guestbook.jsp?guestbookName=" + guestbookName); }  @Override void doPost(HttpServletRequest req, HttpServletResponse resp);  }
@Test public void testDoPost() throws IOException, EntityNotFoundException { final HttpServletRequest request = mock(HttpServletRequest.class); final HttpServletResponse response = mock(HttpServletResponse.class); final String guestbookName = "TestGuestbook"; final String testContent = "Test Content"; when(request.getParameter("guestbookName")).thenReturn(guestbookName); when(request.getParameter("content")).thenReturn(testContent); final Date priorToRequest = new Date(); signGuestbookServlet.doPost(request, response); final Date afterRequest = new Date(); verify(response).sendRedirect("/guestbook.jsp?guestbookName=TestGuestbook"); final User currentUser = UserServiceFactory.getUserService().getCurrentUser(); final Entity greeting = DatastoreServiceFactory.getDatastoreService().prepare(new Query()).asSingleEntity(); assertEquals(guestbookName, greeting.getKey().getParent().getName()); assertEquals(testContent, greeting.getProperty("content")); assertEquals(currentUser, greeting.getProperty("user")); final Date date = (Date) greeting.getProperty("date"); assertTrue("The date in the entity [" + date + "] is prior to the request being performed", priorToRequest.before(date) || priorToRequest.equals(date)); assertTrue("The date in the entity [" + date + "] is after to the request completed", afterRequest.after(date) || afterRequest.equals(date)); }