ProjectApplication { @Override public boolean equals(Object obj) { if(this == obj) { return true; } if(obj instanceof ProjectApplication) { ProjectApplication other = (ProjectApplication) obj; boolean userEquals = Objects.nonNull(this.user) && Objects.nonNull(other.user) && Objects.equals(this.user.getId(), other.user.getId()); return userEquals && Objects.equals(this.id, other.id) && Objects.equals(this.comment, other.comment) && Objects.equals(this.project, other.project) && Objects.equals(this.applicationDate, other.applicationDate); } return false; } ProjectApplication(Project project, String comment, User user, LocalDateTime applicationDate); @Override boolean equals(Object obj); @Override int hashCode();  }
@Test public void equalsReturnsTrueForSameInstance() { long applicationId = 1; var comment = "This is a comment!"; var date = LocalDateTime.now(); var application = new ProjectApplication(projectMock, comment, userMock, date); application.id = applicationId; boolean actualEquals = application.equals(application); assertThat(actualEquals).isTrue(); }
@Test public void equalsReturnsFalseForDifferentType() { var comment = "This is a comment!"; var date = LocalDateTime.now(); var application = new ProjectApplication(projectMock, comment, userMock, date); boolean actualEquals = application.equals("Test"); assertThat(actualEquals).isFalse(); }
@Test public void equalsReturnsFalseForDifferentId() { long applicationId = 1; long otherApplicationId = 2; var userId = "user"; var comment = "This is a comment!"; var date = LocalDateTime.now(); given(userMock.getId()).willReturn(userId); var application = new ProjectApplication(projectMock, comment, userMock, date); application.id = applicationId; var otherApplication = new ProjectApplication(projectMock, comment, userMock, date); otherApplication.id = otherApplicationId; boolean actualEquals = application.equals(otherApplication); assertThat(actualEquals).isFalse(); }
@Test public void equalsReturnsFalseForDifferentProject() { long applicationId = 1; var userId = "user"; var comment = "This is a comment!"; var date = LocalDateTime.now(); given(userMock.getId()).willReturn(userId); var application = new ProjectApplication(projectMock, comment, userMock, date); application.id = applicationId; var otherApplication = new ProjectApplication(otherProjectMock, comment, userMock, date); otherApplication.id = applicationId; boolean actualEquals = application.equals(otherApplication); assertThat(actualEquals).isFalse(); }
@Test public void equalsReturnsFalseForDifferentComment() { long applicationId = 1; var userId = "user"; var comment = "This is a comment!"; var otherComment = "This is a different comment!"; var date = LocalDateTime.now(); given(userMock.getId()).willReturn(userId); var application = new ProjectApplication(projectMock, comment, userMock, date); application.id = applicationId; var otherApplication = new ProjectApplication(projectMock, otherComment, userMock, date); otherApplication.id = applicationId; boolean actualEquals = application.equals(otherApplication); assertThat(actualEquals).isFalse(); }
@Test public void equalsReturnsFalseForDifferentDate() { long applicationId = 1; var userId = "user"; var comment = "This is a comment!"; var date = LocalDateTime.now(); var otherDate = date.minus(1L, ChronoUnit.MINUTES); given(userMock.getId()).willReturn(userId); var application = new ProjectApplication(projectMock, comment, userMock, date); application.id = applicationId; var otherApplication = new ProjectApplication(projectMock, comment, userMock, otherDate); otherApplication.id = applicationId; boolean actualEquals = application.equals(otherApplication); assertThat(actualEquals).isFalse(); }
@Test public void equalsReturnsFalseForDifferentUserIds() { long applicationId = 1; var userId = "user"; var otherUserId = "other-user"; var comment = "This is a comment!"; var date = LocalDateTime.now(); given(userMock.getId()).willReturn(userId); given(otherUserMock.getId()).willReturn(otherUserId); var application = new ProjectApplication(projectMock, comment, userMock, date); application.id = applicationId; var otherApplication = new ProjectApplication(projectMock, comment, otherUserMock, date); otherApplication.id = applicationId; boolean actualEquals = application.equals(otherApplication); assertThat(actualEquals).isFalse(); }