User { public void addAccessInterval(AccessInterval interval) throws IllegalArgumentException { Objects.requireNonNull(interval); if(!this.equals(interval.getUser())) { throw new IllegalArgumentException("Interval instance belongs to a different or no user!"); } accessIntervals.add(interval); } 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 addAccessIntervalAddsIntervalWhenItBelongsToUser() { User user = new User("user"); given(accessIntervalMock.getUser()).willReturn(user); user.addAccessInterval(accessIntervalMock); assertThat(user.accessIntervals).containsExactly(accessIntervalMock); }
@Test public void addAccessIntervalThrowsExceptionWhenNotBelongingToUser() { User user = new User("user"); User otherUser = new User("other-user"); given(accessIntervalMock.getUser()).willReturn(otherUser); assertThatThrownBy(() -> user.addAccessInterval(accessIntervalMock)) .isInstanceOf(IllegalArgumentException.class) .hasMessage("Interval instance belongs to a different or no user!"); }