Best Assertj code snippet using org.assertj.core.api.recursive.comparison.RecursiveComparisonConfiguration.describeIgnoredCollectionOrderInFields
Source:RecursiveComparisonConfiguration.java
...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 }...
describeIgnoredCollectionOrderInFields
Using AI Code Generation
1assertThat(actual).usingRecursiveComparison()2 .ignoringCollectionOrderInFields("addresses")3 .isEqualTo(expected);4assertThat(actual).usingRecursiveComparison()5 .ignoringCollectionOrderInFields("addresses", "emails")6 .isEqualTo(expected);7assertThat(actual).usingRecursiveComparison()8 .ignoringCollectionOrderInFields("addresses", "emails", "phones")9 .isEqualTo(expected);10assertThat(actual).usingRecursiveComparison()11 .ignoringCollectionOrderInFields("addresses", "emails", "phones", "friends")12 .isEqualTo(expected);13assertThat(actual).usingRecursiveComparison()14 .ignoringCollectionOrderInFields("addresses", "emails", "phones", "friends", "pets")15 .isEqualTo(expected);16assertThat(actual).usingRecursiveComparison()17 .ignoringCollectionOrderInFields("addresses", "emails", "phones", "friends", "pets", "cars")18 .isEqualTo(expected);19assertThat(actual).usingRecursiveComparison()20 .ignoringCollectionOrderInFields("addresses", "emails", "phones", "friends", "pets", "cars", "bikes")21 .isEqualTo(expected);22assertThat(actual).usingRecursiveComparison()23 .ignoringCollectionOrderInFields("addresses", "emails", "phones", "friends", "pets", "cars", "bikes", "houses")24 .isEqualTo(expected);
describeIgnoredCollectionOrderInFields
Using AI Code Generation
1import static org.assertj.core.api.Assertions.assertThat;2import static org.assertj.core.api.Assertions.within;3import static org.assertj.core.api.recursive.comparison.RecursiveComparisonConfiguration.builder;4import static org.assertj.core.api.recursive.comparison.RecursiveComparisonConfiguration.recursiveComparison;5import java.util.Arrays;6import java.util.List;7import org.assertj.core.api.recursive.comparison.RecursiveComparisonConfiguration;8import org.junit.jupiter.api.Test;9class RecursiveComparisonConfigurationTest {10 private static final RecursiveComparisonConfiguration configuration = builder()11 .withIgnoredFields("ignored")12 .withIgnoredCollectionOrderInFields("list")13 .build();14 void shouldDescribeCollectionOrderInFields() {15 Foo foo1 = new Foo(Arrays.asList("bar", "baz"));16 Foo foo2 = new Foo(Arrays.asList("baz", "bar"));17 String description = recursiveComparison(configuration).isEqualTo(foo1).descriptionOfDifferences(foo2);18 assertThat(description).isEqualTo("foo.list[0] expected:<[bar]> but was:<[baz]>");19 }20 void shouldDescribeCollectionOrderInFieldsForNestedObjects() {21 Foo foo1 = new Foo(Arrays.asList("bar", "baz"));22 Foo foo2 = new Foo(Arrays.asList("baz", "bar"));23 Bar bar1 = new Bar(foo1);24 Bar bar2 = new Bar(foo2);25 String description = recursiveComparison(configuration).isEqualTo(bar1).descriptionOfDifferences(bar2);26 assertThat(description).isEqualTo("bar.foo.list[0] expected:<[bar]> but was:<[baz]>");27 }28 void shouldDescribeCollectionOrderInFieldsForNestedObjectsInList() {29 Foo foo1 = new Foo(Arrays.asList("bar", "baz"));30 Foo foo2 = new Foo(Arrays.asList("baz", "bar"));31 Foo foo3 = new Foo(Arrays.asList("foo", "bar"));32 Foo foo4 = new Foo(Arrays.asList("bar", "foo"));33 Bar bar1 = new Bar(foo1);34 Bar bar2 = new Bar(foo2);35 Bar bar3 = new Bar(foo
describeIgnoredCollectionOrderInFields
Using AI Code Generation
1public RecursiveComparisonConfiguration ignoreCollectionOrder(boolean ignoreCollectionOrder)2public RecursiveComparisonConfiguration ignoreCollectionOrder(java.util.function.Predicate<java.lang.Object> ignoreCollectionOrderPredicate)3public RecursiveComparisonConfiguration ignoreCollectionOrder(java.util.function.Predicate<java.lang.Object> ignoreCollectionOrderPredicate,4public RecursiveComparisonConfiguration ignoreCollectionOrder(java.util.function.BiPredicate<java.lang.Object,java.lang.Object> ignoreCollectionOrderForElementsPredicate)5public RecursiveComparisonConfiguration ignoreCollectionOrder(java.util.function.Predicate<java.lang.Object> ignoreCollectionOrderPredicate,6public RecursiveComparisonConfiguration ignoreCollectionOrder(java.util.function.BiPredicate<java.lang.Object,java.lang.Object> ignoreCollectionOrderForElementsPredicate,7public RecursiveComparisonConfiguration ignoreCollectionOrder(java.util.function.Predicate<java.lang.Object> ignoreCollectionOrderPredicate,8public RecursiveComparisonConfiguration ignoreCollectionOrder(java.util.function.BiPredicate<java.lang.Object,java.lang.Object> ignoreCollectionOrderForElementsPredicate,9public RecursiveComparisonConfiguration ignoreCollectionOrder(java.util.function.Predicate<java.lang.Object> ignoreCollectionOrderPredicate,10 void shouldDescribeCollectionOrderInFieldsForNestedObjectsInList() {11 Foo foo1 = new Foo(Arrays.asList("bar", "baz"));12 Foo foo2 = new Foo(Arrays.asList("baz", "bar"));13 Foo foo3 = new Foo(Arrays.asList("foo", "bar"));14 Foo foo4 = new Foo(Arrays.asList("bar", "foo"));15 Bar bar1 = new Bar(foo1);16 Bar bar2 = new Bar(foo2);17 Bar bar3 = new Bar(foo
describeIgnoredCollectionOrderInFields
Using AI Code Generation
1public RecursiveComparisonConfiguration ignoreCollectionOrder(boolean ignoreCollectionOrder)2public RecursiveComparisonConfiguration ignoreCollectionOrder(java.util.function.Predicate<java.lang.Object> ignoreCollectionOrderPredicate)3public RecursiveComparisonConfiguration ignoreCollectionOrder(java.util.function.Predicate<java.lang.Object> ignoreCollectionOrderPredicate,4public RecursiveComparisonConfiguration ignoreCollectionOrder(java.util.function.BiPredicate<java.lang.Object,java.lang.Object> ignoreCollectionOrderForElementsPredicate)5public RecursiveComparisonConfiguration ignoreCollectionOrder(java.util.function.Predicate<java.lang.Object> ignoreCollectionOrderPredicate,6public RecursiveComparisonConfiguration ignoreCollectionOrder(java.util.function.BiPredicate<java.lang.Object,java.lang.Object> ignoreCollectionOrderForElementsPredicate,7public RecursiveComparisonConfiguration ignoreCollectionOrder(java.util.function.Predicate<java.lang.Object> ignoreCollectionOrderPredicate,8public RecursiveComparisonConfiguration ignoreCollectionOrder(java.util.function.BiPredicate<java.lang.Object,java.lang.Object> ignoreCollectionOrderForElementsPredicate,9public RecursiveComparisonConfiguration ignoreCollectionOrder(java.util.function.Predicate<java.lang.Object> ignoreCollectionOrderPredicate,
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!!