SortedUserList implements UserList { @Override public boolean add(final User user) { Validate.notNull(user, "User can not be null"); final boolean success = userList.add(user); if (success) { Collections.sort(userList); fireUserAdded(userList.indexOf(user), user); } return success; } SortedUserList(); @Override boolean add(final User user); @Nullable @Override User get(final int pos); @Override int indexOf(final User user); @Override boolean remove(final User user); @Override User set(final int pos, final User user); @Override int size(); @Override void addUserListListener(final UserListListener listener); @Override void removeUserListListener(final UserListListener listener); List<UserListListener> getListeners();  }
@Test public void addShouldThrowExceptionIfUserIsNull() { expectedException.expect(IllegalArgumentException.class); expectedException.expectMessage("User can not be null"); userList.add(null); }
@Test public void addShouldNotifyListenersAboutPosition() { userList.add(annie); userList.add(john); userList.add(lenny); userList.add(niles); verify(listener).userAdded(0, annie); verify(listener).userAdded(1, john); verify(listener).userAdded(2, lenny); verify(listener).userAdded(3, niles); }
@Test public void addShouldNotifyListenersAboutPositionAfterSort() { userList.add(niles); userList.add(lenny); userList.add(john); userList.add(annie); verify(listener).userAdded(0, niles); verify(listener).userAdded(0, lenny); verify(listener).userAdded(0, john); verify(listener).userAdded(0, annie); }