Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make it clear for java classes recursive comparison #3209

Open
wants to merge 7 commits into
base: 3.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -362,8 +362,16 @@ private static List<ComparisonDifference> determineDifferences(Object actual, Ob
continue;
}

if (shouldHonorEquals(dualValue, recursiveComparisonConfiguration)) {
if (!actualFieldValue.equals(expectedFieldValue)) comparisonState.addDifference(dualValue);
boolean shouldHonorJavaTypeEquals = dualValue.hasSomeJavaTypeValue() && !dualValue.isExpectedAContainer();
boolean shouldHonorOverriddenEquals = shouldHonorOverriddenEquals(dualValue, recursiveComparisonConfiguration);

if (shouldHonorJavaTypeEquals || shouldHonorOverriddenEquals) {
if (!actualFieldValue.equals(expectedFieldValue)) {
String description = shouldHonorJavaTypeEquals
? "Comparison objects are of Java types and were then compared with equals method"
: "Comparison objects were compared with equals method";
comparisonState.addDifference(dualValue, description);
}
continue;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,8 @@ void should_not_use_equal_implementation_of_root_objects_to_compare() {
// WHEN
compareRecursivelyFailsAsExpected(actual, expected);
// THEN
ComparisonDifference numberDifference = diff("home.address.number", actual.home.address.number, expected.home.address.number);
ComparisonDifference numberDifference = diff("home.address.number", actual.home.address.number, expected.home.address.number,
"Comparison objects are of Java types and were then compared with equals method");
verifyShouldBeEqualByComparingFieldByFieldRecursivelyCall(actual, expected, numberDifference);
}

Expand All @@ -138,7 +139,8 @@ void should_be_able_to_compare_objects_with_percentages() {
// WHEN
compareRecursivelyFailsAsExpected(actual, expected);
// THEN
ComparisonDifference nameDifference = diff("name", actual.name, expected.name);
ComparisonDifference nameDifference = diff("name", actual.name, expected.name,
"Comparison objects are of Java types and were then compared with equals method");
verifyShouldBeEqualByComparingFieldByFieldRecursivelyCall(actual, expected, nameDifference);
}

Expand All @@ -152,8 +154,10 @@ void should_fail_when_fields_of_different_nesting_levels_differ() {
// WHEN
compareRecursivelyFailsAsExpected(actual, expected);
// THEN
ComparisonDifference nameDifference = diff("name", actual.name, expected.name);
ComparisonDifference numberDifference = diff("home.address.number", actual.home.address.number, expected.home.address.number);
ComparisonDifference nameDifference = diff("name", actual.name, expected.name,
"Comparison objects are of Java types and were then compared with equals method");
ComparisonDifference numberDifference = diff("home.address.number", actual.home.address.number, expected.home.address.number,
"Comparison objects are of Java types and were then compared with equals method");
verifyShouldBeEqualByComparingFieldByFieldRecursivelyCall(actual, expected, numberDifference, nameDifference);
}

Expand Down Expand Up @@ -290,7 +294,8 @@ void should_report_difference_in_collection() {
compareRecursivelyFailsAsExpected(actual, expected);

// THEN
ComparisonDifference friendNumberDifference = diff("friends[0].home.address.number", 99, 10);
ComparisonDifference friendNumberDifference = diff("friends[0].home.address.number", 99, 10,
"Comparison objects are of Java types and were then compared with equals method");
verifyShouldBeEqualByComparingFieldByFieldRecursivelyCall(actual, expected, friendNumberDifference);
}

Expand Down Expand Up @@ -391,8 +396,10 @@ void should_not_handle_value_node_as_iterable() throws IOException {
// WHEN
compareRecursivelyFailsAsExpected(actual, expected);
// THEN
ComparisonDifference difference1 = diff("_children.importantValue._value", "10", "1");
ComparisonDifference difference2 = diff("_children.someNotImportantValue._value", 1, 10);
ComparisonDifference difference1 = diff("_children.importantValue._value", "10", "1",
"Comparison objects are of Java types and were then compared with equals method");
ComparisonDifference difference2 = diff("_children.someNotImportantValue._value", 1, 10,
"Comparison objects are of Java types and were then compared with equals method");
verifyShouldBeEqualByComparingFieldByFieldRecursivelyCall(actual, expected, difference1, difference2);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import java.util.OptionalInt;
import java.util.OptionalLong;
import java.util.stream.Stream;

import org.assertj.core.api.RecursiveComparisonAssert_isEqualTo_BaseTest;
import org.assertj.core.internal.objects.data.Home;
import org.assertj.core.internal.objects.data.Person;
Expand Down Expand Up @@ -123,16 +122,19 @@ void should_fail_when_actual_differs_from_expected_on_compared_fields_of_types(O
}

private static Stream<Arguments> failComparingTopLevelFields() {
return Stream.of(arguments(billie, john, types(String.class), "different name", array(diff("name", billie.name, john.name))),
String javaComparisonInformation = "Comparison objects are of Java types and were then compared with equals method";
return Stream.of(arguments(billie, john, types(String.class), "different name",
array(diff("name", billie.name, john.name, javaComparisonInformation))),
arguments(billie, anotherBillie, types(OptionalInt.class, OptionalDouble.class), "different age and weight",
array(diff("age", billie.age, anotherBillie.age),
diff("weight", billie.weight, anotherBillie.weight))),
array(diff("age", billie.age, anotherBillie.age, javaComparisonInformation),
diff("weight", billie.weight, anotherBillie.weight, javaComparisonInformation))),
arguments(john, jill, types(Person.class),
"different neighbour.name, neighbour.age and neighbour.home.address.number",
array(diff("neighbour.age", john.neighbour.age, jill.neighbour.age),
array(diff("neighbour.age", john.neighbour.age, jill.neighbour.age, javaComparisonInformation),
diff("neighbour.home.address.number", john.neighbour.home.address.number,
jill.neighbour.home.address.number),
diff("neighbour.name", john.neighbour.name, jill.neighbour.name))));
jill.neighbour.home.address.number, javaComparisonInformation),
diff("neighbour.name", john.neighbour.name, jill.neighbour.name,
javaComparisonInformation))));
}

@Test
Expand All @@ -151,8 +153,14 @@ void should_fail_when_combined_with_comparingOnlyFields() {
// WHEN
AssertionError assertionError = compareRecursivelyFailsAsExpected(billie, anotherBillie);
// THEN
ComparisonDifference ageDifference = diff("age", billie.age, anotherBillie.age);
ComparisonDifference weightDifference = diff("weight", billie.weight, anotherBillie.weight);
ComparisonDifference ageDifference = diff("age",
billie.age,
anotherBillie.age,
"Comparison objects are of Java types and were then compared with equals method");
ComparisonDifference weightDifference = diff("weight",
billie.weight,
anotherBillie.weight,
"Comparison objects are of Java types and were then compared with equals method");
verifyShouldBeEqualByComparingFieldByFieldRecursivelyCall(billie, anotherBillie, ageDifference, weightDifference);
}

Expand All @@ -173,7 +181,10 @@ void should_fail_when_combined_with_ignoringFields() {
// WHEN
compareRecursivelyFailsAsExpected(john, jill);
// THEN
ComparisonDifference idDifference = diff("name", john.name, jill.name);
ComparisonDifference idDifference = diff("name",
john.name,
jill.name,
"Comparison objects are of Java types and were then compared with equals method");
verifyShouldBeEqualByComparingFieldByFieldRecursivelyCall(john, jill, idDifference);
}

Expand All @@ -194,7 +205,10 @@ void should_fail_when_combined_with_ignoringFieldsMatchingRegexes() {
// WHEN
compareRecursivelyFailsAsExpected(john, jill);
// THEN
ComparisonDifference nameDifference = diff("name", john.name, jill.name);
ComparisonDifference nameDifference = diff("name",
john.name,
jill.name,
"Comparison objects are of Java types and were then compared with equals method");
verifyShouldBeEqualByComparingFieldByFieldRecursivelyCall(john, jill, nameDifference);
}

Expand All @@ -216,7 +230,10 @@ void should_fail_when_combined_with_ignoringFieldsOfTypes() {
// WHEN
compareRecursivelyFailsAsExpected(billie, anotherBillie);
// THEN
ComparisonDifference weightDifference = diff("weight", billie.weight, anotherBillie.weight);
ComparisonDifference weightDifference = diff("weight",
billie.weight,
anotherBillie.weight,
"Comparison objects are of Java types and were then compared with equals method");
verifyShouldBeEqualByComparingFieldByFieldRecursivelyCall(billie, anotherBillie, weightDifference);
}

Expand Down Expand Up @@ -244,7 +261,10 @@ void failing_javadoc_example_should_fail() {
// WHEN
compareRecursivelyFailsAsExpected(sherlock, moriarty);
// THEN
ComparisonDifference streetNumberDifference = diff("home.address.number", 221, 222);
ComparisonDifference streetNumberDifference = diff("home.address.number",
221,
222,
"Comparison objects are of Java types and were then compared with equals method");
verifyShouldBeEqualByComparingFieldByFieldRecursivelyCall(sherlock, moriarty, streetNumberDifference);

}
Expand All @@ -258,7 +278,10 @@ void should_compare_fields_of_specified_compared_types_even_if_parent_field_is_n
// WHEN
compareRecursivelyFailsAsExpected(lassie, snoopy);
// THEN
ComparisonDifference weightDifference = diff("breed.name", lassie.breed.name, snoopy.breed.name);
ComparisonDifference weightDifference = diff("breed.name",
lassie.breed.name,
snoopy.breed.name,
"Comparison objects are of Java types and were then compared with equals method");
verifyShouldBeEqualByComparingFieldByFieldRecursivelyCall(lassie, snoopy, weightDifference);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,11 +113,18 @@ void should_fail_when_actual_differs_from_expected_on_compared_fields() {
compareRecursivelyFailsAsExpected(actual, expected);

// THEN
ComparisonDifference dateOfBirthDifference = diff("dateOfBirth", actual.dateOfBirth, expected.dateOfBirth);
ComparisonDifference neighbourNameDifference = diff("neighbour.name", actual.neighbour.name, expected.neighbour.name);
ComparisonDifference dateOfBirthDifference = diff("dateOfBirth",
actual.dateOfBirth,
expected.dateOfBirth,
"Comparison objects are of Java types and were then compared with equals method");
ComparisonDifference neighbourNameDifference = diff("neighbour.name",
actual.neighbour.name,
expected.neighbour.name,
"Comparison objects are of Java types and were then compared with equals method");
ComparisonDifference numberDifference = diff("neighbour.neighbour.home.address.number",
actual.neighbour.neighbour.home.address.number,
expected.neighbour.neighbour.home.address.number);
expected.neighbour.neighbour.home.address.number,
"Comparison objects are of Java types and were then compared with equals method");
verifyShouldBeEqualByComparingFieldByFieldRecursivelyCall(actual, expected,
dateOfBirthDifference, neighbourNameDifference, numberDifference);
}
Expand Down Expand Up @@ -216,11 +223,17 @@ public void should_fail_when_actual_differs_from_expected_on_compared_fields_ind
recursiveComparisonConfiguration.compareOnlyFields("deleted");
// THEN
compareRecursivelyFailsAsExpected(staffWithLessFields, staff);
verifyShouldBeEqualByComparingFieldByFieldRecursivelyCall(staffWithLessFields, staff,
diff("deleted", staffWithLessFields.deleted, staff.deleted));
ComparisonDifference differenceWithFirst = diff("deleted",
staffWithLessFields.deleted,
staff.deleted,
"Comparison objects are of Java types and were then compared with equals method");
verifyShouldBeEqualByComparingFieldByFieldRecursivelyCall(staffWithLessFields, staff, differenceWithFirst);
compareRecursivelyFailsAsExpected(staff, staffWithLessFields);
verifyShouldBeEqualByComparingFieldByFieldRecursivelyCall(staff, staffWithLessFields,
diff("deleted", staff.deleted, staffWithLessFields.deleted));
ComparisonDifference differenceWithLast = diff("deleted",
staff.deleted,
staffWithLessFields.deleted,
"Comparison objects are of Java types and were then compared with equals method");
verifyShouldBeEqualByComparingFieldByFieldRecursivelyCall(staff, staffWithLessFields, differenceWithLast);
}

// https://github.com/assertj/assertj/issues/2610
Expand Down Expand Up @@ -249,7 +262,10 @@ public void should_fix_2610() {
// WHEN
compareRecursivelyFailsAsExpected(actual, expected);
// THEN
ComparisonDifference difference = diff("a", actual.a, expected.a);
ComparisonDifference difference = diff("a",
actual.a,
expected.a,
"Comparison objects are of Java types and were then compared with equals method");
verifyShouldBeEqualByComparingFieldByFieldRecursivelyCall(actual, expected, difference);
}

Expand All @@ -271,7 +287,10 @@ private static Stream<Arguments> should_fail_when_actual_is_a_container_whose_el
Student john2 = new Student("John", "math", 1);
Student rohit = new Student("Rohit", "english", 2);
Student rohyt = new Student("Rohyt", "english", 2);
ComparisonDifference difference = diff("[1].name", "Rohit", "Rohyt");
ComparisonDifference difference = diff("[1].name",
"Rohit",
"Rohyt",
"Comparison objects are of Java types and were then compared with equals method");
return Stream.of(arguments(list(john1, rohit), list(john2, rohyt), difference),
arguments(array(john1, rohit), array(john2, rohyt), difference),
arguments(set(john1, rohit), set(john2, rohyt), difference));
Expand Down Expand Up @@ -342,8 +361,14 @@ void should_fail_when_actual_differs_from_expected_lists_on_compared_fields() {
compareRecursivelyFailsAsExpected(actual, expected);

// THEN
ComparisonDifference difference1 = diff("[0].neighbour.neighbour.name", "John", "Jack");
ComparisonDifference difference2 = diff("[1].neighbour.neighbour.name", "Alice", "Joan");
ComparisonDifference difference1 = diff("[0].neighbour.neighbour.name",
"John",
"Jack",
"Comparison objects are of Java types and were then compared with equals method");
ComparisonDifference difference2 = diff("[1].neighbour.neighbour.name",
"Alice",
"Joan",
"Comparison objects are of Java types and were then compared with equals method");
verifyShouldBeEqualByComparingFieldByFieldRecursivelyCall(actual, expected, difference1, difference2);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,8 @@ void should_fail_when_actual_differs_from_expected_even_when_collection_order_is
compareRecursivelyFailsAsExpected(actual, expected);

// THEN
ComparisonDifference comparisonDifference = new ComparisonDifference(new DualValue(list("home.address.number"), 1, 2));
ComparisonDifference comparisonDifference = new ComparisonDifference(new DualValue(list("home.address.number"), 1, 2),
"Comparison objects are of Java types and were then compared with equals method");
verifyShouldBeEqualByComparingFieldByFieldRecursivelyCall(actual, expected, comparisonDifference);
}

Expand Down