UserComparator implements Comparator<User> { public int compare(User o1, User o2) { if (firstNameFirst) { return new CompareToBuilder().append(o1.getFirstName(), o2.getFirstName()) .append(o1.getLastName(), o2.getLastName()) .toComparison(); } else { return new CompareToBuilder().append(o1.getLastName(), o2.getLastName()) .append(o1.getFirstName(), o2.getFirstName()) .toComparison(); } } UserComparator(boolean firstNameFirst); int compare(User o1, User o2);  }
@Test public void should_accept_null_for_first_name() { User user1 = UserObjectMother.createUser(); User user2 = UserObjectMother.createUser(); user1.setFirstName(null); user2.setFirstName(null); assertEquals(0, new UserComparator(true).compare(user1, user2)); }