User { public void addApplication(ProjectApplication application) throws IllegalArgumentException { if(!this.equals(application.getUser())) { throw new IllegalArgumentException("The application belongs to another user!"); } applications.add(application); } User(String userId); protected  User(); void addBookmark(Project project); void removeBookmark(Project project); void addApplication(ProjectApplication application); void removeApplication(ProjectApplication application); Optional<AccessInterval> getLatestAccessInterval(); void addAccessInterval(AccessInterval interval); void removeAccessInterval(AccessInterval interval);  }
@Test public void addApplicationBelongsToUser() { User user = new User("user"); given(applicationMock.getUser()).willReturn(user); user.addApplication(applicationMock); assertThat(user.applications).containsExactly(applicationMock); }
@Test public void addApplicationThrowsExceptionWhenNotBelongingToUser() { User user = new User("user"); User wrongUser = new User("other-user"); given(applicationMock.getUser()).willReturn(wrongUser); assertThatThrownBy(() -> user.addApplication(applicationMock)) .isInstanceOf(IllegalArgumentException.class) .hasMessage("The application belongs to another user!"); }