@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)); }
@Test public void testDoPost() throws IOException, EntityNotFoundException { HttpServletRequest request = mock(HttpServletRequest.class); HttpServletResponse response = mock(HttpServletResponse.class); String GuestBookName = "TestGuestbook"; String testContent = "Test Content"; when(request.getParameter("GuestBookName")).thenReturn(GuestBookName); when(request.getParameter("content")).thenReturn(testContent); Date priorToRequest = new Date(); signGuestbookServlet.doPost(request, response); Date afterRequest = new Date(); verify(response).sendRedirect("/guestbook.jsp?GuestBookName=TestGuestbook"); User currentUser = UserServiceFactory.getUserService().getCurrentUser(); Entity greeting = DatastoreServiceFactory.getDatastoreService().prepare(new Query()).asSingleEntity(); assertEquals(GuestBookName, greeting.getKey().getParent().getName()); assertEquals(testContent, greeting.getProperty("content")); assertEquals(currentUser, greeting.getProperty("user")); 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)); }