### Question:
DbAddressBook implements GlobalAddressBook { @Override public String addContact(Contact contact) { if (contact.getFirstName() == null || contact.getFirstName().trim().length() < 1) { throw new InvalidContactException(); } contact.setFirstName(contact.getFirstName().trim()); if (contact.getSurname() != null) { contact.setSurname(contact.getSurname().trim()); } if (checkDuplicate(contact)) { throw new InvalidContactException(); } return addressBookDao.addContact(contact); }  @Override String addContact(Contact contact); @Override Contact getContact(String contactId); AddressBookDao getAddressBookDao(); void setAddressBookDao(AddressBookDao addressBookDao); @Override List<Contact> getAll(); @Override void deleteContact(String id);  }### Answer:
@Test public void testAddSimpleContact() { expectedContact.setFirstName("Pedro"); when(mockAddressBookDao.addContact(any(Contact.class))).thenReturn("expectedId"); String contactId = addressBook.addContact(expectedContact); assertEquals("expectedId", contactId); }
@Test public void testAddFullContact() throws ParseException { expectedContact.setFirstName("Pedro"); expectedContact.setSurname("Ballesteros"); Date expectedBirthday = dateFormat.parse("8/1/1974"); expectedContact.setBirthday(expectedBirthday); expectedContact.setPhone("610101010"); when(mockAddressBookDao.addContact(any(Contact.class))).thenReturn("fullContactId"); String id = addressBook.addContact(expectedContact); assertEquals("fullContactId", id); ArgumentCaptor<Contact> argument = ArgumentCaptor.forClass(Contact.class); verify(mockAddressBookDao).addContact(argument.capture()); assertEquals("Pedro", argument.getValue().getFirstName()); assertEquals("Ballesteros", argument.getValue().getSurname()); assertEquals(expectedBirthday, argument.getValue().getBirthday()); assertEquals("610101010", argument.getValue().getPhone()); verify(mockAddressBookDao).addContact(eq(expectedContact)); verify(mockAddressBookDao).addContact(expectedContact); }
@Test public void testAddWithoutFirstName() { expectedContact.setSurname("Ballesteros"); try { addressBook.addContact(expectedContact); fail("InvalidContactException expected"); } catch(InvalidContactException ex) { verify(mockAddressBookDao, never()).addContact(any(Contact.class)); } }
@Test public void testAddBlankFirstName() { expectedContact.setFirstName(" "); expectedContact.setSurname("Ballesteros"); try { addressBook.addContact(expectedContact); fail("InvalidContactException expected"); } catch(InvalidContactException ex) { verify(mockAddressBookDao, never()).addContact(any(Contact.class)); } }
@Test public void testAddEmptyFirstName() { expectedContact.setFirstName(""); expectedContact.setSurname("Ballesteros"); try { addressBook.addContact(expectedContact); fail("InvalidContactException expected"); } catch(InvalidContactException ex) { verify(mockAddressBookDao, never()).addContact(any(Contact.class)); } }