### Question:
EmailProvider implements NotificationProvider { @Override public void notifyCustomer(Notification notification) throws DispatchUnsupportedByUserException, DispatchException { notifyCustomerOldEmail(notification); MailParams mailParams = getMailParams(notification); if (mailParams == null) return; Email toEmail = mailParams.getToEmail(); if (!isEmailValid(toEmail)) { logger.warn("Notification [{}] for placeId [{}] for person [{}] had invalid toEmail [{}].", notification.getMessageKey(), notification.getPlaceId(), notification.getPersonId(), toEmail == null ? "toEmail is null" : toEmail.getEmail()); return; } sendEmail(mailParams); responder.handleHandOff(notification); } @Inject  EmailProvider(@Named("email.provider.apikey") String sendGridApiKey, PersonDAO personDao, PlaceDAO placeDao, AccountDAO accountDao, NotificationMessageRenderer messageRenderer, UpstreamNotificationResponder responder); @Override void notifyCustomer(Notification notification); Boolean isEmailValid(Email toEmail); Boolean isEmailValid(String email); void sendEmail(MailParams mailParams);  }### Answer:
@Test(expected = DispatchUnsupportedByUserException.class) public void shouldFailWithUnknownCustomer() throws Exception { Mockito.when(personDao.findById(Mockito.any())).thenReturn(null); uut.notifyCustomer(new NotificationBuilder().build()); }
@Test public void shouldFailWithNoEmail() throws Exception { Mockito.when(personDao.findById(Mockito.any())).thenReturn(person); Mockito.when(person.getEmail()).thenReturn(null); uut.notifyCustomer(notification); Mockito.verify(logger).warn(Mockito.anyString(), Mockito.anyObject(), Mockito.anyObject(), Mockito.anyObject(), Mockito.anyObject()); }
@Test public void shouldSendEmailWithFullName() throws DispatchException, DispatchUnsupportedByUserException, IOException { ArgumentCaptor<Request> mailRequestCaptor = ArgumentCaptor.forClass(Request.class); uut.notifyCustomer(notification); Mockito.verify(sendGrid).api(mailRequestCaptor.capture()); validateEmail(mailRequestCaptor.getValue(), expectedFullName, expectedEmailFromEmail, expectedEmailBody); }
@Test public void shouldSendEmailWithPartialName() throws DispatchException, DispatchUnsupportedByUserException, JsonParseException, JsonMappingException, IOException { Mockito.when(person.getFirstName()).thenReturn(""); ArgumentCaptor<Request> mailRequestCaptor = ArgumentCaptor.forClass(Request.class); uut.notifyCustomer(notification); Mockito.verify(sendGrid).api(mailRequestCaptor.capture()); validateEmail(mailRequestCaptor.getValue(), expectedEmailFromEmail, expectedEmailFromEmail, expectedEmailBody); }
@Test public void shouldSendEmailWithNoName() throws DispatchException, DispatchUnsupportedByUserException, JsonParseException, JsonMappingException, IOException { Mockito.when(person.getFirstName()).thenReturn(""); Mockito.when(person.getLastName()).thenReturn(null); ArgumentCaptor<Request> emailCaptor = ArgumentCaptor.forClass(Request.class); uut.notifyCustomer(notification); Mockito.verify(sendGrid).api(emailCaptor.capture()); validateEmail(emailCaptor.getValue(), expectedEmailFromEmail, expectedEmailFromEmail, expectedEmailBody); }
@Test(expected = DispatchException.class) public void shouldThrowExceptionIfSendGridThrowsException() throws DispatchException, DispatchUnsupportedByUserException, IOException { Mockito.when(sendGrid.api(Mockito.any())).thenThrow(new IOException(new Exception())); uut.notifyCustomer(notification); }