Search in sources :

Example 26 with Person

use of org.assertj.core.internal.objects.data.Person in project assertj-core by joel-costigliola.

the class RecursiveComparisonConfiguration_getActualNonIgnoreFields_Test method should_compute_ignored_fields.

@Test
void should_compute_ignored_fields() {
    // GIVEN
    recursiveComparisonConfiguration.ignoreFieldsMatchingRegexes(".*umber");
    recursiveComparisonConfiguration.ignoreFields("people.name");
    recursiveComparisonConfiguration.ignoreFieldsOfTypes(Date.class);
    Person person1 = new Person("John");
    person1.home.address.number = 1;
    person1.dateOfBirth = new Date(123);
    person1.neighbour = new Person("Jack");
    person1.neighbour.home.address.number = 123;
    Person person2 = new Person("John");
    person2.home.address.number = 1;
    person2.dateOfBirth = new Date(123);
    person2.neighbour = new Person("Jim");
    person2.neighbour.home.address.number = 456;
    DualValue dualValue = new DualValue(list("people"), person1, person2);
    // WHEN
    Set<String> fields = recursiveComparisonConfiguration.getNonIgnoredActualFieldNames(dualValue);
    // THEN
    assertThat(fields).doesNotContain("number", "dateOfBirth", "name");
}
Also used : Person(org.assertj.core.internal.objects.data.Person) Date(java.util.Date) Test(org.junit.jupiter.api.Test)

Example 27 with Person

use of org.assertj.core.internal.objects.data.Person in project assertj-core by joel-costigliola.

the class RecursiveComparisonAssert_isEqualTo_withFieldComparators_Test method should_fail_when_actual_differs_from_expected_when_using_field_bipredicate_comparators.

@Test
void should_fail_when_actual_differs_from_expected_when_using_field_bipredicate_comparators() {
    // GIVEN
    Person actual = new Person("John");
    actual.home.address.number = 1;
    actual.dateOfBirth = new Date(123);
    actual.neighbour = new Person("Jack");
    actual.neighbour.home.address.number = 123;
    // actually a clone of actual
    Person expected = new Person("John");
    expected.home.address.number = 1;
    expected.dateOfBirth = new Date(123);
    expected.neighbour = new Person("Jack");
    expected.neighbour.home.address.number = 123;
    // register some equals methods for some fields that will fail the comparison
    recursiveComparisonConfiguration.registerEqualsForFields((Object o1, Object o2) -> false, "dateOfBirth", "neighbour.home.address");
    // WHEN
    compareRecursivelyFailsAsExpected(actual, expected);
    // THEN
    ComparisonDifference dateOfBirthDifference = diff("dateOfBirth", actual.dateOfBirth, expected.dateOfBirth);
    ComparisonDifference neighbourAddressDifference = diff("neighbour.home.address", actual.neighbour.home.address, expected.neighbour.home.address);
    verifyShouldBeEqualByComparingFieldByFieldRecursivelyCall(actual, expected, dateOfBirthDifference, neighbourAddressDifference);
}
Also used : Person(org.assertj.core.internal.objects.data.Person) AlwaysEqualPerson(org.assertj.core.internal.objects.data.AlwaysEqualPerson) Date(java.util.Date) RecursiveComparisonAssert_isEqualTo_BaseTest(org.assertj.core.api.RecursiveComparisonAssert_isEqualTo_BaseTest) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 28 with Person

use of org.assertj.core.internal.objects.data.Person in project assertj-core by joel-costigliola.

the class RecursiveComparisonAssert_isEqualTo_withFieldComparators_Test method recursivelyEqualObjectsWhenUsingFieldComparators.

private static Stream<Arguments> recursivelyEqualObjectsWhenUsingFieldComparators() {
    Person person1 = new Person("John");
    person1.home.address.number = 1;
    Person person2 = new Person("JoHN");
    person2.home.address.number = 1;
    Person person3 = new Person("John");
    person3.home.address.number = 1;
    Person person4 = new Person("Jack");
    person4.home.address.number = 2;
    Person person5 = new Person("John");
    person5.home.address.number = 1;
    person5.dateOfBirth = new Date(123);
    person5.neighbour = new Person("Jack");
    person5.neighbour.home.address.number = 123;
    Person person6 = new Person("John");
    person6.home.address.number = 1;
    person6.dateOfBirth = new Date(123);
    person6.neighbour = new Person("Jim");
    person6.neighbour.home.address.number = 456;
    return Stream.of(arguments(person1, person2, CaseInsensitiveStringComparator.instance, array("name"), "same data except int fields and case for strings"), arguments(person3, person4, alwaysEqual(), array("name", "home.address.number"), "same data except for int fields"), // any neighbour differences should be ignored as we compare persons with AlwaysEqualComparator
    arguments(person5, person6, alwaysEqual(), array("neighbour"), "same data except for persons, person's fields should not be compared recursively except at the root level"));
}
Also used : Person(org.assertj.core.internal.objects.data.Person) AlwaysEqualPerson(org.assertj.core.internal.objects.data.AlwaysEqualPerson) Date(java.util.Date)

Example 29 with Person

use of org.assertj.core.internal.objects.data.Person in project assertj-core by joel-costigliola.

the class RecursiveComparisonAssert_isEqualTo_withErrorMessageForFields_Test method field_message_should_take_precedence_over_type_message.

@Test
void field_message_should_take_precedence_over_type_message() {
    // GIVEN
    Person actual = new Person("Valera");
    actual.age = OptionalInt.of(15);
    Person expected = new Person("John");
    expected.age = OptionalInt.of(15);
    String fieldMessage = "Name must be the same";
    String fieldLocation = "name";
    String typeMessage = "Type message has to be ignored";
    // WHEN
    AssertionError assertionError = expectAssertionError(() -> assertThat(actual).usingRecursiveComparison().withErrorMessageForFields(fieldMessage, fieldLocation).withErrorMessageForType(typeMessage, String.class).isEqualTo(expected));
    // THEN
    then(assertionError).hasMessageContainingAll(fieldMessage, ERROR_MESSAGE_DESCRIPTION_FOR_FIELDS, "- " + fieldLocation, ERROR_MESSAGE_PRECEDENCE_DESCRIPTION_FOR_FIELDS).hasMessageNotContainingAny(typeMessage);
}
Also used : AssertionsUtil.expectAssertionError(org.assertj.core.util.AssertionsUtil.expectAssertionError) Person(org.assertj.core.internal.objects.data.Person) Test(org.junit.jupiter.api.Test) RecursiveComparisonAssert_isEqualTo_BaseTest(org.assertj.core.api.RecursiveComparisonAssert_isEqualTo_BaseTest)

Example 30 with Person

use of org.assertj.core.internal.objects.data.Person in project assertj-core by joel-costigliola.

the class RecursiveComparisonAssert_isEqualTo_withFieldComparators_Test method should_fail_when_actual_differs_from_expected_when_using_field_comparators.

@Test
void should_fail_when_actual_differs_from_expected_when_using_field_comparators() {
    // GIVEN
    Person actual = new Person("John");
    actual.home.address.number = 1;
    actual.dateOfBirth = new Date(123);
    actual.neighbour = new Person("Jack");
    actual.neighbour.home.address.number = 123;
    // actually a clone of actual
    Person expected = new Person("John");
    expected.home.address.number = 1;
    expected.dateOfBirth = new Date(123);
    expected.neighbour = new Person("Jack");
    expected.neighbour.home.address.number = 123;
    // register comparators for some fields that will fail the comparison
    recursiveComparisonConfiguration.registerComparatorForFields(alwaysDifferent(), "dateOfBirth", "neighbour.home.address");
    // WHEN
    compareRecursivelyFailsAsExpected(actual, expected);
    // THEN
    ComparisonDifference dateOfBirthDifference = diff("dateOfBirth", actual.dateOfBirth, expected.dateOfBirth);
    ComparisonDifference neighbourAddressDifference = diff("neighbour.home.address", actual.neighbour.home.address, expected.neighbour.home.address);
    verifyShouldBeEqualByComparingFieldByFieldRecursivelyCall(actual, expected, dateOfBirthDifference, neighbourAddressDifference);
}
Also used : Person(org.assertj.core.internal.objects.data.Person) AlwaysEqualPerson(org.assertj.core.internal.objects.data.AlwaysEqualPerson) Date(java.util.Date) RecursiveComparisonAssert_isEqualTo_BaseTest(org.assertj.core.api.RecursiveComparisonAssert_isEqualTo_BaseTest) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Aggregations

Person (org.assertj.core.internal.objects.data.Person)62 Test (org.junit.jupiter.api.Test)46 RecursiveComparisonAssert_isEqualTo_BaseTest (org.assertj.core.api.RecursiveComparisonAssert_isEqualTo_BaseTest)38 AlwaysEqualPerson (org.assertj.core.internal.objects.data.AlwaysEqualPerson)27 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)27 Date (java.util.Date)20 AlwaysDifferentPerson (org.assertj.core.internal.objects.data.AlwaysDifferentPerson)9 FriendlyPerson (org.assertj.core.internal.objects.data.FriendlyPerson)9 Human (org.assertj.core.internal.objects.data.Human)6 RecursiveComparisonAssert_isNotEqualTo_BaseTest (org.assertj.core.api.RecursiveComparisonAssert_isNotEqualTo_BaseTest)5 AlwaysDifferentAddress (org.assertj.core.internal.objects.data.AlwaysDifferentAddress)4 Timestamp (java.sql.Timestamp)3 AlwaysEqualAddress (org.assertj.core.internal.objects.data.AlwaysEqualAddress)3 Giant (org.assertj.core.internal.objects.data.Giant)3 AssertionsUtil.expectAssertionError (org.assertj.core.util.AssertionsUtil.expectAssertionError)3 PersonDto (org.assertj.core.internal.objects.data.PersonDto)2 CartoonCharacter (org.assertj.core.test.CartoonCharacter)2 Comparator (java.util.Comparator)1 AtPrecisionComparator (org.assertj.core.internal.AtPrecisionComparator)1 CaseInsensitiveStringComparator (org.assertj.core.internal.CaseInsensitiveStringComparator)1