### Question:
ProviderByPersonNameComparator implements Comparator<Provider>, Serializable { @Override public int compare(Provider provider1, Provider provider2) { boolean provider1IsNull = (provider1 == null || provider1.getPerson() == null); boolean provider2IsNull = (provider2 == null || provider2.getPerson() == null); if (provider1IsNull && provider2IsNull) { return 0; } else if (provider1IsNull) { return 1; } else if (provider2IsNull) { return -1; } return new PersonByNameComparator().compare(provider1.getPerson(), provider2.getPerson()); }  @Override int compare(Provider provider1, Provider provider2);  }### Answer:
@Test public void compareProvidersByPersonsName_shouldReturnNegativeIfPersonNameForProvider1ComesBeforeThatOfProvider2() { Person person1 = new Person(); person1.addName(new PersonName("givenName", "middleName", "familyName")); Provider provider1 = new Provider(); provider1.setPerson(person1); Person person2 = new Person(); person2.addName(new PersonName("givenName", "middleNamf", "familyName")); Provider provider2 = new Provider(); provider2.setPerson(person2); int actualValue = new ProviderByPersonNameComparator().compare(provider1, provider2); Assert.assertTrue("Expected a negative value but it was: " + actualValue, actualValue < 0); }
@Test public void compareProvidersByPersonName_shouldReturnPositiveIfPersonNameForProvider1ComesAfterThatOfProvider2() { Person person1 = new Person(); person1.addName(new PersonName("givenNamf", "middleName", "familyName")); Provider provider1 = new Provider(); provider1.setPerson(person1); Person person2 = new Person(); person2.addName(new PersonName("givenName", "middleName", "familyName")); Provider provider2 = new Provider(); provider2.setPerson(person2); int actualValue = new ProviderByPersonNameComparator().compare(provider1, provider2); Assert.assertTrue("Expected a positive value but it was: " + actualValue, actualValue > 0); }
@Test public void compareProvidersByPersonName_shouldReturnZeroIfTheGivenNameMiddleNameAndFamilyNameMatch() { Person person1 = new Person(); person1.addName(new PersonName("givenName", "middleName", "familyName")); Provider provider1 = new Provider(); provider1.setPerson(person1); Person person2 = new Person(); person2.addName(new PersonName("givenName", "middleName", "familyName")); Provider provider2 = new Provider(); provider2.setPerson(person2); int actualValue = new ProviderByPersonNameComparator().compare(provider1, provider2); Assert.assertTrue("Expected zero but it was: " + actualValue, actualValue == 0); }
@Test public void compareProvidersByPersonName_shouldNotFailIfProvider1HasNoAssociatedPerson() { Provider provider1 = new Provider(); Person person2 = new Person(); person2.addName(new PersonName("givenName", "middleName", "familyName")); Provider provider2 = new Provider(); provider2.setPerson(person2); int actualValue = new ProviderByPersonNameComparator().compare(provider1, provider2); Assert.assertTrue("Expected a positive value but it was: " + actualValue, actualValue > 0); }
@Test public void compareProvidersByPersonName_shouldNotFailIfProvider2HasNoAssociatedPerson() { Person person1 = new Person(); person1.addName(new PersonName("givenName", "middleName", "familyName")); Provider provider1 = new Provider(); provider1.setPerson(person1); Provider provider2 = new Provider(); int actualValue = new ProviderByPersonNameComparator().compare(provider1, provider2); Assert.assertTrue("Expected a negative value but it was: " + actualValue, actualValue < 0); }
@Test public void compareProvidersByPersonName_shouldNotFailIfNeitherProviderHasAnAssociatedPerson() { Provider provider1 = new Provider(); Provider provider2 = new Provider(); int actualValue = new ProviderByPersonNameComparator().compare(provider1, provider2); Assert.assertTrue("Expected zero but it was: " + actualValue, actualValue == 0); }