How to use describeComparedFields method of org.assertj.core.api.recursive.comparison.RecursiveComparisonConfiguration class

Best Assertj code snippet using org.assertj.core.api.recursive.comparison.RecursiveComparisonConfiguration.describeComparedFields

Source:RecursiveComparisonConfiguration.java Github

copy

Full Screen

...555 StringBuilder description = new StringBuilder();556 describeIgnoreAllActualNullFields(description);557 describeIgnoreAllActualEmptyOptionalFields(description);558 describeIgnoreAllExpectedNullFields(description);559 describeComparedFields(description);560 describeIgnoredFields(description);561 describeIgnoredFieldsRegexes(description);562 describeIgnoredFieldsForTypes(description);563 describeOverriddenEqualsMethodsUsage(description, representation);564 describeIgnoreCollectionOrder(description);565 describeIgnoredCollectionOrderInFields(description);566 describeIgnoredCollectionOrderInFieldsMatchingRegexes(description);567 describeRegisteredComparatorByTypes(description);568 describeRegisteredComparatorForFields(description);569 describeTypeCheckingStrictness(description);570 describeRegisteredErrorMessagesForFields(description);571 describeRegisteredErrorMessagesForTypes(description);572 return description.toString();573 }574 boolean shouldIgnore(DualValue dualValue) {575 return shouldIgnoreFieldBasedOnFieldLocation(dualValue.fieldLocation) || shouldIgnoreFieldBasedOnFieldValue(dualValue);576 }577 private boolean shouldBeCompared(FieldLocation fieldLocation) {578 // empty comparedFields <=> no restriction on compared fields <=> must be compared579 if (comparedFields.isEmpty()) return true;580 return comparedFields.stream().anyMatch(matchesComparedField(fieldLocation));581 }582 private static Predicate<FieldLocation> matchesComparedField(FieldLocation field) {583 // a field f must be compared if any compared fields is f itself (obviously), a parent of f or a child of f.584 // - "name.first" must be compared if "name" is a compared field so will other "name" subfields like "name.last"585 // - "name" must be compared if "name.first" is a compared field otherwise "name" is ignored and "name.first" too586 return comparedField -> field.matches(comparedField) // exact match587 || field.hasParent(comparedField) // ex: field "name.first" and "name" compared field588 || field.hasChild(comparedField); // ex: field "name" and "name.first" compared field589 }590 Set<String> getActualFieldNamesToCompare(DualValue dualValue) {591 Set<String> actualFieldsNames = Objects.getFieldsNames(dualValue.actual.getClass());592 // we are doing the same as shouldIgnore(DualValue dualValue) but in two steps for performance reasons:593 // - we filter first ignored field by names that don't need building DualValues594 // - then we filter field DualValues with the remaining criteria that need to get the field value595 // DualValues are built introspecting fields which is expensive.596 return actualFieldsNames.stream()597 // evaluate field name ignoring criteria on dualValue field location + field name598 .filter(fieldName -> !shouldIgnoreFieldBasedOnFieldLocation(dualValue.fieldLocation.field(fieldName)))599 .map(fieldName -> dualValueForField(dualValue, fieldName))600 // evaluate field value ignoring criteria601 .filter(fieldDualValue -> !shouldIgnoreFieldBasedOnFieldValue(fieldDualValue))602 // back to field name603 .map(DualValue::getFieldName)604 .filter(fieldName -> !fieldName.isEmpty())605 .collect(toSet());606 }607 // non accessible stuff608 private boolean shouldIgnoreFieldBasedOnFieldValue(DualValue dualValue) {609 return matchesAnIgnoredNullField(dualValue)610 || matchesAnIgnoredFieldType(dualValue)611 || matchesAnIgnoredEmptyOptionalField(dualValue);612 }613 private boolean shouldIgnoreFieldBasedOnFieldLocation(FieldLocation fieldLocation) {614 return !shouldBeCompared(fieldLocation) || matchesAnIgnoredField(fieldLocation) || matchesAnIgnoredFieldRegex(fieldLocation);615 }616 private static DualValue dualValueForField(DualValue parentDualValue, String fieldName) {617 Object actualFieldValue = COMPARISON.getSimpleValue(fieldName, parentDualValue.actual);618 // no guarantees we have a field in expected named as fieldName619 Object expectedFieldValue;620 try {621 expectedFieldValue = COMPARISON.getSimpleValue(fieldName, parentDualValue.expected);622 } catch (@SuppressWarnings("unused") Exception e) {623 // set the field to null to express it is absent, this not 100% accurate as the value could be null624 // but it works to evaluate if dualValue should be ignored with matchesAnIgnoredFieldType625 expectedFieldValue = null;626 }627 FieldLocation fieldLocation = parentDualValue.fieldLocation.field(fieldName);628 return new DualValue(fieldLocation, actualFieldValue, expectedFieldValue);629 }630 boolean hasCustomComparator(DualValue dualValue) {631 String fieldName = dualValue.getConcatenatedPath();632 if (hasComparatorForField(fieldName)) return true;633 if (dualValue.actual == null && dualValue.expected == null) return false;634 // best effort assuming actual and expected have the same type (not 100% true as we can compare object of different types)635 Class<?> valueType = dualValue.actual != null ? dualValue.actual.getClass() : dualValue.expected.getClass();636 return hasComparatorForType(valueType);637 }638 boolean shouldIgnoreOverriddenEqualsOf(DualValue dualValue) {639 // we must compare java basic types otherwise the recursive comparison loops infinitely!640 if (dualValue.isActualJavaType()) return false;641 // enums don't have fields, comparing them field by field makes no sense, we need to use equals which is overridden and final642 if (dualValue.isActualAnEnum()) return false;643 return ignoreAllOverriddenEquals644 || matchesAnIgnoredOverriddenEqualsField(dualValue.fieldLocation)645 || (dualValue.actual != null && shouldIgnoreOverriddenEqualsOf(dualValue.actual.getClass()));646 }647 @VisibleForTesting648 boolean shouldIgnoreOverriddenEqualsOf(Class<?> clazz) {649 return matchesAnIgnoredOverriddenEqualsRegex(clazz) || matchesAnIgnoredOverriddenEqualsType(clazz);650 }651 boolean shouldIgnoreCollectionOrder(FieldLocation fieldLocation) {652 return ignoreCollectionOrder653 || matchesAnIgnoredCollectionOrderInField(fieldLocation)654 || matchesAnIgnoredCollectionOrderInFieldRegex(fieldLocation);655 }656 private void describeIgnoredFieldsRegexes(StringBuilder description) {657 if (!ignoredFieldsRegexes.isEmpty())658 description.append(format("- the fields matching the following regexes were ignored in the comparison: %s%n",659 describeRegexes(ignoredFieldsRegexes)));660 }661 private void describeIgnoredFields(StringBuilder description) {662 if (!ignoredFields.isEmpty())663 description.append(format("- the following fields were ignored in the comparison: %s%n", describeIgnoredFields()));664 }665 private void describeComparedFields(StringBuilder description) {666 if (!comparedFields.isEmpty())667 description.append(format("- the comparison was performed on the following fields: %s%n", describeComparedFields()));668 }669 private void describeIgnoredFieldsForTypes(StringBuilder description) {670 if (!ignoredTypes.isEmpty())671 description.append(format("- the following types were ignored in the comparison: %s%n", describeIgnoredTypes()));672 }673 private void describeIgnoreAllActualNullFields(StringBuilder description) {674 if (ignoreAllActualNullFields) description.append(format("- all actual null fields were ignored in the comparison%n"));675 }676 private void describeIgnoreAllActualEmptyOptionalFields(StringBuilder description) {677 if (getIgnoreAllActualEmptyOptionalFields())678 description.append(format("- all actual empty optional fields were ignored in the comparison (including Optional, OptionalInt, OptionalLong and OptionalDouble)%n"));679 }680 private void describeIgnoreAllExpectedNullFields(StringBuilder description) {681 if (ignoreAllExpectedNullFields) description.append(format("- all expected null fields were ignored in the comparison%n"));682 }683 private void describeOverriddenEqualsMethodsUsage(StringBuilder description, Representation representation) {684 String header = ignoreAllOverriddenEquals685 ? "- no overridden equals methods were used in the comparison (except for java types)"686 : "- overridden equals methods were used in the comparison";687 description.append(header);688 if (isConfiguredToIgnoreSomeButNotAllOverriddenEqualsMethods()) {689 description.append(format(" except for:%n"));690 describeIgnoredOverriddenEqualsMethods(description, representation);691 } else {692 description.append(format("%n"));693 }694 }695 private void describeIgnoredOverriddenEqualsMethods(StringBuilder description, Representation representation) {696 if (!ignoredOverriddenEqualsForFields.isEmpty())697 description.append(format("%s the following fields: %s%n", INDENT_LEVEL_2,698 describeIgnoredOverriddenEqualsForFields()));699 if (!ignoredOverriddenEqualsForTypes.isEmpty())700 description.append(format("%s the following types: %s%n", INDENT_LEVEL_2,701 describeIgnoredOverriddenEqualsForTypes(representation)));702 if (!ignoredOverriddenEqualsForFieldsMatchingRegexes.isEmpty())703 description.append(format("%s the types matching the following regexes: %s%n", INDENT_LEVEL_2,704 describeRegexes(ignoredOverriddenEqualsForFieldsMatchingRegexes)));705 }706 private String describeIgnoredOverriddenEqualsForTypes(Representation representation) {707 List<String> fieldsDescription = ignoredOverriddenEqualsForTypes.stream()708 .map(representation::toStringOf)709 .collect(toList());710 return join(fieldsDescription);711 }712 private String describeIgnoredOverriddenEqualsForFields() {713 return join(ignoredOverriddenEqualsForFields);714 }715 private void describeIgnoreCollectionOrder(StringBuilder description) {716 if (ignoreCollectionOrder) description.append(format("- collection order was ignored in all fields in the comparison%n"));717 }718 private void describeIgnoredCollectionOrderInFields(StringBuilder description) {719 if (!ignoredCollectionOrderInFields.isEmpty())720 description.append(format("- collection order was ignored in the following fields in the comparison: %s%n",721 describeIgnoredCollectionOrderInFields()));722 }723 private void describeIgnoredCollectionOrderInFieldsMatchingRegexes(StringBuilder description) {724 if (!ignoredCollectionOrderInFieldsMatchingRegexes.isEmpty())725 description.append(format("- collection order was ignored in the fields matching the following regexes in the comparison: %s%n",726 describeRegexes(ignoredCollectionOrderInFieldsMatchingRegexes)));727 }728 private boolean matchesAnIgnoredOverriddenEqualsRegex(Class<?> clazz) {729 if (ignoredOverriddenEqualsForFieldsMatchingRegexes.isEmpty()) return false; // shortcut730 String canonicalName = clazz.getCanonicalName();731 return ignoredOverriddenEqualsForFieldsMatchingRegexes.stream().anyMatch(regex -> regex.matcher(canonicalName).matches());732 }733 private boolean matchesAnIgnoredOverriddenEqualsType(Class<?> clazz) {734 return ignoredOverriddenEqualsForTypes.contains(clazz);735 }736 private boolean matchesAnIgnoredOverriddenEqualsField(FieldLocation fieldLocation) {737 return ignoredOverriddenEqualsForFields.stream().anyMatch(fieldLocation::matches);738 }739 private boolean matchesAnIgnoredNullField(DualValue dualValue) {740 return (ignoreAllActualNullFields && dualValue.actual == null)741 || (ignoreAllExpectedNullFields && dualValue.expected == null);742 }743 private boolean matchesAnIgnoredEmptyOptionalField(DualValue dualValue) {744 return ignoreAllActualEmptyOptionalFields745 && dualValue.isActualFieldAnEmptyOptionalOfAnyType();746 }747 private boolean matchesAnIgnoredFieldRegex(FieldLocation fieldLocation) {748 return ignoredFieldsRegexes.stream()749 .anyMatch(regex -> regex.matcher(fieldLocation.getPathToUseInRules()).matches());750 }751 private boolean matchesAnIgnoredFieldType(DualValue dualValue) {752 Object actual = dualValue.actual;753 if (actual != null) return ignoredTypes.contains(actual.getClass());754 Object expected = dualValue.expected;755 // actual is null => we can't evaluate its type, we can only reliably check dualValue.expected's type if756 // strictTypeChecking is enabled which guarantees expected is of the same type.757 if (strictTypeChecking && expected != null) return ignoredTypes.contains(expected.getClass());758 // if strictTypeChecking is disabled, we can't safely ignore the field (if we did, we would ignore all null fields!).759 return false;760 }761 private boolean matchesAnIgnoredField(FieldLocation fieldLocation) {762 return ignoredFields.stream().anyMatch(fieldLocation::matches);763 }764 private boolean matchesAnIgnoredCollectionOrderInField(FieldLocation fieldLocation) {765 return ignoredCollectionOrderInFields.stream().anyMatch(fieldLocation::matches);766 }767 private boolean matchesAnIgnoredCollectionOrderInFieldRegex(FieldLocation fieldLocation) {768 String pathToUseInRules = fieldLocation.getPathToUseInRules();769 return ignoredCollectionOrderInFieldsMatchingRegexes.stream().anyMatch(regex -> regex.matcher(pathToUseInRules).matches());770 }771 private String describeIgnoredFields() {772 return join(ignoredFields);773 }774 private String describeComparedFields() {775 return join(comparedFields.stream().map(FieldLocation::shortDescription).collect(toList()));776 }777 private String describeIgnoredTypes() {778 List<String> typesDescription = ignoredTypes.stream()779 .map(Class::getName)780 .collect(toList());781 return join(typesDescription);782 }783 private static String join(Collection<String> typesDescription) {784 return Strings.join(typesDescription).with(DEFAULT_DELIMITER);785 }786 private String describeIgnoredCollectionOrderInFields() {787 return join(ignoredCollectionOrderInFields);788 }...

Full Screen

Full Screen

describeComparedFields

Using AI Code Generation

copy

Full Screen

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 java.util.ArrayList;5import java.util.List;6import org.assertj.core.api.recursive.comparison.RecursiveComparisonConfiguration;7import org.junit.jupiter.api.Test;8class RecursiveComparisonTest {9 void test() {10 RecursiveComparisonConfiguration config = builder().withIgnoredFields("id").build();11 List<Child> children = new ArrayList<>();12 children.add(new Child(1, "child1"));13 children.add(new Child(2, "child2"));14 Parent parent1 = new Parent(1, "parent1", children);15 children = new ArrayList<>();16 children.add(new Child(1, "child1"));17 children.add(new Child(2, "child2"));18 Parent parent2 = new Parent(2, "parent2", children);19 assertThat(parent1).usingRecursiveComparison(config).isEqualTo(parent2);20 }21 void test2() {22 RecursiveComparisonConfiguration config = builder().withIgnoredFields("id").build();23 List<Child> children = new ArrayList<>();24 children.add(new Child(1, "child1"));25 children.add(new Child(2, "child2"));26 Parent parent1 = new Parent(1, "parent1", children);27 children = new ArrayList<>();28 children.add(new Child(1, "child1"));29 children.add(new Child(2, "child2"));30 Parent parent2 = new Parent(2, "parent2", children);31 assertThat(parent1).usingRecursiveComparison(config).isEqualTo(parent2);32 }33 void test3() {34 RecursiveComparisonConfiguration config = builder().withIgnoredFields("id").build();35 List<Child> children = new ArrayList<>();36 children.add(new Child(1, "child1"));37 children.add(new Child(2, "child2"));38 Parent parent1 = new Parent(1, "parent1", children);39 children = new ArrayList<>();40 children.add(new Child(1, "child1"));41 children.add(new Child(2, "child2"));42 Parent parent2 = new Parent(2, "parent2", children

Full Screen

Full Screen

describeComparedFields

Using AI Code Generation

copy

Full Screen

1import org.assertj.core.api.recursive.comparison.RecursiveComparisonConfiguration;2import org.junit.Test;3import java.util.Arrays;4import java.util.List;5import static org.assertj.core.api.Assertions.assertThat;6public class TestRecursiveComparisonConfiguration {7 public void testRecursiveComparisonConfiguration() {8 List<String> list1 = Arrays.asList("a", "b", "c");9 List<String> list2 = Arrays.asList("a", "b", "c");10 assertThat(list1).usingRecursiveComparison()11 .withConfiguration(c -> c.describeComparedFields())12 .isEqualTo(list2);13 }14}15import org.assertj.core.api.recursive.comparison.RecursiveComparisonConfiguration;16import org.junit.Test;17import java.util.Arrays;18import java.util.List;19import static org.assertj.core.api.Assertions.assertThat;20public class TestRecursiveComparisonConfiguration {21 public void testRecursiveComparisonConfiguration() {22 List<String> list1 = Arrays.asList("a", "b", "c");23 List<String> list2 = Arrays.asList("a", "b", "c");24 assertThat(list1).usingRecursiveComparison()25 .withConfiguration(c -> c.describeComparedFields()26 .withPathFormatter(RecursiveComparisonConfiguration

Full Screen

Full Screen

describeComparedFields

Using AI Code Generation

copy

Full Screen

1import org.assertj.core.api.recursive.comparison.RecursiveComparisonConfiguration;2RecursiveComparisonConfiguration config = new RecursiveComparisonConfiguration();3config.describeComparedFields("name", "age");4assertThat(actual).usingRecursiveComparison(config).isEqualTo(expected);5 <Person(name=John, age=25)>6 <Person(name=John, age=26)>7import org.assertj.core.api.recursive.comparison.RecursiveComparisonConfiguration;8RecursiveComparisonConfiguration config = new RecursiveComparisonConfiguration();9config.ignoreAllOverriddenEquals();10assertThat(actual).usingRecursiveComparison(config).isEqualTo(expected);11 <Person(name=John, age=25)>12 <Person(name=John, age=26)>13import org.assertj.core.api.recursive.comparison.RecursiveComparisonConfiguration;14RecursiveComparisonConfiguration config = new RecursiveComparisonConfiguration();15config.ignoreCollectionOrder(true);16assertThat(actual).usingRecursiveComparison(config).isEqualTo(expected);17 <Person(name=John, age=25)>18 <Person(name=John, age=26)>

Full Screen

Full Screen

Automation Testing Tutorials

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.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Assertj automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Most used method in RecursiveComparisonConfiguration

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful