### Question:
AddContactController extends HttpServlet { @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { ContactCommandConverter converter = new ContactCommandConverter(); if(converter.verifyAndConvert(request)) { addressBook.addContact(converter.getCommand()); response.sendRedirect(successedSubmitRedirect); } else { request.setAttribute("errors", converter.getErrors()); request.getRequestDispatcher(formView).forward(request, response); } }  @Override void init(ServletConfig config); GlobalAddressBook getAddressBook(); void setAddressBook(GlobalAddressBook addressBook); String getSuccessedSubmitRedirect(); void setSuccessedSubmitRedirect(String successedSubmitRedirect); String getFormView(); void setFormView(String formView);  }### Answer:
@Test public void testDoPostAddContact() throws ServletException, IOException, ParseException { FakeHttpServletRequest fakeRequest = new FakeHttpServletRequest(); FakeHttpServletResponse fakeResponse = new FakeHttpServletResponse(); fakeRequest.setParameter("firstName", "Pedro"); fakeRequest.setParameter("surname", "Ballesteros"); fakeRequest.setParameter("birthday", "8/1/1974"); fakeRequest.setParameter("phone", "69696969"); controller.doPost(fakeRequest, fakeResponse); ArgumentCaptor<Contact> argument = ArgumentCaptor.forClass(Contact.class); verify(mockAddressbook).addContact(argument.capture()); Contact contact = argument.getValue(); assertEquals("Pedro", contact.getFirstName()); assertEquals("Ballesteros", contact.getSurname()); assertEquals(dateFormat.parse("8/1/1974"), contact.getBirthday()); assertEquals("69696969", contact.getPhone()); assertNull(fakeRequest.getAttribute("errors")); assertEquals("redirectPath", fakeResponse.getSendRedirectLocation()); }
@Test public void testDoPostNullParams() throws ServletException, IOException { FakeHttpServletRequest fakeRequest = new FakeHttpServletRequest(); FakeHttpServletResponse fakeResponse = new FakeHttpServletResponse(); controller.doPost(fakeRequest, fakeResponse); verify(mockAddressbook, never()).addContact(any(Contact.class)); Map<String, String> errors = (Map<String, String>)fakeRequest.getAttribute("errors"); assertTrue(errors.containsKey("firstName")); assertFalse(errors.containsKey("surname")); assertFalse(errors.containsKey("birthday")); assertFalse(errors.containsKey("phone")); assertEquals("formViewPath", fakeResponse.getForwardLocation()); }
@Test public void testDoPostOnlyName() throws ServletException, IOException { FakeHttpServletRequest fakeRequest = new FakeHttpServletRequest(); FakeHttpServletResponse fakeResponse = new FakeHttpServletResponse(); fakeRequest.setParameter("firstName", "Pedro"); controller.doPost(fakeRequest, fakeResponse); ArgumentCaptor<Contact> argument = ArgumentCaptor.forClass(Contact.class); verify(mockAddressbook).addContact(argument.capture()); Contact contact = argument.getValue(); assertEquals("Pedro", contact.getFirstName()); assertNull(contact.getSurname()); assertNull(contact.getBirthday()); assertNull(contact.getPhone()); assertNull(fakeRequest.getAttribute("errors")); assertEquals("redirectPath", fakeResponse.getSendRedirectLocation()); }