Best Assertj code snippet using org.assertj.core.api.recursive.comparison.FieldComparators.comparatorByFields
Source:RecursiveComparisonConfiguration.java
...421 }422 public List<Pattern> getIgnoredOverriddenEqualsForFieldsMatchingRegexes() {423 return ignoredOverriddenEqualsForFieldsMatchingRegexes;424 }425 public Stream<Entry<String, Comparator<?>>> comparatorByFields() {426 return fieldComparators.comparatorByFields();427 }428 @Override429 public String toString() {430 return multiLineDescription(CONFIGURATION_PROVIDER.representation());431 }432 @Override433 public int hashCode() {434 return java.util.Objects.hash(fieldComparators, ignoreAllActualEmptyOptionalFields, ignoreAllActualNullFields,435 ignoreAllExpectedNullFields, ignoreAllOverriddenEquals, ignoreCollectionOrder,436 ignoredCollectionOrderInFields, ignoredCollectionOrderInFieldsMatchingRegexes, ignoredFields,437 ignoredFieldsRegexes, ignoredOverriddenEqualsForFields, ignoredOverriddenEqualsForTypes,438 ignoredOverriddenEqualsForFieldsMatchingRegexes, ignoredTypes, strictTypeChecking,439 typeComparators);440 }441 @Override442 public boolean equals(Object obj) {443 if (this == obj) return true;444 if (obj == null) return false;445 if (getClass() != obj.getClass()) return false;446 RecursiveComparisonConfiguration other = (RecursiveComparisonConfiguration) obj;447 return java.util.Objects.equals(fieldComparators, other.fieldComparators)448 && ignoreAllActualEmptyOptionalFields == other.ignoreAllActualEmptyOptionalFields449 && ignoreAllActualNullFields == other.ignoreAllActualNullFields450 && ignoreAllExpectedNullFields == other.ignoreAllExpectedNullFields451 && ignoreAllOverriddenEquals == other.ignoreAllOverriddenEquals452 && ignoreCollectionOrder == other.ignoreCollectionOrder453 && java.util.Objects.equals(ignoredCollectionOrderInFields, other.ignoredCollectionOrderInFields)454 && java.util.Objects.equals(ignoredFields, other.ignoredFields)455 && java.util.Objects.equals(ignoredFieldsRegexes, other.ignoredFieldsRegexes)456 && java.util.Objects.equals(ignoredOverriddenEqualsForFields, other.ignoredOverriddenEqualsForFields)457 && java.util.Objects.equals(ignoredOverriddenEqualsForTypes, other.ignoredOverriddenEqualsForTypes)458 && java.util.Objects.equals(ignoredOverriddenEqualsForFieldsMatchingRegexes,459 other.ignoredOverriddenEqualsForFieldsMatchingRegexes)460 && java.util.Objects.equals(ignoredTypes, other.ignoredTypes) && strictTypeChecking == other.strictTypeChecking461 && java.util.Objects.equals(typeComparators, other.typeComparators)462 && java.util.Objects.equals(ignoredCollectionOrderInFieldsMatchingRegexes,463 other.ignoredCollectionOrderInFieldsMatchingRegexes);464 }465 public String multiLineDescription(Representation representation) {466 StringBuilder description = new StringBuilder();467 describeIgnoreAllActualNullFields(description);468 describeIgnoreAllActualEmptyOptionalFields(description);469 describeIgnoreAllExpectedNullFields(description);470 describeIgnoredFields(description);471 describeIgnoredFieldsRegexes(description);472 describeIgnoredFieldsForTypes(description);473 describeOverriddenEqualsMethodsUsage(description, representation);474 describeIgnoreCollectionOrder(description);475 describeIgnoredCollectionOrderInFields(description);476 describeIgnoredCollectionOrderInFieldsMatchingRegexes(description);477 describeRegisteredComparatorByTypes(description);478 describeRegisteredComparatorForFields(description);479 describeTypeCheckingStrictness(description);480 return description.toString();481 }482 boolean shouldIgnore(DualValue dualValue) {483 FieldLocation fieldLocation = dualValue.fieldLocation;484 return matchesAnIgnoredField(fieldLocation)485 || matchesAnIgnoredFieldRegex(fieldLocation)486 || shouldIgnoreFieldBasedOnFieldValue(dualValue);487 }488 Set<String> getNonIgnoredActualFieldNames(DualValue dualValue) {489 Set<String> actualFieldsNames = Objects.getFieldsNames(dualValue.actual.getClass());490 // we are doing the same as shouldIgnore(DualValue dualValue) but in two steps for performance reasons:491 // - we filter first ignored field by names that don't need building DualValues492 // - then we filter field DualValues with the remaining criteria that need to get the field value493 // DualValues are built introspecting fields which is expensive.494 return actualFieldsNames.stream()495 // evaluate field name ignoring criteria on dualValue field location + field name496 .filter(fieldName -> !shouldIgnoreFieldBasedOnFieldLocation(dualValue.fieldLocation.field(fieldName)))497 .map(fieldName -> dualValueForField(dualValue, fieldName))498 // evaluate field value ignoring criteria499 .filter(fieldDualValue -> !shouldIgnoreFieldBasedOnFieldValue(fieldDualValue))500 // back to field name501 .map(DualValue::getFieldName)502 .filter(fieldName -> !fieldName.isEmpty())503 .collect(toSet());504 }505 // non accessible stuff506 private boolean shouldIgnoreFieldBasedOnFieldValue(DualValue dualValue) {507 return matchesAnIgnoredNullField(dualValue)508 || matchesAnIgnoredFieldType(dualValue)509 || matchesAnIgnoredEmptyOptionalField(dualValue);510 }511 private boolean shouldIgnoreFieldBasedOnFieldLocation(FieldLocation fieldLocation) {512 return matchesAnIgnoredField(fieldLocation) || matchesAnIgnoredFieldRegex(fieldLocation);513 }514 private static DualValue dualValueForField(DualValue parentDualValue, String fieldName) {515 Object actualFieldValue = COMPARISON.getSimpleValue(fieldName, parentDualValue.actual);516 // no guarantees we have a field in expected named as fieldName517 Object expectedFieldValue;518 try {519 expectedFieldValue = COMPARISON.getSimpleValue(fieldName, parentDualValue.expected);520 } catch (@SuppressWarnings("unused") Exception e) {521 // set the field to null to express it is absent, this not 100% accurate as the value could be null522 // but it works to evaluate if dualValue should be ignored with matchesAnIgnoredFieldType523 expectedFieldValue = null;524 }525 FieldLocation fieldLocation = parentDualValue.fieldLocation.field(fieldName);526 return new DualValue(fieldLocation, actualFieldValue, expectedFieldValue);527 }528 boolean hasCustomComparator(DualValue dualValue) {529 String fieldName = dualValue.getConcatenatedPath();530 if (hasComparatorForField(fieldName)) return true;531 if (dualValue.actual == null && dualValue.expected == null) return false;532 // best effort assuming actual and expected have the same type (not 100% true as we can compare object of differennt types)533 Class<?> valueType = dualValue.actual != null ? dualValue.actual.getClass() : dualValue.expected.getClass();534 return hasComparatorForType(valueType);535 }536 boolean shouldIgnoreOverriddenEqualsOf(DualValue dualValue) {537 // we must compare java basic types otherwise the recursive comparison loops infinitely!538 if (dualValue.isActualJavaType()) return false;539 // enums don't have fields, comparing them field by field has no sense, we need to use equals which is overridden and final540 if (dualValue.isActualAnEnum()) return false;541 return ignoreAllOverriddenEquals542 || matchesAnIgnoredOverriddenEqualsField(dualValue.fieldLocation)543 || (dualValue.actual != null && shouldIgnoreOverriddenEqualsOf(dualValue.actual.getClass()));544 }545 @VisibleForTesting546 boolean shouldIgnoreOverriddenEqualsOf(Class<? extends Object> clazz) {547 return matchesAnIgnoredOverriddenEqualsRegex(clazz) || matchesAnIgnoredOverriddenEqualsType(clazz);548 }549 boolean shouldIgnoreCollectionOrder(FieldLocation fieldLocation) {550 return ignoreCollectionOrder551 || matchesAnIgnoredCollectionOrderInField(fieldLocation)552 || matchesAnIgnoredCollectionOrderInFieldRegex(fieldLocation);553 }554 private void describeIgnoredFieldsRegexes(StringBuilder description) {555 if (!ignoredFieldsRegexes.isEmpty())556 description.append(format("- the fields matching the following regexes were ignored in the comparison: %s%n",557 describeRegexes(ignoredFieldsRegexes)));558 }559 private void describeIgnoredFields(StringBuilder description) {560 if (!ignoredFields.isEmpty())561 description.append(format("- the following fields were ignored in the comparison: %s%n", describeIgnoredFields()));562 }563 private void describeIgnoredFieldsForTypes(StringBuilder description) {564 if (!ignoredTypes.isEmpty())565 description.append(format("- the following types were ignored in the comparison: %s%n", describeIgnoredTypes()));566 }567 private void describeIgnoreAllActualNullFields(StringBuilder description) {568 if (ignoreAllActualNullFields) description.append(format("- all actual null fields were ignored in the comparison%n"));569 }570 private void describeIgnoreAllActualEmptyOptionalFields(StringBuilder description) {571 if (getIgnoreAllActualEmptyOptionalFields())572 description.append(format("- all actual empty optional fields were ignored in the comparison (including Optional, OptionalInt, OptionalLong and OptionalDouble)%n"));573 }574 private void describeIgnoreAllExpectedNullFields(StringBuilder description) {575 if (ignoreAllExpectedNullFields) description.append(format("- all expected null fields were ignored in the comparison%n"));576 }577 private void describeOverriddenEqualsMethodsUsage(StringBuilder description, Representation representation) {578 String header = ignoreAllOverriddenEquals579 ? "- no overridden equals methods were used in the comparison (except for java types)"580 : "- overridden equals methods were used in the comparison";581 description.append(header);582 if (isConfiguredToIgnoreSomeButNotAllOverriddenEqualsMethods()) {583 description.append(format(" except for:%n"));584 describeIgnoredOverriddenEqualsMethods(description, representation);585 } else {586 description.append(format("%n"));587 }588 }589 private void describeIgnoredOverriddenEqualsMethods(StringBuilder description, Representation representation) {590 if (!ignoredOverriddenEqualsForFields.isEmpty())591 description.append(format("%s the following fields: %s%n", INDENT_LEVEL_2,592 describeIgnoredOverriddenEqualsForFields()));593 if (!ignoredOverriddenEqualsForTypes.isEmpty())594 description.append(format("%s the following types: %s%n", INDENT_LEVEL_2,595 describeIgnoredOverriddenEqualsForTypes(representation)));596 if (!ignoredOverriddenEqualsForFieldsMatchingRegexes.isEmpty())597 description.append(format("%s the types matching the following regexes: %s%n", INDENT_LEVEL_2,598 describeRegexes(ignoredOverriddenEqualsForFieldsMatchingRegexes)));599 }600 private String describeIgnoredOverriddenEqualsForTypes(Representation representation) {601 List<String> fieldsDescription = ignoredOverriddenEqualsForTypes.stream()602 .map(representation::toStringOf)603 .collect(toList());604 return join(fieldsDescription).with(", ");605 }606 private String describeIgnoredOverriddenEqualsForFields() {607 return join(ignoredOverriddenEqualsForFields).with(", ");608 }609 private void describeIgnoreCollectionOrder(StringBuilder description) {610 if (ignoreCollectionOrder) description.append(format("- collection order was ignored in all fields in the comparison%n"));611 }612 private void describeIgnoredCollectionOrderInFields(StringBuilder description) {613 if (!ignoredCollectionOrderInFields.isEmpty())614 description.append(format("- collection order was ignored in the following fields in the comparison: %s%n",615 describeIgnoredCollectionOrderInFields()));616 }617 private void describeIgnoredCollectionOrderInFieldsMatchingRegexes(StringBuilder description) {618 if (!ignoredCollectionOrderInFieldsMatchingRegexes.isEmpty())619 description.append(format("- collection order was ignored in the fields matching the following regexes in the comparison: %s%n",620 describeRegexes(ignoredCollectionOrderInFieldsMatchingRegexes)));621 }622 private boolean matchesAnIgnoredOverriddenEqualsRegex(Class<?> clazz) {623 if (ignoredOverriddenEqualsForFieldsMatchingRegexes.isEmpty()) return false; // shortcut624 String canonicalName = clazz.getCanonicalName();625 return ignoredOverriddenEqualsForFieldsMatchingRegexes.stream()626 .anyMatch(regex -> regex.matcher(canonicalName).matches());627 }628 private boolean matchesAnIgnoredOverriddenEqualsType(Class<?> clazz) {629 return ignoredOverriddenEqualsForTypes.contains(clazz);630 }631 private boolean matchesAnIgnoredOverriddenEqualsField(FieldLocation fieldLocation) {632 return ignoredOverriddenEqualsForFields.stream().anyMatch(fieldLocation::matches);633 }634 private boolean matchesAnIgnoredNullField(DualValue dualValue) {635 return (ignoreAllActualNullFields && dualValue.actual == null)636 || (ignoreAllExpectedNullFields && dualValue.expected == null);637 }638 private boolean matchesAnIgnoredEmptyOptionalField(DualValue dualValue) {639 return ignoreAllActualEmptyOptionalFields640 && dualValue.isActualFieldAnEmptyOptionalOfAnyType();641 }642 private boolean matchesAnIgnoredFieldRegex(FieldLocation fieldLocation) {643 return ignoredFieldsRegexes.stream()644 .anyMatch(regex -> regex.matcher(fieldLocation.getPathToUseInRules()).matches());645 }646 private boolean matchesAnIgnoredFieldType(DualValue dualValue) {647 Object actual = dualValue.actual;648 if (actual != null) return ignoredTypes.contains(actual.getClass());649 Object expected = dualValue.expected;650 // actual is null => we can't evaluate its type, we can only reliably check dualValue.expected's type if651 // strictTypeChecking is enabled which guarantees expected is of the same type.652 if (strictTypeChecking && expected != null) return ignoredTypes.contains(expected.getClass());653 // if strictTypeChecking is disabled, we can't safely ignore the field (if we did, we would ignore all null fields!).654 return false;655 }656 private boolean matchesAnIgnoredField(FieldLocation fieldLocation) {657 return ignoredFields.stream().anyMatch(fieldLocation::matches);658 }659 private boolean matchesAnIgnoredCollectionOrderInField(FieldLocation fieldLocation) {660 return ignoredCollectionOrderInFields.stream().anyMatch(fieldLocation::matches);661 }662 private boolean matchesAnIgnoredCollectionOrderInFieldRegex(FieldLocation fieldLocation) {663 String pathToUseInRules = fieldLocation.getPathToUseInRules();664 return ignoredCollectionOrderInFieldsMatchingRegexes.stream().anyMatch(regex -> regex.matcher(pathToUseInRules).matches());665 }666 private String describeIgnoredFields() {667 return join(ignoredFields).with(", ");668 }669 private String describeIgnoredTypes() {670 List<String> typesDescription = ignoredTypes.stream()671 .map(Class::getName)672 .collect(toList());673 return join(typesDescription).with(", ");674 }675 private String describeIgnoredCollectionOrderInFields() {676 return join(ignoredCollectionOrderInFields).with(", ");677 }678 private String describeRegexes(List<Pattern> regexes) {679 List<String> fieldsDescription = regexes.stream()680 .map(Pattern::pattern)681 .collect(toList());682 return join(fieldsDescription).with(", ");683 }684 private boolean isConfiguredToIgnoreSomeButNotAllOverriddenEqualsMethods() {685 boolean ignoreSomeOverriddenEqualsMethods = !ignoredOverriddenEqualsForFieldsMatchingRegexes.isEmpty()686 || !ignoredOverriddenEqualsForTypes.isEmpty()687 || !ignoredOverriddenEqualsForFields.isEmpty();688 return !ignoreAllOverriddenEquals && ignoreSomeOverriddenEqualsMethods;689 }690 private void describeRegisteredComparatorByTypes(StringBuilder description) {691 if (!typeComparators.isEmpty()) {692 description.append(format("- these types were compared with the following comparators:%n"));693 describeComparatorForTypes(description);694 }695 }696 private void describeComparatorForTypes(StringBuilder description) {697 typeComparators.comparatorByTypes()698 .map(this::formatRegisteredComparatorByType)699 .forEach(description::append);700 }701 private String formatRegisteredComparatorByType(Entry<Class<?>, Comparator<?>> next) {702 return format("%s %s -> %s%n", INDENT_LEVEL_2, next.getKey().getName(), next.getValue());703 }704 private void describeRegisteredComparatorForFields(StringBuilder description) {705 if (!fieldComparators.isEmpty()) {706 description.append(format("- these fields were compared with the following comparators:%n"));707 describeComparatorForFields(description);708 if (!typeComparators.isEmpty()) {709 description.append(format("- field comparators take precedence over type comparators.%n"));710 }711 }712 }713 private void describeComparatorForFields(StringBuilder description) {714 fieldComparators.comparatorByFields()715 .map(this::formatRegisteredComparatorForField)716 .forEach(description::append);717 }718 private String formatRegisteredComparatorForField(Entry<String, Comparator<?>> comparatorForField) {719 return format("%s %s -> %s%n", INDENT_LEVEL_2, comparatorForField.getKey(), comparatorForField.getValue());720 }721 private void describeTypeCheckingStrictness(StringBuilder description) {722 String str = strictTypeChecking723 ? "- actual and expected objects and their fields were considered different when of incompatible types (i.e. expected type does not extend actual's type) even if all their fields match, for example a Person instance will never match a PersonDto (call strictTypeChecking(false) to change that behavior).%n"724 : "- actual and expected objects and their fields were compared field by field recursively even if they were not of the same type, this allows for example to compare a Person to a PersonDto (call strictTypeChecking(true) to change that behavior).%n";725 description.append(format(str));726 }727 /**728 * Creates builder to build {@link RecursiveComparisonConfiguration}....
Source:RecursiveComparisonAssert_fluent_API_Test.java
...43 // THEN44 assertThat(recursiveComparisonConfiguration.isInStrictTypeCheckingMode()).isFalse();45 List<Entry<Class<?>, Comparator<?>>> defaultComparators = defaultTypeComparators().comparatorByTypes().collect(toList());46 assertThat(recursiveComparisonConfiguration.comparatorByTypes()).containsExactlyElementsOf(defaultComparators);47 assertThat(recursiveComparisonConfiguration.comparatorByFields()).isEmpty();48 assertThat(recursiveComparisonConfiguration.getIgnoreAllActualNullFields()).isFalse();49 assertThat(recursiveComparisonConfiguration.getIgnoredFields()).isEmpty();50 assertThat(recursiveComparisonConfiguration.getIgnoredTypes()).isEmpty();51 assertThat(recursiveComparisonConfiguration.getIgnoredFieldsRegexes()).isEmpty();52 assertThat(recursiveComparisonConfiguration.getIgnoredOverriddenEqualsForFields()).isEmpty();53 assertThat(recursiveComparisonConfiguration.getIgnoredOverriddenEqualsForTypes()).isEmpty();54 assertThat(recursiveComparisonConfiguration.getIgnoredOverriddenEqualsForFieldsMatchingRegexes()).isEmpty();55 assertThat(recursiveComparisonConfiguration.hasCustomComparators()).isTrue();56 }57 @Test58 void should_allow_to_enable_strict_mode_comparison() {59 // GIVEN60 RecursiveComparisonConfiguration recursiveComparisonConfiguration = new RecursiveComparisonConfiguration();61 // WHEN62 RecursiveComparisonConfiguration configuration = assertThat(ACTUAL).usingRecursiveComparison(recursiveComparisonConfiguration)63 .withStrictTypeChecking()64 .getRecursiveComparisonConfiguration();65 // THEN66 assertThat(configuration.isInStrictTypeCheckingMode()).isTrue();67 }68 @Test69 void should_allow_to_use_its_own_RecursiveComparisonConfiguration() {70 // GIVEN71 RecursiveComparisonConfiguration recursiveComparisonConfiguration = new RecursiveComparisonConfiguration();72 // WHEN73 RecursiveComparisonConfiguration configuration = assertThat(ACTUAL).usingRecursiveComparison(recursiveComparisonConfiguration)74 .getRecursiveComparisonConfiguration();75 // THEN76 assertThat(configuration).isSameAs(recursiveComparisonConfiguration);77 }78 @Test79 void should_allow_to_ignore_all_actual_null_fields() {80 // WHEN81 RecursiveComparisonConfiguration configuration = assertThat(ACTUAL).usingRecursiveComparison()82 .ignoringActualNullFields()83 .getRecursiveComparisonConfiguration();84 // THEN85 assertThat(configuration.getIgnoreAllActualNullFields()).isTrue();86 }87 @Test88 void should_allow_to_ignore_all_actual_empty_optional_fields() {89 // WHEN90 RecursiveComparisonConfiguration configuration = assertThat(ACTUAL).usingRecursiveComparison()91 .ignoringActualEmptyOptionalFields()92 .getRecursiveComparisonConfiguration();93 // THEN94 assertThat(configuration.getIgnoreAllActualEmptyOptionalFields()).isTrue();95 }96 @Test97 void should_allow_to_ignore_fields() {98 // GIVEN99 String field1 = "foo";100 String field2 = "foo.bar";101 // WHEN102 RecursiveComparisonConfiguration configuration = assertThat(ACTUAL).usingRecursiveComparison()103 .ignoringFields(field1, field2)104 .getRecursiveComparisonConfiguration();105 // THEN106 assertThat(configuration.getIgnoredFields()).containsExactly(field1, field2);107 }108 @Test109 void should_allow_to_ignore_fields_matching_regexes() {110 // GIVEN111 String regex1 = "foo";112 String regex2 = ".*foo.*";113 // WHEN114 RecursiveComparisonConfiguration configuration = assertThat(ACTUAL).usingRecursiveComparison()115 .ignoringFieldsMatchingRegexes(regex1, regex2)116 .getRecursiveComparisonConfiguration();117 // THEN118 assertThat(configuration.getIgnoredFieldsRegexes()).extracting(Pattern::pattern)119 .containsExactly(regex1, regex2);120 }121 @Test122 void should_allow_to_ignore_fields_of_the_given_types() {123 // GIVEN124 Class<?> type1 = UUID.class;125 Class<?> type2 = String.class;126 // WHEN127 RecursiveComparisonConfiguration configuration = assertThat(ACTUAL).usingRecursiveComparison()128 .ignoringFieldsOfTypes(type1, type2)129 .getRecursiveComparisonConfiguration();130 // THEN131 assertThat(configuration.getIgnoredTypes()).containsExactly(type1, type2);132 }133 @Test134 void should_allow_to_ignore_overridden_equals_for_fields() {135 // GIVEN136 String field1 = "foo";137 String field2 = "foo.bar";138 // WHEN139 RecursiveComparisonConfiguration configuration = assertThat(ACTUAL).usingRecursiveComparison()140 .ignoringOverriddenEqualsForFields(field1, field2)141 .getRecursiveComparisonConfiguration();142 // THEN143 assertThat(configuration.getIgnoredOverriddenEqualsForFields()).containsExactly(field1, field2);144 }145 @Test146 void should_allow_to_ignore_overridden_equals_by_regexes() {147 // GIVEN148 String regex1 = "foo";149 String regex2 = ".*foo.*";150 // WHEN151 RecursiveComparisonConfiguration configuration = assertThat(ACTUAL).usingRecursiveComparison()152 .ignoringOverriddenEqualsForFieldsMatchingRegexes(regex1,153 regex2)154 .getRecursiveComparisonConfiguration();155 // THEN156 assertThat(configuration.getIgnoredOverriddenEqualsForFieldsMatchingRegexes()).extracting(Pattern::pattern)157 .containsExactly(regex1, regex2);158 }159 @Test160 void should_allow_to_ignore_overridden_equals_for_types() {161 // GIVEN162 Class<String> type1 = String.class;163 Class<Date> type2 = Date.class;164 // WHEN165 RecursiveComparisonConfiguration configuration = assertThat(ACTUAL).usingRecursiveComparison()166 .ignoringOverriddenEqualsForTypes(type1, type2)167 .getRecursiveComparisonConfiguration();168 // THEN169 assertThat(configuration.getIgnoredOverriddenEqualsForTypes()).containsExactly(type1, type2);170 }171 @Test172 void should_allow_to_ignore_collection_order() {173 // WHEN174 RecursiveComparisonConfiguration configuration = assertThat(ACTUAL).usingRecursiveComparison()175 .ignoringCollectionOrder()176 .getRecursiveComparisonConfiguration();177 // THEN178 assertThat(configuration.getIgnoreCollectionOrder()).isTrue();179 }180 @Test181 void should_allow_to_ignore_collection_order_in_fields() {182 // GIVEN183 String field1 = "foo";184 String field2 = "foo.bar";185 // WHEN186 RecursiveComparisonConfiguration configuration = assertThat(ACTUAL).usingRecursiveComparison()187 .ignoringCollectionOrderInFields(field1, field2)188 .getRecursiveComparisonConfiguration();189 // THEN190 assertThat(configuration.getIgnoredCollectionOrderInFields()).containsExactly(field1, field2);191 }192 @Test193 void should_allow_to_ignore_collection_order_in_fields_matching_regexes() {194 // GIVEN195 String regex1 = "foo";196 String regex2 = ".*foo.*";197 // WHEN198 RecursiveComparisonConfiguration configuration = assertThat(ACTUAL).usingRecursiveComparison()199 .ignoringCollectionOrderInFieldsMatchingRegexes(regex1,200 regex2)201 .getRecursiveComparisonConfiguration();202 // THEN203 assertThat(configuration.getIgnoredCollectionOrderInFieldsMatchingRegexes()).extracting(Pattern::pattern)204 .containsExactly(regex1, regex2);205 }206 @Test207 void should_allow_to_register_field_comparators() {208 // GIVEN209 String field1 = "foo";210 String field2 = "foo.bar";211 String field3 = "bar";212 String field4 = "baz";213 AlwaysEqualComparator<?> alwaysEqualComparator = alwaysEqual();214 AlwaysDifferentComparator<?> alwaysDifferentComparator = alwaysDifferent();215 // WHEN216 // @format:off217 RecursiveComparisonConfiguration configuration = assertThat(ACTUAL).usingRecursiveComparison()218 .withComparatorForFields(alwaysEqualComparator, field1, field3)219 .withComparatorForFields(alwaysDifferentComparator, field2)220 .withEqualsForFields((o1, o2) -> true, field4)221 .getRecursiveComparisonConfiguration();222 // @format:on223 // THEN224 assertThat(configuration.comparatorByFields()).hasSize(4)225 .contains(entry(field3, alwaysEqualComparator),226 entry(field1, alwaysEqualComparator),227 entry(field2, alwaysDifferentComparator));228 assertThat(configuration.comparatorByFields()).anyMatch(entry -> entry.getKey().equals(field4) && entry.getValue() != null);229 }230 @Test231 void should_allow_to_register_comparators_by_regex_matching_fields() {232 // WHEN233 // @format:off234 RecursiveComparisonConfiguration configuration = assertThat(ACTUAL).usingRecursiveComparison()235 .withEqualsForFieldsMatchingRegexes(STRING_EQUALS, ".*a", ".*b")236 .withEqualsForFieldsMatchingRegexes(DOUBLE_EQUALS, ".*d", ".*e")237 .getRecursiveComparisonConfiguration();238 // @format:on239 // THEN240 FieldComparators fieldComparators = configuration.getFieldComparators();241 then(fieldComparators.hasRegexFieldComparators()).isTrue();242 then(fieldComparators.comparatorByRegexFields()).allMatch(entry -> entry.getValue() != null);...
Source:FieldComparators.java
...70 }71 public Comparator<?> getComparatorForField(FieldLocation fieldLocation) {72 return fieldComparators.get(fieldLocation);73 }74 public Stream<Entry<FieldLocation, Comparator<?>>> comparatorByFields() {75 return fieldComparators.entrySet().stream();76 }77}...
comparatorByFields
Using AI Code Generation
1import static org.assertj.core.api.Assertions.assertThat;2import static org.assertj.core.api.recursive.comparison.FieldComparators.comparatorByFields;3import static org.assertj.core.api.recursive.comparison.RecursiveComparisonConfiguration.builder;4import static org.assertj.core.util.Lists.list;5import java.util.List;6import org.assertj.core.api.recursive.comparison.RecursiveComparisonConfiguration;7import org.junit.jupiter.api.Test;8class RecursiveComparisonTest {9 void testRecursiveComparison() {10 RecursiveComparisonConfiguration configuration = builder().withComparatorForFields(comparatorByFields("id", "name"), Person.class).build();11 Person person1 = new Person("1", "John");12 Person person2 = new Person("1", "John");13 assertThat(person1).usingRecursiveComparison(configuration).isEqualTo(person2);14 }15 void testRecursiveComparison2() {16 RecursiveComparisonConfiguration configuration = builder().withComparatorForFields(comparatorByFields("id", "name"), Person.class).build();17 Person person1 = new Person("1", "John");18 Person person2 = new Person("1", "John");19 List<Person> list1 = list(person1);20 List<Person> list2 = list(person2);21 assertThat(list1).usingRecursiveComparison(configuration).isEqualTo(list2);22 }23}24class Person {25 String id;26 String name;27 public Person(String id, String name) {28 this.id = id;29 this.name = name;30 }31 public String getId() {32 return id;33 }34 public void setId(String id) {35 this.id = id;36 }37 public String getName() {38 return name;39 }40 public void setName(String name) {41 this.name = name;42 }43}44 <[Person{id='1', name='John'}]>45 <[Person{id='1', name='John'}]>46at org.assertj.core.api.recursive.comparison.RecursiveComparisonAssert.isEqualTo(RecursiveComparisonAssert.java:133)47at RecursiveComparisonTest.testRecursiveComparison2(RecursiveComparisonTest.java:33)
comparatorByFields
Using AI Code Generation
1import static org.assertj.core.api.recursive.comparison.FieldComparators.comparatorByFields;2import static org.assertj.core.api.recursive.comparison.FieldComparators.byName;3import static org.assertj.core.api.recursive.comparison.FieldComparators.byNameAndType;4import static org.assertj.core.api.recursive.comparison.FieldComparators.byNameAndTypeAndValue;5import static org.assertj.core.api.recursive.comparison.FieldComparators.byNameAndTypeAndValueIgnoringFields;6import static org.assertj.core.api.recursive.comparison.FieldComparators.byNameAndTypeAndValueIgnoringFieldsRecursive;7import static org.assertj.core.api.recursive.comparison.FieldComparators.byNameAndValue;8import static org.assertj.core.api.recursive.comparison.FieldComparators.byNameAndValueIgnoringFields;9import static org.assertj.core.api.recursive.comparison.FieldComparators.byNameAndValueIgnoringFieldsRecursive;10import static org.assertj.core.api.recursive.comparison.FieldComparators.byNameIgnoringFields;11import static org.assertj.core.api.recursive.comparison.FieldComparators.byNameIgnoringFieldsRecursive;12import static org.assertj.core.api.recursive.comparison.FieldComparators.byType;13import static org.assertj.core.api.recursive.comparison.FieldComparators.byTypeAndValue;14import static org.assertj.core.api.recursive.comparison.FieldComparators.byTypeAndValueIgnoringFields;15import static org.assertj.core.api.recursive.comparison.FieldComparators.byTypeAndValueIgnoringFieldsRecursive;16import static org.assertj.core.api.recursive.comparison.FieldComparators.byValue;17import static org.assertj.core.api.recursive.comparison.FieldComparators.byValueIgnoringFields;18import static org.assertj.core.api.recursive.comparison.FieldComparators.byValueIgnoringFieldsRecursive;19import java.util.Comparator;20import org.assertj.core.api.recursive.comparison.FieldComparator;21import org.assertj.core.api.recursive.comparison.RecursiveComparisonConfiguration;22import org.assertj.core.api.recursive.comparison.RecursiveComparisonDifference;23public class Test {24 public static void main(String[] args) {25 Comparator<RecursiveComparisonDifference> comparator = comparatorByFields(26 byName("name"), byName("age"), byName("address"), byName("city"), byName("country"),27 byName("phoneNumbers"), byName("email"), byName("birthDate"), byName("birthTime"),28 byName("birthDateTime"), byName("birthLocalDate"), byName("birthLocalTime"),29 byName("birthLocalDateTime"), byName("birthZonedDateTime
comparatorByFields
Using AI Code Generation
1import org.assertj.core.api.recursive.comparison.FieldComparators;2import org.assertj.core.api.recursive.comparison.FieldLocation;3import org.assertj.core.api.recursive.comparison.FieldLocationComparator;4import org.assertj.core.api.recursive.comparison.FieldLocationComparatorByField;5import java.util.Comparator;6public class ComparatorByFields {7 public static void main(String[] args) {8 FieldLocationComparator fieldLocationComparator = FieldComparators.comparatorByFields("field1", "field2");9 FieldLocation fieldLocation1 = new FieldLocation("field1", 1);10 FieldLocation fieldLocation2 = new FieldLocation("field2", 2);11 Comparator<FieldLocation> comparator = new FieldLocationComparatorByField(fieldLocationComparator, "field1", "field2");12 int result = comparator.compare(fieldLocation1, fieldLocation2);13 System.out.println(result);14 }15}16import org.assertj.core.api.recursive.comparison.FieldComparators;17import org.assertj.core.api.recursive.comparison.FieldLocation;18import org.assertj.core.api.recursive.comparison.FieldLocationComparator;19import org.assertj.core.api.recursive.comparison.FieldLocationComparatorByField;20import java.util.Comparator;21public class ComparatorByFields {22 public static void main(String[] args) {23 FieldLocationComparator fieldLocationComparator = FieldComparators.comparatorByFields("field1", "field2");24 FieldLocation fieldLocation1 = new FieldLocation("field1", 1);25 FieldLocation fieldLocation2 = new FieldLocation("field2", 2);26 Comparator<FieldLocation> comparator = new FieldLocationComparatorByField(fieldLocationComparator, "field1", "field2");27 int result = comparator.compare(fieldLocation1, fieldLocation2);28 System.out.println(result);29 }30}31import org.assertj.core.api.recursive.comparison.FieldComparators;32import org.assertj.core.api.recursive.comparison.FieldLocation;33import org.assertj.core.api.recursive.comparison.FieldLocationComparator;34import org.assertj.core.api.recursive.comparison.FieldLocationComparatorByField;35import java.util.Comparator;36public class ComparatorByFields {
comparatorByFields
Using AI Code Generation
1import static org.assertj.core.api.recursive.comparison.FieldComparators.comparatorByFields;2import static org.assertj.core.api.recursive.comparison.FieldComparators.ignoreAllActualNullFieldsComparator;3import static org.assertj.core.api.recursive.comparison.FieldComparators.ignoreAllActualEmptyValuesComparator;4import static org.assertj.core.api.recursive.comparison.FieldComparators.ignoreAllExpectedNullFieldsComparator;5import static org.assertj.core.api.recursive.comparison.FieldComparators.ignoreAllExpectedEmptyValuesComparator;6import static org.assertj.core.api.recursive.comparison.FieldComparators.ignoreAllActualAndExpectedNullFieldsComparator;7import static org.assertj.core.api.recursive.comparison.FieldComparators.ignoreAllActualAndExpectedEmptyValuesComparator;8import static org.assertj.core.api.recursive.comparison.FieldComparators.ignoreAllActualAndExpectedNullFieldsAndEmptyValuesComparator;9import static org.assertj.core.api.recursive.comparison.FieldComparators.ignoreAllActualAndExpectedNullFieldsAndEmptyValuesExceptForGivenFieldsComparator;10import static org.assertj.core.api.recursive.comparison.FieldComparators.ignoreAllActualAndExpectedNullFieldsAndEmptyValuesExceptForGivenFieldsComparator;11import static org.assertj.core.api.recursive.comparison.FieldComparators.ignoreAllActualAndExpectedNullFieldsAndEmptyValuesExceptForGivenFieldsComparator;12import static org.assertj.core.api.recursive.comparison.FieldComparators.ignoreAllActualAndExpectedNullFieldsAndEmptyValuesExceptForGivenFieldsComparator;13import java.util.ArrayList;14import java.util.Arrays;15import java.util.List;16import org.assertj.core.api.recursive.comparison.FieldComparisonStrategy;17import org.assertj.core.api.recursive.comparison.FieldLocation;18import org.assertj.core.api.recursive.comparison.FieldLocation.FieldLocationBuilder;19import org.assertj.core.api.recursive.comparison.FieldLocation.FieldLocationType;20import org.assertj.core.api.recursive.comparison.RecursiveComparisonConfiguration;21import org.assertj.core.api.recursive.comparison.RecursiveComparisonDifference;22import org.assertj.core.api.recursive.comparison.RecursiveComparisonDifference.DifferenceType;23import org.assertj.core.api.recursive.comparison.RecursiveComparisonDifference.DifferenceType.DifferenceTypeBuilder;24import org.assertj.core.api.recursive.comparison.RecursiveComparisonDifference.DifferenceType.DifferenceTypeBuilder.DifferenceTypeBuilderWithActualAndExpected;25import org.assertj.core.api.recursive.comparison.RecursiveComparisonDifference.DifferenceType.DifferenceTypeBuilder.DifferenceTypeBuilderWithActualAndExpected.DifferenceTypeBuilderWithActualAndExpectedAndLocation;26import org.assertj.core.api.recursive.comparison.Recursive
comparatorByFields
Using AI Code Generation
1import java.util.Arrays;2import java.util.List;3import java.util.Map;4import org.assertj.core.api.recursive.comparison.FieldComparators;5import org.assertj.core.api.recursive.comparison.RecursiveComparisonConfiguration;6import org.assertj.core.api.recursive.comparison.RecursiveComparisonDifference;7import org.assertj.core.api.recursive.comparison.RecursiveComparisonDifferenceCalculator;8import org.assertj.core.api.recursive.comparison.RecursiveComparisonDifferenceCalculator.RecursiveComparisonDifferenceCalculatorFactory;9import org.assertj.core.api.recursive.comparison.RecursiveComparisonDifferenceCalculator.RecursiveComparisonDifferenceCalculatorFactoryImpl;10public class RecursiveComparisonDifferenceCalculatorTest {11 public static void main(String[] args) {12 RecursiveComparisonDifferenceCalculatorFactory factory = new RecursiveComparisonDifferenceCalculatorFactoryImpl();13 RecursiveComparisonDifferenceCalculator calculator = factory.createRecursiveComparisonDifferenceCalculator(14 new RecursiveComparisonConfiguration().withFieldComparators(FieldComparators.comparatorByFields("id")));15 List<RecursiveComparisonDifference> differences = calculator.calculateDifferences(Arrays.asList(new Object[] { new Person(1, "John", 25), new Person(2, "Jane", 30) }),16 Arrays.asList(new Object[] { new Person(1, "John", 25), new Person(2, "Jane", 30) }));17 System.out.println(differences);18 }19}20public class Person {21 private final int id;22 private final String name;23 private final int age;24 public Person(int id, String name, int age) {25 this.id = id;26 this.name = name;27 this.age = age;28 }29 public int getId() {30 return id;31 }32 public String getName() {33 return name;34 }35 public int getAge() {36 return age;37 }38 public String toString() {39 return "Person [id=" + id + ", name=" + name + ", age=" + age + "]";40 }41}
comparatorByFields
Using AI Code Generation
1import static org.assertj.core.api.Assertions.*;2import static org.assertj.core.api.recursive.comparison.FieldComparators.*;3import org.assertj.core.api.recursive.comparison.FieldComparators;4import org.assertj.core.api.recursive.comparison.RecursiveComparisonConfiguration;5import org.assertj.core.api.recursive.comparison.RecursiveComparisonDifference;6import org.assertj.core.api.recursive.comparison.RecursiveComparisonConfiguration.*;7import org.junit.Test;8import java.util.*;9public class TestRecursiveComparison {10 public void test1() {11 List<String> list1 = new ArrayList<>();12 list1.add("a");13 list1.add("b");14 list1.add("c");15 List<String> list2 = new ArrayList<>();16 list2.add("b");17 list2.add("c");18 list2.add("a");19 RecursiveComparisonConfiguration configuration = RecursiveComparisonConfiguration.builder()20 .withComparatorForFields(comparatorByFields("size"), "size")21 .build();22 List<RecursiveComparisonDifference> differences = assertThat(list1).usingRecursiveComparison(configuration).isEqualTo(list2);23 assertThat(differences).isEmpty();24 }25}26 at org.assertj.core.api.recursive.comparison.FieldComparators.comparatorByFields(FieldComparators.java:91)27 at TestRecursiveComparison.test1(TestRecursiveComparison.java:23)28 at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)29 at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)30 at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)31 at java.base/java.lang.reflect.Method.invoke(Method.java:566)32 at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)33 at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)34 at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)35 at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)36 at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)37 at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)38 at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
comparatorByFields
Using AI Code Generation
1import org.assertj.core.api.recursive.comparison.FieldComparators;2import org.assertj.core.api.recursive.comparison.RecursiveComparisonConfiguration;3import org.assertj.core.api.recursive.comparison.RecursiveComparisonDifference;4import org.assertj.core.api.recursive.comparison.RecursiveComparisonDifferenceListener;5import java.util.List;6public class RecursiveComparisonByComparator {7 public static void main(String[] args) {8 Employee employee1 = new Employee("John", "Doe", 100000);9 Employee employee2 = new Employee("John", "Doe", 100000);10 RecursiveComparisonConfiguration recursiveComparisonConfiguration = new RecursiveComparisonConfiguration();11 recursiveComparisonConfiguration.registerComparatorForType(new EmployeeComparator(), Employee.class);12 List<RecursiveComparisonDifference> differences = FieldComparators.comparatorByFields(recursiveComparisonConfiguration).compare(employee1, employee2);13 differences.forEach(System.out::println);14 }15}16import org.assertj.core.api.recursive.comparison.FieldComparators;17import org.assertj.core.api.recursive.comparison.RecursiveComparisonConfiguration;18import org.assertj.core.api.recursive.comparison.RecursiveComparisonDifference;19import org.assertj.core.api.recursive.comparison.RecursiveComparisonDifferenceListener;20import java.util.List;21public class RecursiveComparisonByComparator {22 public static void main(String[] args) {23 Employee employee1 = new Employee("John", "Doe", 100000);24 Employee employee2 = new Employee("John", "Doe", 100000);25 RecursiveComparisonConfiguration recursiveComparisonConfiguration = new RecursiveComparisonConfiguration();26 recursiveComparisonConfiguration.registerComparatorForType(new EmployeeComparator(), Employee.class);27 List<RecursiveComparisonDifference> differences = FieldComparators.comparatorByFields(recursiveComparisonConfiguration).compare(employee1, employee2);28 differences.forEach(System.out::println);29 }30}
comparatorByFields
Using AI Code Generation
1public class Main {2 public static void main(String[] args) {3 Employee emp1 = new Employee(1, "John", 10000);4 Employee emp2 = new Employee(1, "John", 10000);5 Assertions.assertThat(emp1).usingComparator(ComparatorByFieldsComparator.comparatorByFields(Employee.class)).isEqualTo(emp2);6 }7}8public class Main {9 public static void main(String[] args) {10 Employee emp1 = new Employee(1, "John", 10000);11 Employee emp2 = new Employee(1, "John", 10000);12 Assertions.assertThat(emp1).usingComparator(ComparatorByFieldsComparator.comparatorByFields(Employee.class, "id", "name")).isEqualTo(emp2);13 }14}15public class Main {16 public static void main(String[] args) {17 Employee emp1 = new Employee(1, "John", 10000);18 Employee emp2 = new Employee(1, "John", 10000);19 Assertions.assertThat(emp1).usingComparator(ComparatorByFieldsComparator.comparatorByFields(Employee.class, "id", "name", "salary")).isEqualTo(emp2);20 }21}22public class Main {23 public static void main(String[] args) {24 Employee emp1 = new Employee(1, "John", 10000);25 Employee emp2 = new Employee(1, "John", 10000);26 Assertions.assertThat(emp1).usingComparator(ComparatorByFieldsComparator.comparatorByFields(Employee.class, "id", "name", "salary")).isEqualTo(emp2);27 }28}29public class Main {30 public static void main(String[] args)
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!