How to use withEqualsForType method of org.assertj.core.api.RecursiveComparisonAssert class

Best Assertj code snippet using org.assertj.core.api.RecursiveComparisonAssert.withEqualsForType

Source:RecursiveComparisonConfiguration.java Github

copy

Full Screen

...340 * or comparators registered with {@link #registerComparatorForFields(Comparator, String...)}.341 * <p>342 * Note that registering a {@link BiPredicate} for a given type will override the previously registered BiPredicate/Comparator (if any).343 * <p>344 * See {@link RecursiveComparisonAssert#withEqualsForType(BiPredicate, Class)} for examples.345 *346 * @param <T> the class type to register a comparator for347 * @param equals the equals implementation to compare the given type348 * @param type the type to be compared with the given equals implementation .349 * @throws NullPointerException if the given BiPredicate is null.350 * @since 3.17.0351 */352 @SuppressWarnings("unchecked")353 public <T> void registerEqualsForType(BiPredicate<? super T, ? super T> equals, Class<T> type) {354 registerComparatorForType(toComparator(equals), type);355 }356 /**357 * Registers the given {@link Comparator} to compare the fields at the given locations.358 * <p>359 * The fields must be specified from the root object, for example if {@code Foo} has a {@code Bar} field and both have an {@code id} field,360 * one can register a comparator for Foo and Bar's {@code id} by calling:361 * <pre><code class='java'> registerComparatorForFields(idComparator, "foo.id", "foo.bar.id")</code></pre>362 * <p>363 * Comparators registered with this method have precedence over comparators registered with {@link #registerComparatorForType(Comparator, Class)}.364 * <p>365 * Note that registering a {@link Comparator} for a given field will override the previously registered BiPredicate/Comparator (if any).366 * <p>367 * See {@link RecursiveComparisonAssert#withComparatorForFields(Comparator, String...) RecursiveComparisonAssert#withComparatorForFields(Comparator, String...)} for examples.368 *369 * @param comparator the {@link java.util.Comparator Comparator} to use to compare the given field370 * @param fieldLocations the locations from the root object of the fields the comparator should be used for371 * @throws NullPointerException if the given comparator is null.372 */373 public void registerComparatorForFields(Comparator<?> comparator, String... fieldLocations) {374 requireNonNull(comparator, "Expecting a non null Comparator");375 Stream.of(fieldLocations).forEach(fieldLocation -> fieldComparators.registerComparator(fieldLocation, comparator));376 }377 /**378 * Registers the given {@link BiPredicate} to compare the fields at the given locations.379 * <p>380 * The fields must be specified from the root object, for example if {@code Foo} has a {@code Bar} field and both have an {@code id} field,381 * one can register a BiPredicate for Foo and Bar's {@code id} by calling:382 * <pre><code class='java'> registerEqualsForFields(idBiPredicate, "foo.id", "foo.bar.id")</code></pre>383 * <p>384 * BiPredicates registered with this method have precedence over the ones registered with {@link #registerEqualsForType(BiPredicate, Class)}385 * or the comparators registered with {@link #registerComparatorForType(Comparator, Class)}.386 * <p>387 * Note that registering a {@link BiPredicate} for a given field will override the previously registered BiPredicate/Comparator (if any).388 * <p>389 * See {@link RecursiveComparisonAssert#withEqualsForFields(BiPredicate, String...) RecursiveComparisonAssert#withEqualsForFields(BiPredicate, String...)} for examples.390 *391 * @param equals the equals implementation to compare the given fields.392 * @param fieldLocations the locations from the root object of the fields the comparator should be used for393 * @throws NullPointerException if the given BiPredicate is null.394 * @since 3.17.0395 */396 public void registerEqualsForFields(BiPredicate<?, ?> equals, String... fieldLocations) {397 registerComparatorForFields(toComparator(equals), fieldLocations);398 }399 /**400 * Sets whether the recursive comparison will check that actual's type is compatible with expected's type (the same applies for each field).401 * Compatible means that the expected's type is the same or a subclass of actual's type.402 * <p>403 * See {@link RecursiveComparisonAssert#withStrictTypeChecking()} for code examples.404 *405 * @param strictTypeChecking whether the recursive comparison will check that actual's type is compatible with expected's type.406 */407 public void strictTypeChecking(boolean strictTypeChecking) {408 this.strictTypeChecking = strictTypeChecking;409 }410 public boolean isInStrictTypeCheckingMode() {411 return strictTypeChecking;412 }413 public List<Pattern> getIgnoredFieldsRegexes() {414 return ignoredFieldsRegexes;415 }416 public List<Class<?>> getIgnoredOverriddenEqualsForTypes() {417 return ignoredOverriddenEqualsForTypes;418 }419 public List<String> getIgnoredOverriddenEqualsForFields() {420 return ignoredOverriddenEqualsForFields;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}.729 * @return created builder730 */731 public static Builder builder() {732 return new Builder();733 }734 /**735 * Builder to build {@link RecursiveComparisonConfiguration}.736 */737 public static final class Builder {738 private boolean strictTypeChecking;739 private boolean ignoreAllActualNullFields;740 private boolean ignoreAllActualEmptyOptionalFields;741 private boolean ignoreAllExpectedNullFields;742 private String[] ignoredFields = {};743 private String[] ignoredFieldsMatchingRegexes = {};744 private Class<?>[] ignoredTypes = {};745 private Class<?>[] ignoredOverriddenEqualsForTypes = {};746 private String[] ignoredOverriddenEqualsForFields = {};747 private String[] ignoredOverriddenEqualsForFieldsMatchingRegexes = {};748 private boolean ignoreAllOverriddenEquals;749 private boolean ignoreCollectionOrder;750 private String[] ignoredCollectionOrderInFields = {};751 private String[] ignoredCollectionOrderInFieldsMatchingRegexes = {};752 private TypeComparators typeComparators = new TypeComparators();753 private FieldComparators fieldComparators = new FieldComparators();754 private Builder() {}755 /**756 * Sets whether the recursive comparison will check that actual's type is compatible with expected's type (the same applies for each field).757 * Compatible means that the expected's type is the same or a subclass of actual's type.758 * <p>759 * See {@link RecursiveComparisonAssert#withStrictTypeChecking()} for code examples.760 *761 * @param strictTypeChecking whether the recursive comparison will check that actual's type is compatible with expected's type.762 * @return this builder.763 */764 public Builder withStrictTypeChecking(boolean strictTypeChecking) {765 this.strictTypeChecking = strictTypeChecking;766 return this;767 }768 /**769 * Sets whether actual null fields are ignored in the recursive comparison.770 * <p>771 * See {@link RecursiveComparisonAssert#ignoringActualNullFields()} for code examples.772 *773 * @param ignoreAllActualNullFields whether to ignore actual null fields in the recursive comparison774 * @return this builder.775 */776 public Builder withIgnoreAllActualNullFields(boolean ignoreAllActualNullFields) {777 this.ignoreAllActualNullFields = ignoreAllActualNullFields;778 return this;779 }780 /**781 * Sets whether actual empty optional fields are ignored in the recursive comparison.782 * <p>783 * See {@link RecursiveComparisonAssert#ignoringActualEmptyOptionalFields()} for code examples.784 *785 * @param ignoreAllActualEmptyOptionalFields whether to ignore actual empty optional fields in the recursive comparison786 * @return this builder.787 */788 public Builder withIgnoreAllActualEmptyOptionalFields(boolean ignoreAllActualEmptyOptionalFields) {789 this.ignoreAllActualEmptyOptionalFields = ignoreAllActualEmptyOptionalFields;790 return this;791 }792 /**793 * Sets whether expected null fields are ignored in the recursive comparison.794 * <p>795 * See {@link RecursiveComparisonAssert#ignoringExpectedNullFields()} for code examples.796 *797 * @param ignoreAllExpectedNullFields whether to ignore expected null fields in the recursive comparison798 * @return this builder.799 */800 public Builder withIgnoreAllExpectedNullFields(boolean ignoreAllExpectedNullFields) {801 this.ignoreAllExpectedNullFields = ignoreAllExpectedNullFields;802 return this;803 }804 /**805 * Adds the given fields to the list of the object under test fields to ignore in the recursive comparison. Nested fields can be specified like this: home.address.street.806 * <p>807 * See {@link RecursiveComparisonAssert#ignoringFields(String...) RecursiveComparisonAssert#ignoringFields(String...)} for examples.808 *809 * @param fieldsToIgnore the fields of the object under test to ignore in the comparison.810 * @return this builder.811 */812 public Builder withIgnoredFields(String... fieldsToIgnore) {813 this.ignoredFields = fieldsToIgnore;814 return this;815 }816 /**817 * Allows to ignore in the recursive comparison the object under test fields matching the given regexes. The given regexes are added to the already registered ones.818 * <p>819 * See {@link RecursiveComparisonAssert#ignoringFieldsMatchingRegexes(String...) RecursiveComparisonAssert#ignoringFieldsMatchingRegexes(String...)} for examples.820 *821 * @param regexes regexes used to ignore fields in the comparison.822 * @return this builder.823 */824 public Builder withIgnoredFieldsMatchingRegexes(String... regexes) {825 this.ignoredFieldsMatchingRegexes = regexes;826 return this;827 }828 /**829 * Adds the given types to the list of the object under test fields types to ignore in the recursive comparison.830 * The fields are ignored if their types exactly match one of the ignored types, if a field is a subtype of an ignored type it won't be ignored.831 * <p>832 * Note that if some object under test fields are null, they are not ignored by this method as their type can't be evaluated.833 * <p>834 * See {@link RecursiveComparisonAssert#ignoringFields(String...) RecursiveComparisonAssert#ignoringFieldsOfTypes(Class...)} for examples.835 *836 * @param types the types of the object under test to ignore in the comparison.837 * @return this builder.838 */839 public Builder withIgnoredFieldsOfTypes(Class<?>... types) {840 this.ignoredTypes = types;841 return this;842 }843 /**844 * Adds the given types to the list of types to force a recursive comparison on.845 * <p>846 * See {@link RecursiveComparisonAssert#ignoringOverriddenEqualsForTypes(Class...) RecursiveComparisonAssert#ignoringOverriddenEqualsForTypes(Class...)} for examples.847 *848 * @param types the types to the list of types to force a recursive comparison on.849 * @return this builder.850 */851 public Builder withIgnoredOverriddenEqualsForTypes(Class<?>... types) {852 this.ignoredOverriddenEqualsForTypes = types;853 return this;854 }855 /**856 * Adds the given fields to the list of fields to force a recursive comparison on.857 * <p>858 * See {@link RecursiveComparisonAssert#ignoringOverriddenEqualsForFields(String...) RecursiveComparisonAssert#ignoringOverriddenEqualsForFields(String...)} for examples.859 *860 * @param fields the fields to force a recursive comparison on.861 * @return this builder.862 */863 public Builder withIgnoredOverriddenEqualsForFields(String... fields) {864 this.ignoredOverriddenEqualsForFields = fields;865 return this;866 }867 /**868 * Adds the given regexes to the list of regexes used find the fields to force a recursive comparison on.869 * <p>870 * See {@link RecursiveComparisonAssert#ignoringOverriddenEqualsForFieldsMatchingRegexes(String...) RecursiveComparisonAssert#ignoringOverriddenEqualsForFieldsMatchingRegexes(String...)} for examples.871 *872 * @param regexes regexes used to specify the fields we want to force a recursive comparison on.873 * @return this builder.874 */875 public Builder withIgnoredOverriddenEqualsForFieldsMatchingRegexes(String... regexes) {876 this.ignoredOverriddenEqualsForFieldsMatchingRegexes = regexes;877 return this;878 }879 /**880 * Force a recursive comparison on all fields (except java types) if true.881 * <p>882 * See {@link RecursiveComparisonAssert#ignoringAllOverriddenEquals()} for examples.883 *884 * @param ignoreAllOverriddenEquals whether to force a recursive comparison on all fields (except java types) or not.885 * @return this builder.886 */887 public Builder withIgnoreAllOverriddenEquals(boolean ignoreAllOverriddenEquals) {888 this.ignoreAllOverriddenEquals = ignoreAllOverriddenEquals;889 return this;890 }891 /**892 * Sets whether to ignore collection order in the comparison.893 * <p>894 * See {@link RecursiveComparisonAssert#ignoringCollectionOrder()} for code examples.895 *896 * @param ignoreCollectionOrder whether to ignore collection order in the comparison.897 * @return this builder.898 */899 public Builder withIgnoreCollectionOrder(boolean ignoreCollectionOrder) {900 this.ignoreCollectionOrder = ignoreCollectionOrder;901 return this;902 }903 /**904 * Adds the given fields to the list of the object under test fields to ignore collection order in the recursive comparison.905 * <p>906 * See {@link RecursiveComparisonAssert#ignoringCollectionOrderInFields(String...) RecursiveComparisonAssert#ignoringCollectionOrderInFields(String...)} for examples.907 *908 * @param fieldsToIgnoreCollectionOrder the fields of the object under test to ignore collection order in the comparison.909 * @return this builder.910 */911 public Builder withIgnoredCollectionOrderInFields(String... fieldsToIgnoreCollectionOrder) {912 this.ignoredCollectionOrderInFields = fieldsToIgnoreCollectionOrder;913 return this;914 }915 /**916 * Adds the given regexes to the list of regexes used to find the object under test fields to ignore collection order in the recursive comparison.917 * <p>918 * See {@link RecursiveComparisonAssert#ignoringCollectionOrderInFieldsMatchingRegexes(String...) RecursiveComparisonAssert#ignoringCollectionOrderInFieldsMatchingRegexes(String...)} for examples.919 *920 * @param regexes regexes used to find the object under test fields to ignore collection order in in the comparison.921 * @return this builder.922 */923 public Builder withIgnoredCollectionOrderInFieldsMatchingRegexes(String... regexes) {924 this.ignoredCollectionOrderInFieldsMatchingRegexes = regexes;925 return this;926 }927 /**928 * Registers the given {@link Comparator} to compare the fields with the given type.929 * <p>930 * Comparators registered with this method have less precedence than comparators registered with {@link #withComparatorForFields(Comparator, String...)}931 * or BiPredicate registered with {@link #withEqualsForFields(BiPredicate, String...)}.932 * <p>933 * Note that registering a {@link Comparator} for a given type will override the previously registered BiPredicate/Comparator (if any).934 * <p>935 * See {@link RecursiveComparisonAssert#withComparatorForType(Comparator, Class)} for examples.936 *937 * @param <T> the class type to register a comparator for938 * @param comparator the {@link java.util.Comparator Comparator} to use to compare the given field939 * @param type the type to be compared with the given comparator.940 * @return this builder.941 * @throws NullPointerException if the given Comparator is null.942 */943 public <T> Builder withComparatorForType(Comparator<? super T> comparator, Class<T> type) {944 requireNonNull(comparator, "Expecting a non null Comparator");945 this.typeComparators.put(type, comparator);946 return this;947 }948 /**949 * Registers the given {@link BiPredicate} to compare the fields with the given type.950 * <p>951 * BiPredicates registered with this method have less precedence than the ones registered with {@link #withEqualsForFields(BiPredicate, String...)}952 * or the comparators registered with {@link #withComparatorForFields(Comparator, String...)}.953 * <p>954 * Note that registering a {@link BiPredicate} for a given type will override the previously registered BiPredicate/Comparator (if any).955 * <p>956 * See {@link RecursiveComparisonAssert#withEqualsForType(BiPredicate, Class)} for examples.957 *958 * @param <T> the class type to register a BiPredicate for959 * @param equals the {@link BiPredicate} to use to compare the given field960 * @param type the type to be compared with the given comparator.961 * @return this builder.962 * @since 3.17.0963 * @throws NullPointerException if the given BiPredicate is null.964 */965 @SuppressWarnings("unchecked")966 public <T> Builder withEqualsForType(BiPredicate<? super T, ? super T> equals, Class<T> type) {967 return withComparatorForType(toComparator(equals), type);968 }969 /**970 * Registers the given {@link Comparator} to compare the fields at the given locations.971 * <p>972 * The fields must be specified from the root object, for example if {@code Foo} has a {@code Bar} field and both have an {@code id} field,973 * one can register a comparator for Foo and Bar's {@code id} by calling:974 * <pre><code class='java'> registerComparatorForFields(idComparator, "foo.id", "foo.bar.id")</code></pre>975 * <p>976 * Comparators registered with this method have precedence over comparators registered with {@link #withComparatorForType(Comparator, Class)}977 * or BiPredicate registered with {@link #withEqualsForType(BiPredicate, Class)}.978 * <p>979 * Note that registering a {@link Comparator} for a given field will override the previously registered BiPredicate/Comparator (if any).980 * <p>981 * See {@link RecursiveComparisonAssert#withComparatorForFields(Comparator, String...) RecursiveComparisonAssert#withComparatorForFields(Comparator comparator, String...fields)} for examples.982 *983 * @param comparator the {@link java.util.Comparator Comparator} to use to compare the given field984 * @param fields the fields the comparator should be used for985 * @return this builder.986 * @throws NullPointerException if the given Comparator is null.987 */988 public Builder withComparatorForFields(Comparator<?> comparator, String... fields) {989 requireNonNull(comparator, "Expecting a non null Comparator");990 Stream.of(fields).forEach(fieldLocation -> fieldComparators.registerComparator(fieldLocation, comparator));991 return this;992 }993 /**994 * Registers the given {@link BiPredicate} to compare the fields at the given locations.995 * <p>996 * The fields must be specified from the root object, for example if {@code Foo} has a {@code Bar} field and both have an {@code id} field,997 * one can register a BiPredicate for Foo and Bar's {@code id} by calling:998 * <pre><code class='java'> withEqualsForFields(idBiPredicate, "foo.id", "foo.bar.id")</code></pre>999 * <p>1000 * BiPredicates registered with this method have precedence over the ones registered with {@link #withEqualsForType(BiPredicate, Class)}1001 * or the comparators registered with {@link #withComparatorForType(Comparator, Class)}.1002 * <p>1003 * Note that registering a {@link BiPredicate} for a given field will override the previously registered BiPredicate/Comparator (if any).1004 * <p>1005 * See {@link RecursiveComparisonAssert#withEqualsForFields(BiPredicate, String...) RecursiveComparisonAssert#withEqualsForFields(BiPredicate equals, String...fields)} for examples.1006 *1007 * @param equals the {@link BiPredicate} to use to compare the given fields1008 * @param fields the fields the BiPredicate should be used for1009 * @return this builder.1010 * @since 3.17.01011 * @throws NullPointerException if the given BiPredicate is null.1012 */1013 public Builder withEqualsForFields(BiPredicate<?, ?> equals, String... fields) {1014 return withComparatorForFields(toComparator(equals), fields);...

Full Screen

Full Screen

Source:RecursiveComparisonAssert.java Github

copy

Full Screen

...88 * By default {@code floats} are compared with a precision of 1.0E-6 and {@code doubles} with 1.0E-15.89 * <p>90 * You can specify a custom comparator or equals BiPredicate per (nested) fields or type with the methods below (but before calling {@code isEqualTo} otherwise this has no effect!):91 * <ol>92 * <li> {@link #withEqualsForType(BiPredicate, Class)} for a given type</li>93 * <li> {@link #withComparatorForType(Comparator, Class)} for a given type</li>94 * <li> {@link #withComparatorForFields(Comparator, String...) withComparatorForFields(Comparator, String...)} for one or multiple fields</li>95 * <li> {@link #withComparatorForFields(Comparator, String...) withComparatorForFields(Comparator, String...)} for one or multiple fields</li>96 * </ol>97 * <p>98 * Note that field comparators always take precedence over type comparators.99 * <p>100 * <strong>Example</strong>101 * <p>102 * Here is a basic example with a default {@link RecursiveComparisonConfiguration}, you can find other examples for each of the method changing the recursive comparison behavior103 * like {@link #ignoringFields(String...)}.104 * <pre><code class='java'> public class Person {105 * String name;106 * double height;107 * Home home = new Home();108 * }109 *110 * public class Home {111 * Address address = new Address();112 * Date ownedSince;113 * }114 *115 * public static class Address {116 * int number;117 * String street;118 * }119 *120 * Person sherlock = new Person("Sherlock", 1.80);121 * sherlock.home.ownedSince = new Date(123);122 * sherlock.home.address.street = "Baker Street";123 * sherlock.home.address.number = 221;124 *125 * Person sherlock2 = new Person("Sherlock", 1.80);126 * sherlock2.home.ownedSince = new Date(123);127 * sherlock2.home.address.street = "Baker Street";128 * sherlock2.home.address.number = 221;129 *130 * // assertion succeeds as the data of both objects are the same.131 * assertThat(sherlock).usingRecursiveComparison()132 * .isEqualTo(sherlock2);</code></pre>133 *134 * @param expected the object to compare {@code actual} to.135 * @return {@code this} assertion object.136 * @throws AssertionError if the actual object is {@code null}.137 * @throws AssertionError if the actual and the given objects are not deeply equal property/field by property/field.138 * @throws IntrospectionError if one property/field to compare can not be found.139 */140 @Override141 public SELF isEqualTo(Object expected) {142 // deals with both actual and expected being null143 if (actual == expected) return myself;144 if (expected == null) {145 // for the assertion to pass, actual must be null but this is not the case since actual != expected146 // => we fail expecting actual to be null147 objects.assertNull(info, actual);148 }149 // at this point expected is not null, which means actual must not be null for the assertion to pass150 objects.assertNotNull(info, actual);151 // at this point both actual and expected are not null, we can compare them recursively!152 List<ComparisonDifference> differences = determineDifferencesWith(expected);153 if (!differences.isEmpty()) throw objects.getFailures().failure(info, shouldBeEqualByComparingFieldByFieldRecursively(actual,154 expected,155 differences,156 recursiveComparisonConfiguration,157 info.representation()));158 return myself;159 }160 /**161 * Asserts that actual object is not equal to the given object based on a recursive property/field by property/field comparison162 * (including inherited ones).163 * <p>164 * This is typically useful when actual's {@code equals} was not overridden.165 * <p>166 * The comparison is <b>not symmetrical</b> since it is <b>limited to actual's fields</b>, the algorithm gather all167 * actual's fields and then compare them to the corresponding expected's fields.<br>168 * It is then possible for the expected object to have more fields than actual which is handy when comparing169 * a base type to a subtype.170 * <p>171 * This method is based on {@link #isEqualTo(Object)}, you can check out more usages in that method.172 * <p>173 * Example174 * <pre><code class='java'> // equals not overridden in TolkienCharacter175 * TolkienCharacter frodo = new TolkienCharacter("Frodo", 33, HOBBIT);176 * TolkienCharacter frodoClone = new TolkienCharacter("Frodo", 33, HOBBIT);177 * TolkienCharacter youngFrodo = new TolkienCharacter("Frodo", 22, HOBBIT);178 *179 * // Pass as equals compares object references180 * assertThat(frodo).isNotEqualTo(frodoClone);181 *182 * // Fail as frodo and frodoClone are equals when doing a field by field comparison.183 * assertThat(frodo).usingRecursiveComparison()184 * .isNotEqualTo(frodoClone);185 *186 * // Pass as one the age fields differ between frodo and youngFrodo.187 * assertThat(frodo).usingRecursiveComparison()188 * .isNotEqualTo(youngFrodo);</code></pre>189 *190 * @param other the object to compare {@code actual} to.191 * @return {@code this} assertions object192 * @throws AssertionError if the actual object and the given objects are both {@code null}.193 * @throws AssertionError if the actual and the given objects are equals property/field by property/field recursively.194 * @see #isEqualTo(Object)195 * @since 3.17.0196 */197 @Override198 public SELF isNotEqualTo(Object other) {199 if (actual == other) throw objects.getFailures().failure(info,200 shouldNotBeEqualComparingFieldByFieldRecursively(actual, other,201 recursiveComparisonConfiguration,202 info.representation()));203 if (other != null && actual != null) {204 List<ComparisonDifference> differences = determineDifferencesWith(other);205 if (differences.isEmpty())206 throw objects.getFailures().failure(info,207 shouldNotBeEqualComparingFieldByFieldRecursively(actual, other,208 recursiveComparisonConfiguration,209 info.representation()));210 }211 // either one of actual or other was null (but not both) or there were no differences212 return myself;213 }214 /**215 * Makes the recursive comparison to ignore all <b>actual null fields</b> (but note that the expected object null fields are used in the comparison).216 * <p>217 * Example:218 * <pre><code class='java'> public class Person {219 * String name;220 * double height;221 * Home home = new Home();222 * }223 *224 * public class Home {225 * Address address = new Address();226 * }227 *228 * public static class Address {229 * int number;230 * String street;231 * }232 *233 * Person noName = new Person(null, 1.80);234 * noName.home.address.street = null;235 * noName.home.address.number = 221;236 *237 * Person sherlock = new Person("Sherlock", 1.80);238 * sherlock.home.address.street = "Baker Street";239 * sherlock.home.address.number = 221;240 *241 * // assertion succeeds as name and home.address.street fields are ignored in the comparison242 * assertThat(noName).usingRecursiveComparison()243 * .ignoringActualNullFields()244 * .isEqualTo(sherlock);245 *246 * // assertion fails as name and home.address.street fields are populated for sherlock but not for noName.247 * assertThat(sherlock).usingRecursiveComparison()248 * .ignoringActualNullFields()249 * .isEqualTo(noName);</code></pre>250 *251 * @return this {@link RecursiveComparisonAssert} to chain other methods.252 */253 @CheckReturnValue254 public SELF ignoringActualNullFields() {255 recursiveComparisonConfiguration.setIgnoreAllActualNullFields(true);256 return myself;257 }258 /**259 * Makes the recursive comparison to ignore all <b>actual empty optional fields</b> (including {@link Optional}, {@link OptionalInt}, {@link OptionalLong} and {@link OptionalDouble}),260 * note that the expected object empty optional fields are not ignored, this only applies to actual's fields.261 * <p>262 * Example:263 * <pre><code class='java'> public class Person {264 * String name;265 * OptionalInt age;266 * OptionalLong id;267 * OptionalDouble height;268 * Home home = new Home();269 * }270 *271 * public class Home {272 * String address;273 * Optional&lt;String&gt; phone;274 * }275 *276 * Person homerWithoutDetails = new Person("Homer Simpson");277 * homerWithoutDetails.home.address.street = "Evergreen Terrace";278 * homerWithoutDetails.home.address.number = 742;279 * homerWithoutDetails.home.phone = Optional.empty();280 * homerWithoutDetails.age = OptionalInt.empty();281 * homerWithoutDetails.id = OptionalLong.empty();282 * homerWithoutDetails.height = OptionalDouble.empty();283 *284 * Person homerWithDetails = new Person("Homer Simpson");285 * homerWithDetails.home.address.street = "Evergreen Terrace";286 * homerWithDetails.home.address.number = 742;287 * homerWithDetails.home.phone = Optional.of("(939) 555-0113");288 * homerWithDetails.age = OptionalInt.of(39);289 * homerWithDetails.id = OptionalLong.of(123456);290 * homerWithDetails.height = OptionalDouble.of(1.83);291 *292 * // assertion succeeds as phone is ignored in the comparison293 * assertThat(homerWithoutDetails).usingRecursiveComparison()294 * .ignoringActualEmptyOptionalFields()295 * .isEqualTo(homerWithDetails);296 *297 * // assertion fails as phone, age, id and height are not ignored and are populated for homerWithDetails but not for homerWithoutDetails.298 * assertThat(homerWithDetails).usingRecursiveComparison()299 * .ignoringActualEmptyOptionalFields()300 * .isEqualTo(homerWithoutDetails);</code></pre>301 *302 * @return this {@link RecursiveComparisonAssert} to chain other methods.303 */304 @CheckReturnValue305 public SELF ignoringActualEmptyOptionalFields() {306 recursiveComparisonConfiguration.setIgnoreAllActualEmptyOptionalFields(true);307 return myself;308 }309 /**310 * Makes the recursive comparison to ignore all <b>expected null fields</b>.311 * <p>312 * Example:313 * <pre><code class='java'> public class Person {314 * String name;315 * double height;316 * Home home = new Home();317 * }318 *319 * public class Home {320 * Address address = new Address();321 * }322 *323 * public static class Address {324 * int number;325 * String street;326 * }327 *328 * Person sherlock = new Person("Sherlock", 1.80);329 * sherlock.home.address.street = "Baker Street";330 * sherlock.home.address.number = 221;331 *332 * Person noName = new Person(null, 1.80);333 * noName.home.address.street = null;334 * noName.home.address.number = 221;335 *336 * // assertion succeeds as name and home.address.street fields are ignored in the comparison337 * assertThat(sherlock).usingRecursiveComparison()338 * .ignoringExpectedNullFields()339 * .isEqualTo(noName);340 *341 * // assertion fails as name and home.address.street fields are populated for sherlock but not for noName.342 * assertThat(noName).usingRecursiveComparison()343 * .ignoringExpectedNullFields()344 * .isEqualTo(sherlock);</code></pre>345 *346 * @return this {@link RecursiveComparisonAssert} to chain other methods.347 */348 @CheckReturnValue349 public SELF ignoringExpectedNullFields() {350 recursiveComparisonConfiguration.setIgnoreAllExpectedNullFields(true);351 return myself;352 }353 /**354 * Makes the recursive comparison to ignore the given object under test fields. Nested fields can be specified like this: {@code home.address.street}.355 * <p>356 * Example:357 * <pre><code class='java'> public class Person {358 * String name;359 * double height;360 * Home home = new Home();361 * }362 *363 * public class Home {364 * Address address = new Address();365 * }366 *367 * public static class Address {368 * int number;369 * String street;370 * }371 *372 * Person sherlock = new Person("Sherlock", 1.80);373 * sherlock.home.address.street = "Baker Street";374 * sherlock.home.address.number = 221;375 *376 * Person noName = new Person(null, 1.80);377 * noName.home.address.street = null;378 * noName.home.address.number = 221;379 *380 * // assertion succeeds as name and home.address.street fields are ignored in the comparison381 * assertThat(sherlock).usingRecursiveComparison()382 * .ignoringFields("name", "home.address.street")383 * .isEqualTo(noName);384 *385 * // assertion fails as home.address.street fields differ and is not ignored.386 * assertThat(sherlock).usingRecursiveComparison()387 * .ignoringFields("name")388 * .isEqualTo(noName);</code></pre>389 *390 * @param fieldsToIgnore the fields of the object under test to ignore in the comparison.391 * @return this {@link RecursiveComparisonAssert} to chain other methods.392 */393 @CheckReturnValue394 public SELF ignoringFields(String... fieldsToIgnore) {395 recursiveComparisonConfiguration.ignoreFields(fieldsToIgnore);396 return myself;397 }398 /**399 * Makes the recursive comparison to ignore the object under test fields matching the given regexes.400 * <p>401 * Nested fields can be specified by using dots like this: {@code home\.address\.street} ({@code \} is used to escape402 * dots since they have a special meaning in regexes).403 * <p>404 * Example:405 * <pre><code class='java'> public class Person {406 * String name;407 * double height;408 * Home home = new Home();409 * }410 *411 * public class Home {412 * Address address = new Address();413 * }414 *415 * public static class Address {416 * int number;417 * String street;418 * }419 *420 * Person sherlock = new Person("Sherlock", 1.80);421 * sherlock.home.address.street = "Baker Street";422 * sherlock.home.address.number = 221;423 *424 * Person noName = new Person(null, 1.80);425 * noName.home.address.street = "Butcher Street";426 * noName.home.address.number = 222;427 *428 * // assertion succeeds as name and all home fields are ignored in the comparison429 * assertThat(sherlock).usingRecursiveComparison()430 * .ignoringFieldsMatchingRegexes("n.me", "home.*")431 * .isEqualTo(noName);432 *433 * // although home fields are ignored, assertion fails as name fields differ.434 * assertThat(sherlock).usingRecursiveComparison()435 * .ignoringFieldsMatchingRegexes("home.*")436 * .isEqualTo(noName);</code></pre>437 *438 * @param regexes regexes used to ignore fields in the comparison.439 * @return this {@link RecursiveComparisonAssert} to chain other methods.440 */441 @CheckReturnValue442 public SELF ignoringFieldsMatchingRegexes(String... regexes) {443 recursiveComparisonConfiguration.ignoreFieldsMatchingRegexes(regexes);444 return myself;445 }446 /**447 * Makes the recursive comparison to ignore the object under test fields of the given types.448 * The fields are ignored if their types <b>exactly match one of the ignored types</b>, for example if a field is a subtype of an ignored type it is not ignored.449 * <p>450 * If some object under test fields are null it is not possible to evaluate their types unless in {@link #withStrictTypeChecking() strictTypeChecking mode},451 * in that case the corresponding expected field's type is evaluated instead but if strictTypeChecking mode is disabled then null fields are not ignored.452 * <p>453 * Example:454 * <pre><code class='java'> public class Person {455 * String name;456 * double height;457 * Home home = new Home();458 * }459 *460 * public class Home {461 * Address address = new Address();462 * }463 *464 * public static class Address {465 * int number;466 * String street;467 * }468 *469 * Person sherlock = new Person("Sherlock", 1.80);470 * sherlock.home.address.street = "Baker Street";471 * sherlock.home.address.number = 221;472 *473 * Person sherlock2 = new Person("Sherlock", 1.90);474 * sherlock2.home.address.street = "Butcher Street";475 * sherlock2.home.address.number = 221;476 *477 * // assertion succeeds as we ignore Address and height478 * assertThat(sherlock).usingRecursiveComparison()479 * .ignoringFieldsOfTypes(double.class, Address.class)480 * .isEqualTo(sherlock2);481 *482 * // now this assertion fails as expected since the home.address.street fields and height differ483 * assertThat(sherlock).usingRecursiveComparison()484 * .isEqualTo(sherlock2);</code></pre>485 *486 * @param typesToIgnore the types we want to ignore in the object under test fields.487 * @return this {@link RecursiveComparisonAssert} to chain other methods.488 */489 public RecursiveComparisonAssert<?> ignoringFieldsOfTypes(Class<?>... typesToIgnore) {490 recursiveComparisonConfiguration.ignoreFieldsOfTypes(typesToIgnore);491 return myself;492 }493 /**494 * This method instructs the recursive comparison to compare recursively all fields including the one whose type have overridden equals,495 * <b>except fields with java types</b> (at some point we need to compare something!).496 * <p>497 * Since 3.17.0 this is the default behavior for recursive comparisons, to revert to the previous behavior call {@link #usingOverriddenEquals()}.498 * <p>499 * Example:500 * <pre><code class='java'> public class Person {501 * String name;502 * double height;503 * Home home = new Home();504 * }505 *506 * public class Home {507 * Address address = new Address();508 * }509 *510 * public static class Address {511 * int number;512 * String street;513 *514 * // only compares number, ouch!515 * {@literal @}Override516 * public boolean equals(final Object other) {517 * if (!(other instanceof Address)) return false;518 * Address castOther = (Address) other;519 * return Objects.equals(number, castOther.number);520 * }521 * }522 *523 * Person sherlock = new Person("Sherlock", 1.80);524 * sherlock.home.address.street = "Baker Street";525 * sherlock.home.address.number = 221;526 *527 * Person sherlock2 = new Person("Sherlock", 1.80);528 * sherlock2.home.address.street = "Butcher Street";529 * sherlock2.home.address.number = 221;530 *531 * // Assertion succeeds because:532 * // - overridden equals are used533 * // - Address has overridden equals and does not compare street fields.534 * assertThat(sherlock).usingRecursiveComparison()535 * .usingOverriddenEquals()536 * .isEqualTo(sherlock2);537 *538 * // To avoid using Address overridden equals, don't call usingOverriddenEquals() or call ignoringAllOverriddenEquals()539 * // (calling ignoringAllOverriddenEquals() is actually not required as this is the default behavior).540 * // This assertion fails as it will compare home.address.street fields which differ541 * assertThat(sherlock).usingRecursiveComparison()542 * //.ignoringAllOverriddenEquals() // not needed as this is the default543 * .isEqualTo(sherlock2);</code></pre>544 *545 * @return this {@link RecursiveComparisonAssert} to chain other methods.546 */547 public SELF ignoringAllOverriddenEquals() {548 recursiveComparisonConfiguration.ignoreAllOverriddenEquals();549 return myself;550 }551 /**552 * By default the recursive comparison compare recursively all fields including the ones whose type have overridden equals553 * <b>except fields with java types</b> (at some point we need to compare something!).554 * <p>555 * This method instructs the recursive comparison to use overridden equals.556 * <p>557 * Example:558 * <pre><code class='java'> public class Person {559 * String name;560 * double height;561 * Home home = new Home();562 * }563 *564 * public class Home {565 * Address address = new Address();566 * }567 *568 * public static class Address {569 * int number;570 * String street;571 *572 * // only compares number!573 * {@literal @}Override574 * public boolean equals(final Object other) {575 * if (!(other instanceof Address)) return false;576 * Address castOther = (Address) other;577 * return Objects.equals(number, castOther.number);578 * }579 * }580 *581 * Person sherlock = new Person("Sherlock", 1.80);582 * sherlock.home.address.street = "Baker Street";583 * sherlock.home.address.number = 221;584 *585 * Person sherlock2 = new Person("Sherlock", 1.80);586 * sherlock2.home.address.street = "Butcher Street";587 * sherlock2.home.address.number = 221;588 *589 * // assertion succeeds because Address equals does not compare street fields.590 * assertThat(sherlock).usingRecursiveComparison()591 * .usingOverriddenEquals()592 * .isEqualTo(sherlock2);593 *594 * // Assertion fails because:595 * // - Address equals is not used.596 * // - street fields are compared and differ.597 * assertThat(sherlock).usingRecursiveComparison()598 * .isEqualTo(sherlock2);</code></pre>599 *600 * @return this {@link RecursiveComparisonAssert} to chain other methods.601 * @since 3.17.0602 */603 public SELF usingOverriddenEquals() {604 recursiveComparisonConfiguration.useOverriddenEquals();605 return myself;606 }607 /**608 * In case you have instructed the recursive to use overridden {@code equals} with {@link #usingOverriddenEquals()},609 * this method allows to ignore overridden {@code equals} for the given fields (it adds them to the already registered ones).610 * <p>611 * Since 3.17.0 all overridden {@code equals} so this method is only relevant if you have called {@link #usingOverriddenEquals()} before.612 * <p>613 * Nested fields can be specified by using dots like this: {@code home.address.street}614 * <p>615 * Example:616 * <pre><code class='java'> public class Person {617 * String name;618 * double height;619 * Home home = new Home();620 * }621 *622 * public class Home {623 * Address address = new Address();624 * }625 *626 * public static class Address {627 * int number;628 * String street;629 *630 * // only compares number631 * {@literal @}Override632 * public boolean equals(final Object other) {633 * if (!(other instanceof Address)) return false;634 * Address castOther = (Address) other;635 * return Objects.equals(number, castOther.number);636 * }637 * }638 *639 * Person sherlock = new Person("Sherlock", 1.80);640 * sherlock.home.address.street = "Baker Street";641 * sherlock.home.address.number = 221;642 *643 * Person sherlock2 = new Person("Sherlock", 1.80);644 * sherlock2.home.address.street = "Butcher Street";645 * sherlock2.home.address.number = 221;646 *647 * // Assertion succeeds because:648 * // - overridden equals are used649 * // - Address has overridden equals and does not compare street fields.650 * assertThat(sherlock).usingRecursiveComparison()651 * .usingOverriddenEquals()652 * .isEqualTo(sherlock2);653 *654 * // ignoringOverriddenEqualsForFields force a recursive comparison on the given field655 * // Assertion fails because:656 * // - Address equals is not used.657 * // - street fields are compared and differ.658 * assertThat(sherlock).usingRecursiveComparison()659 * .usingOverriddenEquals()660 * .ignoringOverriddenEqualsForFields("home.address")661 * .isEqualTo(sherlock2);</code></pre>662 *663 * @param fields the fields we want to force a recursive comparison on.664 * @return this {@link RecursiveComparisonAssert} to chain other methods.665 */666 @CheckReturnValue667 public SELF ignoringOverriddenEqualsForFields(String... fields) {668 recursiveComparisonConfiguration.ignoreOverriddenEqualsForFields(fields);669 return myself;670 }671 /**672 * By default the recursive comparison uses overridden {@code equals} methods to compare fields,673 * this method allows to force a recursive comparison for all fields of the given types (it adds them to the already registered ones).674 * <p>675 * Since 3.17.0 all overridden {@code equals} so this method is only relevant if you have called {@link #usingOverriddenEquals()} before.676 * <p>677 * Example:678 * <pre><code class='java'> public class Person {679 * String name;680 * double height;681 * Home home = new Home();682 * }683 *684 * public class Home {685 * Address address = new Address();686 * }687 *688 * public static class Address {689 * int number;690 * String street;691 *692 * // only compares number, ouch!693 * {@literal @}Override694 * public boolean equals(final Object other) {695 * if (!(other instanceof Address)) return false;696 * Address castOther = (Address) other;697 * return Objects.equals(number, castOther.number);698 * }699 * }700 *701 * Person sherlock = new Person("Sherlock", 1.80);702 * sherlock.home.address.street = "Baker Street";703 * sherlock.home.address.number = 221;704 *705 * Person sherlock2 = new Person("Sherlock", 1.80);706 * sherlock2.home.address.street = "Butcher Street";707 * sherlock2.home.address.number = 221;708 *709 * // Assertion succeeds because:710 * // - overridden equals are used711 * // - Address has overridden equals and does not compare street fields.712 * assertThat(sherlock).usingRecursiveComparison()713 * .usingOverriddenEquals()714 * .isEqualTo(sherlock2);715 *716 * // ignoringOverriddenEqualsForTypes force a recursive comparison on the given types.717 * // Assertion fails because:718 * // - Address equals is not used.719 * // - street fields are compared and differ.720 * assertThat(sherlock).usingRecursiveComparison()721 * .usingOverriddenEquals()722 * .ignoringOverriddenEqualsForTypes(Address.class)723 * .isEqualTo(sherlock2);</code></pre>724 *725 * @param types the types we want to force a recursive comparison on.726 * @return this {@link RecursiveComparisonAssert} to chain other methods.727 */728 @CheckReturnValue729 public SELF ignoringOverriddenEqualsForTypes(Class<?>... types) {730 recursiveComparisonConfiguration.ignoreOverriddenEqualsForTypes(types);731 return myself;732 }733 /**734 * In case you have instructed the recursive to use overridden {@code equals} with {@link #usingOverriddenEquals()},735 * this method allows to force a recursive comparison for the fields matching the given regexes (it adds them to the already registered ones).736 * <p>737 * Since 3.17.0 all overridden {@code equals} so this method is only relevant if you have called {@link #usingOverriddenEquals()} before.738 * <p>739 * Nested fields can be specified by using dots like: {@code home\.address\.street} ({@code \} is used to escape740 * dots since they have a special meaning in regexes).741 * <p>742 * Example:743 * <pre><code class='java'> public class Person {744 * String name;745 * double height;746 * Home home = new Home();747 * }748 *749 * public class Home {750 * Address address = new Address();751 * }752 *753 * public static class Address {754 * int number;755 * String street;756 *757 * // only compares number, ouch!758 * {@literal @}Override759 * public boolean equals(final Object other) {760 * if (!(other instanceof Address)) return false;761 * Address castOther = (Address) other;762 * return Objects.equals(number, castOther.number);763 * }764 * }765 *766 * Person sherlock = new Person("Sherlock", 1.80);767 * sherlock.home.address.street = "Baker Street";768 * sherlock.home.address.number = 221;769 *770 * Person sherlock2 = new Person("Sherlock", 1.80);771 * sherlock2.home.address.street = "Butcher Street";772 * sherlock2.home.address.number = 221;773 *774 * // assertion succeeds because overridden equals are used and thus street fields are mot compared775 * assertThat(sherlock).usingRecursiveComparison()776 * .usingOverriddenEquals()777 * .isEqualTo(sherlock2);778 *779 * // ignoringOverriddenEqualsForFields force a recursive comparison on the field matching the regex780 * // now this assertion fails as we expect since the home.address.street fields differ781 * assertThat(sherlock).usingRecursiveComparison()782 * .usingOverriddenEquals()783 * .ignoringOverriddenEqualsForFieldsMatchingRegexes("home.*")784 * .isEqualTo(sherlock2);</code></pre>785 *786 * @param regexes regexes used to specify the fields we want to force a recursive comparison on.787 * @return this {@link RecursiveComparisonAssert} to chain other methods.788 */789 @CheckReturnValue790 public SELF ignoringOverriddenEqualsForFieldsMatchingRegexes(String... regexes) {791 recursiveComparisonConfiguration.ignoreOverriddenEqualsForFieldsMatchingRegexes(regexes);792 return myself;793 }794 /**795 * Makes the recursive comparison to ignore collection order in all fields in the object under test.796 * <p>797 * Example:798 * <pre><code class='java'> public class Person {799 * String name;800 * List&lt;Person&gt; friends = new ArrayList&lt;&gt;();801 * }802 *803 * Person sherlock1 = new Person("Sherlock Holmes");804 * sherlock1.friends.add(new Person("Dr. John Watson"));805 * sherlock1.friends.add(new Person("Molly Hooper"));806 *807 * Person sherlock2 = new Person("Sherlock Holmes");808 * sherlock2.friends.add(new Person("Molly Hooper"));809 * sherlock2.friends.add(new Person("Dr. John Watson"));810 *811 * // assertion succeeds as all fields collection order is ignored in the comparison812 * assertThat(sherlock1).usingRecursiveComparison()813 * .ignoringCollectionOrder()814 * .isEqualTo(sherlock2);815 *816 * // assertion fails as fields collection order is not ignored in the comparison817 * assertThat(sherlock1).usingRecursiveComparison()818 * .isEqualTo(sherlock2);</code></pre>819 *820 * @return this {@link RecursiveComparisonAssert} to chain other methods.821 */822 @CheckReturnValue823 public SELF ignoringCollectionOrder() {824 recursiveComparisonConfiguration.ignoreCollectionOrder(true);825 return myself;826 }827 /**828 * Makes the recursive comparison to ignore collection order in the object under test specified fields. Nested fields can be specified like this: {@code home.address.street}.829 * <p>830 * Example:831 * <pre><code class='java'> public class Person {832 * String name;833 * List&lt;Person&gt; friends = new ArrayList&lt;&gt;();834 * List&lt;Person&gt; enemies = new ArrayList&lt;&gt;();835 * }836 *837 * Person sherlock1 = new Person("Sherlock Holmes");838 * sherlock1.friends.add(new Person("Dr. John Watson"));839 * sherlock1.friends.add(new Person("Molly Hooper"));840 * sherlock1.enemies.add(new Person("Jim Moriarty"));841 * sherlock1.enemies.add(new Person("Irene Adler"));842 *843 * Person sherlock2 = new Person("Sherlock Holmes");844 * sherlock2.friends.add(new Person("Molly Hooper"));845 * sherlock2.friends.add(new Person("Dr. John Watson"));846 * sherlock2.enemies.add(new Person("Irene Adler"));847 * sherlock2.enemies.add(new Person("Jim Moriarty"));848 *849 * // assertion succeeds as friends and enemies fields collection order is ignored in the comparison850 * assertThat(sherlock1).usingRecursiveComparison()851 * .ignoringCollectionOrderInFields("friends", "enemies")852 * .isEqualTo(sherlock2);853 *854 * // assertion fails as enemies field collection order differ and it is not ignored855 * assertThat(sherlock1).usingRecursiveComparison()856 * .ignoringCollectionOrderInFields("friends")857 * .isEqualTo(sherlock2);</code></pre>858 *859 * @param fieldsToIgnoreCollectionOrder the fields of the object under test to ignore collection order in the comparison.860 * @return this {@link RecursiveComparisonAssert} to chain other methods.861 */862 @CheckReturnValue863 public SELF ignoringCollectionOrderInFields(String... fieldsToIgnoreCollectionOrder) {864 recursiveComparisonConfiguration.ignoreCollectionOrderInFields(fieldsToIgnoreCollectionOrder);865 return myself;866 }867 /**868 * Makes the recursive comparison to ignore collection order in the object under test fields matching the specified regexes.869 * <p>870 * Nested fields can be specified by using dots like this: {@code home\.address\.street} ({@code \} is used to escape871 * dots since they have a special meaning in regexes).872 * <p>873 * Example:874 * <pre><code class='java'> public class Person {875 * String name;876 * List&lt;Person&gt; friends = new ArrayList&lt;&gt;();877 * List&lt;Person&gt; enemies = new ArrayList&lt;&gt;();878 * }879 *880 * Person sherlock1 = new Person("Sherlock Holmes");881 * sherlock1.friends.add(new Person("Dr. John Watson"));882 * sherlock1.friends.add(new Person("Molly Hooper"));883 * sherlock1.enemies.add(new Person("Jim Moriarty"));884 * sherlock1.enemies.add(new Person("Irene Adler"));885 *886 * Person sherlock2 = new Person("Sherlock Holmes");887 * sherlock2.friends.add(new Person("Molly Hooper"));888 * sherlock2.friends.add(new Person("Dr. John Watson"));889 * sherlock2.enemies.add(new Person("Irene Adler"));890 * sherlock2.enemies.add(new Person("Jim Moriarty"));891 *892 * // assertion succeeds as friends and enemies fields collection order is ignored in the comparison893 * assertThat(sherlock1).usingRecursiveComparison()894 * .ignoringCollectionOrderInFieldsMatchingRegexes("friend.", "enemie.")895 * .isEqualTo(sherlock2);896 *897 * // assertion fails as enemies field collection order differ and it is not ignored898 * assertThat(sherlock1).usingRecursiveComparison()899 * .ignoringCollectionOrderInFields("friend.")900 * .isEqualTo(sherlock2);</code></pre>901 *902 * @param regexes regexes used to find the object under test fields to ignore collection order in the comparison.903 * @return this {@link RecursiveComparisonAssert} to chain other methods.904 */905 @CheckReturnValue906 public SELF ignoringCollectionOrderInFieldsMatchingRegexes(String... regexes) {907 recursiveComparisonConfiguration.ignoreCollectionOrderInFieldsMatchingRegexes(regexes);908 return myself;909 }910 /**911 * Makes the recursive comparison to check that actual's type is compatible with expected's type (and do the same for each field). <br>912 * Compatible means that the expected's type is the same or a subclass of actual's type.913 * <p>914 * Examples:915 * <pre><code class='java'> public class Person {916 * String name;917 * double height;918 * Person bestFriend;919 * }920 *921 * Person sherlock = new Person("Sherlock", 1.80);922 * sherlock.bestFriend = new Person("Watson", 1.70);923 *924 * Person sherlockClone = new Person("Sherlock", 1.80);925 * sherlockClone.bestFriend = new Person("Watson", 1.70);926 *927 * // assertion succeeds as sherlock and sherlockClone have the same data and types928 * assertThat(sherlock).usingRecursiveComparison()929 * .withStrictTypeChecking()930 * .isEqualTo(sherlockClone);931 *932 * // Let's now define a data structure similar to Person933 *934 * public class PersonDTO {935 * String name;936 * double height;937 * PersonDTO bestFriend;938 * }939 *940 * PersonDTO sherlockDto = new PersonDTO("Sherlock", 1.80);941 * sherlockDto.bestFriend = new PersonDTO("Watson", 1.70);942 *943 * // assertion fails as Person and PersonDTO are not compatible even though they have the same data944 * assertThat(sherlock).usingRecursiveComparison()945 * .withStrictTypeChecking()946 * .isEqualTo(noName);947 *948 * // Let's define a subclass of Person949 *950 * public class Detective extends Person {951 * boolean busy;952 * }953 *954 * Detective detectiveSherlock = new Detective("Sherlock", 1.80);955 * detectiveSherlock.bestFriend = new Person("Watson", 1.70);956 * detectiveSherlock.busy = true;957 *958 * // assertion succeeds as Detective inherits from Person and959 * // only Person's fields are included into the comparison.960 * assertThat(sherlock).usingRecursiveComparison()961 * .withStrictTypeChecking()962 * .isEqualTo(detectiveSherlock);</code></pre>963 *964 * @return this {@link RecursiveComparisonAssert} to chain other methods.965 */966 @CheckReturnValue967 public SELF withStrictTypeChecking() {968 recursiveComparisonConfiguration.strictTypeChecking(true);969 return myself;970 }971 /**972 * Allows to register a {@link BiPredicate} to compare fields with the given locations.973 * A typical usage is for comparing double/float fields with a given precision.974 * <p>975 * BiPredicates specified with this method have precedence over the ones registered with {@link #withEqualsForType(BiPredicate, Class)}976 * or the comparators registered with {@link #withComparatorForType(Comparator, Class)}.977 * <p>978 * Note that registering a {@link BiPredicate} for a given field will override the previously registered Comparator (if any).979 * <p>980 * The field locations must be specified from the root object,981 * for example if {@code Foo} has a {@code Bar} field which has an {@code id}, one can register to a comparator for Bar's {@code id} by calling:982 * <pre><code class='java'> withEqualsForFields(idBiPredicate, "foo.id", "foo.bar.id")</code></pre>983 * <p>984 * Complete example:985 * <pre><code class='java'> public class TolkienCharacter {986 * String name;987 * double height;988 * }989 *990 * TolkienCharacter frodo = new TolkienCharacter(&quot;Frodo&quot;, 1.2);991 * TolkienCharacter tallerFrodo = new TolkienCharacter(&quot;Frodo&quot;, 1.3);992 * TolkienCharacter reallyTallFrodo = new TolkienCharacter(&quot;Frodo&quot;, 1.9);993 *994 * BiPredicate&lt;Double, Double&gt; closeEnough = (d1, d2) -&gt; Math.abs(d1 - d2) &lt;= 0.5;995 *996 * // assertion succeeds997 * assertThat(frodo).usingRecursiveComparison()998 * .withEqualsForFields(closeEnough, &quot;height&quot;)999 * .isEqualTo(tallerFrodo);1000 *1001 * // assertion fails1002 * assertThat(frodo).usingRecursiveComparison()1003 * .withEqualsForFields(closeEnough, &quot;height&quot;)1004 * .isEqualTo(reallyTallFrodo);</code></pre>1005 *1006 * @param equals the {@link BiPredicate} to use to compare the given fields1007 * @param fieldLocations the location from the root object of the fields the BiPredicate should be used for1008 *1009 * @return this {@link RecursiveComparisonAssert} to chain other methods.1010 * @throws NullPointerException if the given BiPredicate is null.1011 */1012 @CheckReturnValue1013 public SELF withEqualsForFields(BiPredicate<?, ?> equals, String... fieldLocations) {1014 recursiveComparisonConfiguration.registerEqualsForFields(equals, fieldLocations);1015 return myself;1016 }1017 /**1018 * Allows to register a comparator to compare fields with the given locations.1019 * A typical usage is for comparing double/float fields with a given precision.1020 * <p>1021 * Comparators registered with this method have precedence over comparators registered with {@link #withComparatorForType(Comparator, Class)}1022 * or {@link BiPredicate} registered with {@link #withEqualsForType(BiPredicate, Class)}.1023 * <p>1024 * The field locations must be specified from the root object,1025 * for example if {@code Foo} has a {@code Bar} field which has an {@code id}, one can register to a comparator for Bar's {@code id} by calling:1026 * <pre><code class='java'> withComparatorForFields(idComparator, "foo.id", "foo.bar.id")</code></pre>1027 * <p>1028 * Note that registering a {@link Comparator} for a given field will override the previously registered BiPredicate/Comparator (if any).1029 * <p>1030 * Complete example:1031 * <pre><code class='java'> public class TolkienCharacter {1032 * String name;1033 * double height;1034 * }1035 *1036 * TolkienCharacter frodo = new TolkienCharacter(&quot;Frodo&quot;, 1.2);1037 * TolkienCharacter tallerFrodo = new TolkienCharacter(&quot;Frodo&quot;, 1.3);1038 * TolkienCharacter reallyTallFrodo = new TolkienCharacter(&quot;Frodo&quot;, 1.9);1039 *1040 * Comparator&lt;Double&gt; closeEnough = (d1, d2) -&gt; Math.abs(d1 - d2) &lt;= 0.5 ? 0 : 1;1041 *1042 * // assertion succeeds1043 * assertThat(frodo).usingRecursiveComparison()1044 * .withComparatorForFields(closeEnough, &quot;height&quot;)1045 * .isEqualTo(tallerFrodo);1046 *1047 * // assertion fails1048 * assertThat(frodo).usingRecursiveComparison()1049 * .withComparatorForFields(closeEnough, &quot;height&quot;)1050 * .isEqualTo(reallyTallFrodo);</code></pre>1051 *1052 * @param comparator the {@link java.util.Comparator Comparator} to use to compare the given fields1053 * @param fieldLocations the location from the root object of the fields the comparator should be used for1054 * @return this {@link RecursiveComparisonAssert} to chain other methods.1055 * @throws NullPointerException if the given comparator is null.1056 */1057 @CheckReturnValue1058 public SELF withComparatorForFields(Comparator<?> comparator, String... fieldLocations) {1059 recursiveComparisonConfiguration.registerComparatorForFields(comparator, fieldLocations);1060 return myself;1061 }1062 /**1063 * Allows to register a comparator to compare the fields with the given type.1064 * A typical usage is for comparing double/float fields with a given precision.1065 * <p>1066 * Comparators registered with this method have less precedence than comparators registered with {@link #withComparatorForFields(Comparator, String...) withComparatorForFields(Comparator, String...)}1067 * or BiPredicate registered with {@link #withEqualsForFields(BiPredicate, String...) withEqualsForFields(BiPredicate, String...)}.1068 * <p>1069 * Note that registering a {@link Comparator} for a given type will override the previously registered BiPredicate/Comparator (if any).1070 * <p>1071 * Example:1072 * <pre><code class='java'> public class TolkienCharacter {1073 * String name;1074 * double height;1075 * }1076 *1077 * TolkienCharacter frodo = new TolkienCharacter(&quot;Frodo&quot;, 1.2);1078 * TolkienCharacter tallerFrodo = new TolkienCharacter(&quot;Frodo&quot;, 1.3);1079 * TolkienCharacter reallyTallFrodo = new TolkienCharacter(&quot;Frodo&quot;, 1.9);1080 *1081 * Comparator&lt;Double&gt; closeEnough = (d1, d2) -&gt; Math.abs(d1 - d2) &lt;= 0.5 ? 0 : 1;1082 *1083 * // assertion succeeds1084 * assertThat(frodo).usingRecursiveComparison()1085 * .withComparatorForType(closeEnough, Double.class)1086 * .isEqualTo(tallerFrodo);1087 *1088 * // assertion fails1089 * assertThat(frodo).usingRecursiveComparison()1090 * .withComparatorForType(closeEnough, Double.class)1091 * .isEqualTo(reallyTallFrodo);</code></pre>1092 *1093 * @param <T> the class type to register a comparator for1094 * @param comparator the {@link java.util.Comparator Comparator} to use to compare the given fields1095 * @param type the type to be compared with the given comparator.1096 *1097 * @return this {@link RecursiveComparisonAssert} to chain other methods.1098 * @throws NullPointerException if the given comparator is null.1099 */1100 @CheckReturnValue1101 public <T> SELF withComparatorForType(Comparator<? super T> comparator, Class<T> type) {1102 recursiveComparisonConfiguration.registerComparatorForType(comparator, type);1103 return myself;1104 }1105 /**1106 * Allows to register a {@link BiPredicate} to compare the fields with the given type.1107 * A typical usage is for comparing double/float fields with a given precision.1108 * <p>1109 * BiPredicates registered with this method have less precedence than the one registered with {@link #withEqualsForFields(BiPredicate, String...) withEqualsForFields(BiPredicate, String...)}1110 * or comparators registered with {@link #withComparatorForFields(Comparator, String...) withComparatorForFields(Comparator, String...)}.1111 * <p>1112 * Note that registering a {@link BiPredicate} for a given type will override the previously registered BiPredicate/Comparator (if any).1113 * <p>1114 * Example:1115 * <pre><code class='java'> public class TolkienCharacter {1116 * String name;1117 * double height;1118 * }1119 *1120 * TolkienCharacter frodo = new TolkienCharacter(&quot;Frodo&quot;, 1.2);1121 * TolkienCharacter tallerFrodo = new TolkienCharacter(&quot;Frodo&quot;, 1.3);1122 * TolkienCharacter reallyTallFrodo = new TolkienCharacter(&quot;Frodo&quot;, 1.9);1123 *1124 * BiPredicate&lt;Double, Double&gt; closeEnough = (d1, d2) -&gt; Math.abs(d1 - d2) &lt;= 0.5;1125 *1126 * // assertion succeeds1127 * assertThat(frodo).usingRecursiveComparison()1128 * .withEqualsForType(closeEnough, Double.class)1129 * .isEqualTo(tallerFrodo);1130 *1131 * // assertion fails1132 * assertThat(frodo).usingRecursiveComparison()1133 * .withEqualsForType(closeEnough, Double.class)1134 * .isEqualTo(reallyTallFrodo);</code></pre>1135 *1136 * @param <T> the class type to register a BiPredicate for1137 * @param equals the {@link BiPredicate} to use to compare the given fields1138 * @param type the type to be compared with the given comparator.1139 *1140 * @return this {@link RecursiveComparisonAssert} to chain other methods.1141 * @throws NullPointerException if the given BiPredicate is null.1142 */1143 @CheckReturnValue1144 public <T> SELF withEqualsForType(BiPredicate<? super T, ? super T> equals, Class<T> type) {1145 recursiveComparisonConfiguration.registerEqualsForType(equals, type);1146 return myself;1147 }1148 SELF withTypeComparators(TypeComparators typeComparators) {1149 Optional.ofNullable(typeComparators)1150 .map(TypeComparators::comparatorByTypes)1151 .ifPresent(comparatorByTypes -> comparatorByTypes.forEach(this::registerComparatorForType));1152 return myself;1153 }1154 @SuppressWarnings({ "unchecked", "rawtypes" })1155 private void registerComparatorForType(Entry<Class<?>, Comparator<?>> entry) {1156 withComparatorForType((Comparator) entry.getValue(), entry.getKey());1157 }1158 /**...

Full Screen

Full Screen

withEqualsForType

Using AI Code Generation

copy

Full Screen

1import org.assertj.core.api.Assertions;2import org.assertj.core.api.RecursiveComparisonAssert;3import org.assertj.core.api.RecursiveComparisonConfiguration;4import org.junit.jupiter.api.Test;5public class RecursiveComparisonAssertTest {6 public void test() {7 RecursiveComparisonConfiguration configuration = new RecursiveComparisonConfiguration();8 configuration.withEqualsForType((actual, other) -> actual instanceof String && other instanceof String && ((String) actual).toLowerCase().equals(((String) other).toLowerCase()), String.class);9 RecursiveComparisonAssert recursiveComparisonAssert = Assertions.assertThat(new Person("John", "Doe")).usingRecursiveComparison(configuration);10 recursiveComparisonAssert.isEqualTo(new Person("john", "doe"));11 }12}13class Person {14 String firstName;15 String lastName;16 public Person(String firstName, String lastName) {17 this.firstName = firstName;18 this.lastName = lastName;19 }20}21 <Person(firstName=John, lastName=Doe)>22 <Person(firstName=john, lastName=doe)>

Full Screen

Full Screen

withEqualsForType

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.Assertions.withinPercentage;4import static org.assertj.core.api.Assertions.withinPrecision;5public class Test {6 public static void main(String[] args) {7 Person person = new Person("John", "Smith", 30);8 Person person2 = new Person("John", "Smith", 30);9 assertThat(person).usingRecursiveComparison().isEqualTo(person2);10 }11 static class Person {12 private String firstName;13 private String lastName;14 private int age;15 public Person(String firstName, String lastName, int age) {16 this.firstName = firstName;17 this.lastName = lastName;18 this.age = age;19 }20 public String getFirstName() {21 return firstName;22 }23 public String getLastName() {24 return lastName;25 }26 public int getAge() {27 return age;28 }29 }30}

Full Screen

Full Screen

withEqualsForType

Using AI Code Generation

copy

Full Screen

1import static org.assertj.core.api.Assertions.*;2import static org.assertj.core.api.Assertions.withEqualsForType;3import static org.assertj.core.api.Assertions.withoutEqualsForType;4import java.util.ArrayList;5import java.util.List;6import java.util.Objects;7import org.junit.Test;8public class AssertJTest {9 public void testWithEqualsForType() {10 List<Department> departments = new ArrayList<>();11 departments.add(new Department(1, "IT"));12 departments.add(new Department(2, "HR"));13 departments.add(new Department(3, "Admin"));14 departments.add(new Department(4, "Sales"));15 List<Department> departments2 = new ArrayList<>();16 departments2.add(new Department(1, "IT"));17 departments2.add(new Department(2, "HR"));18 departments2.add(new Department(3, "Admin"));19 departments2.add(new Department(4, "Sales"));20 assertThat(departments).usingRecursiveComparison().withEqualsForType(Department.class, (d1, d2) -> d1.getName().equals(d2.getName())).isEqualTo(departments2);21 }22 public void testWithoutEqualsForType() {23 List<Department> departments = new ArrayList<>();24 departments.add(new Department(1, "IT"));25 departments.add(new Department(2, "HR"));26 departments.add(new Department(3, "Admin"));27 departments.add(new Department(4, "Sales"));28 List<Department> departments2 = new ArrayList<>();29 departments2.add(new Department(1, "IT"));30 departments2.add(new Department(2, "HR"));31 departments2.add(new Department(3, "Admin"));32 departments2.add(new Department(4, "Sales"));33 assertThat(departments).usingRecursiveComparison().withoutEqualsForType(Department.class).isEqualTo(departments2);34 }35}36class Department {37 private int id;38 private String name;39 public Department(int id, String name) {40 this.id = id;41 this.name = name;42 }43 public int getId() {44 return id;45 }46 public String getName() {47 return name;48 }49 public boolean equals(Object o) {50 if (this == o)51 return true;52 if (o == null || getClass() != o.getClass())53 return false;54 Department that = (Department) o;

Full Screen

Full Screen

withEqualsForType

Using AI Code Generation

copy

Full Screen

1import org.junit.Test;2import static org.assertj.core.api.Assertions.*;3import static org.assertj.core.api.Assertions.within;4import static org.assertj.core.api.RecursiveComparisonAssert.*;5import static org.assertj.core.api.RecursiveComparisonConfiguration.*;6public class Test1 {7 public void test1() {8 A a1 = new A();9 a1.b = new B();10 a1.b.c = new C();11 a1.b.c.d = new D();12 a1.b.c.d.e = new E();13 a1.b.c.d.e.f = new F();14 a1.b.c.d.e.f.g = new G();15 a1.b.c.d.e.f.g.h = new H();16 a1.b.c.d.e.f.g.h.i = new I();17 a1.b.c.d.e.f.g.h.i.j = new J();18 a1.b.c.d.e.f.g.h.i.j.k = new K();19 a1.b.c.d.e.f.g.h.i.j.k.l = new L();20 a1.b.c.d.e.f.g.h.i.j.k.l.m = new M();21 a1.b.c.d.e.f.g.h.i.j.k.l.m.n = new N();22 a1.b.c.d.e.f.g.h.i.j.k.l.m.n.o = new O();23 a1.b.c.d.e.f.g.h.i.j.k.l.m.n.o.p = new P();24 a1.b.c.d.e.f.g.h.i.j.k.l.m.n.o.p.q = new Q();25 a1.b.c.d.e.f.g.h.i.j.k.l.m.n.o.p.q.r = new R();26 a1.b.c.d.e.f.g.h.i.j.k.l.m.n.o.p.q.r.s = new S();27 a1.b.c.d.e.f.g.h.i.j.k.l.m.n.o.p.q.r.s.t = new T();28 a1.b.c.d.e.f.g.h.i.j.k.l.m.n.o.p.q.r.s.t.u = new U();29 a1.b.c.d.e.f.g.h.i.j.k.l.m.n.o.p.q.r.s.t.u.v = new V();30 a1.b.c.d.e.f.g.h.i.j.k.l.m.n.o.p.q.r.s.t.u.v.w = new W();

Full Screen

Full Screen

withEqualsForType

Using AI Code Generation

copy

Full Screen

1import org.junit.Test;2import static org.assertj.core.api.Assertions.assertThat;3public class AssertJTest {4 public void test() {5 assertThat(new Foo())6 .usingRecursiveComparison()7 .withEqualsForType((a, b) -> true, Foo.class)8 .isEqualTo(new Foo());9 }10}11class Foo {12}13at org.assertj.core.api.RecursiveComparisonAssert.isEqualTo(RecursiveComparisonAssert.java:233)14at org.assertj.core.api.AbstractAssert.isEqualTo(AbstractAssert.java:82)15at AssertJTest.test(AssertJTest.java:9)16assertThat(new Foo())17 .usingRecursiveComparison()18 .withEqualsForType((a, b) -> true, Foo.class)19 .isEqualTo(new Foo());20at org.assertj.core.api.RecursiveComparisonAssert.isEqualTo(RecursiveComparisonAssert.java:233)21at org.assertj.core.api.AbstractAssert.isEqualTo(AbstractAssert.java:82)22at AssertJTest.test(AssertJTest.java:9)23assertThat(new Foo())24 .usingRecursiveComparison()

Full Screen

Full Screen

withEqualsForType

Using AI Code Generation

copy

Full Screen

1import org.assertj.core.api.Assertions;2import org.junit.Test;3public class RecursiveComparisonAssertTest {4 public void test() {5 Assertions.assertThat(new Object()).usingRecursiveComparison().withEqualsForType("abc", String.class).isEqualTo(new Object());6 }7}

Full Screen

Full Screen

withEqualsForType

Using AI Code Generation

copy

Full Screen

1package com.mycompany.app;2import org.junit.jupiter.api.Test;3import static org.assertj.core.api.Assertions.*;4import static org.assertj.core.api.Assertions.within;5import static org.assertj.core.api.Assertions.withinPercentage;6import java.util.*;7class WithEqualsForTypeTest {8 public void withEqualsForTypeTest() {9 Person person1 = new Person("John", 30, new Address("London"));10 Person person2 = new Person("John", 30, new Address("London"));11 Person person3 = new Person("John", 30, new Address("New York"));12 assertThat(person1).usingRecursiveComparison()13 .withEqualsForType((a, b) -> a.toString().equals(b.toString()), Address.class)14 .isEqualTo(person2);15 assertThat(person1).usingRecursiveComparison()16 .withEqualsForType((a, b) -> a.toString().equals(b.toString()), Address.class)17 .isNotEqualTo(person3);18 }19}20package com.mycompany.app;21class Person {22 private String name;23 private int age;24 private Address address;25 public Person(String name, int age, Address address) {26 this.name = name;27 this.age = age;28 this.address = address;29 }30 public String getName() {31 return name;32 }33 public int getAge() {34 return age;35 }36 public Address getAddress() {37 return address;38 }39}40package com.mycompany.app;41class Address {42 private String city;43 public Address(String city) {44 this.city = city;45 }46 public String toString() {47 return city;48 }49}

Full Screen

Full Screen

withEqualsForType

Using AI Code Generation

copy

Full Screen

1import java.util.ArrayList;2import java.util.List;3import java.util.Objects;4import org.assertj.core.api.Assertions;5import org.assertj.core.api.RecursiveComparisonAssert;6public class AssertJRecursiveComparisonAssert {7 public static void main(String[] args) {8 Employee employee = new Employee();9 employee.setId(1);10 employee.setName("John");11 employee.setSalary(1000);12 employee.setAddress("London");13 Employee employee2 = new Employee();14 employee2.setId(1);15 employee2.setName("John");16 employee2.setSalary(1000);17 employee2.setAddress("London");18 List<Employee> employees = new ArrayList<>();19 employees.add(employee);20 employees.add(employee2);21 List<Employee> employees2 = new ArrayList<>();22 employees2.add(employee);23 employees2.add(employee2);24 List<Employee> employees3 = new ArrayList<>();25 employees3.add(employee2);26 employees3.add(employee);27 RecursiveComparisonAssert recursiveComparisonAssert = Assertions.assertThat(employees).usingRecursiveComparison();28 recursiveComparisonAssert.isEqualTo(employees2);29 RecursiveComparisonAssert recursiveComparisonAssert2 = Assertions.assertThat(employees).usingRecursiveComparison();30 recursiveComparisonAssert2.isEqualTo(employees3);31 }32 static class Employee {33 private int id;34 private String name;35 private int salary;36 private String address;37 public int getId() {38 return id;39 }40 public void setId(int id) {41 this.id = id;42 }43 public String getName() {44 return name;45 }46 public void setName(String name) {47 this.name = name;48 }49 public int getSalary() {50 return salary;51 }52 public void setSalary(int salary) {53 this.salary = salary;54 }55 public String getAddress() {56 return address;57 }58 public void setAddress(String address) {59 this.address = address;60 }61 public int hashCode() {62 int hash = 7;63 hash = 67 * hash + this.id;64 hash = 67 * hash + Objects.hashCode(this.name);65 hash = 67 * hash + this.salary;66 hash = 67 * hash + Objects.hashCode(this.address);67 return hash;68 }69 public boolean equals(Object obj) {70 if (this == obj) {71 return true;72 }73 if (obj == null

Full Screen

Full Screen

withEqualsForType

Using AI Code Generation

copy

Full Screen

1import org.junit.Test;2import static org.assertj.core.api.Assertions.assertThat;3public class AssertJTest {4 public void testAssertJ() {5 Foo foo = new Foo();6 foo.setBar("bar");7 foo.setBaz("baz");8 Foo foo2 = new Foo();9 foo2.setBar("bar");10 foo2.setBaz("baz");11 assertThat(foo).usingRecursiveComparison()12 .withEqualsForType((actual, expected) -> true, String.class)13 .isEqualTo(foo2);14 assertThat(foo).usingRecursiveComparison()15 .withEqualsForType((actual, expected) -> false, String.class)16 .isEqualTo(foo2);17 }18}19public class Foo {20 String bar;21 String baz;22 public String getBar() {23 return bar;24 }25 public void setBar(String bar) {26 this.bar = bar;27 }28 public String getBaz() {29 return baz;30 }31 public void setBaz(String baz) {32 this.baz = baz;33 }34 public String toString() {35 return "Foo{" +36 '}';37 }38}39 Foo{bar='bar', baz='baz'}40 Foo{bar='bar', baz='baz'}41 at org.junit.Assert.fail(Assert.java:88)42 at org.junit.Assert.failNotEquals(Assert.java:834)43 at org.junit.Assert.assertNotEquals(Assert.java:1010)44 at org.junit.Assert.assertNotEquals(Assert.java:1031)45 at org.assertj.core.api.AbstractAssert.isNotEqualTo(AbstractAssert.java:167)46 at org.assertj.core.api.AbstractAssert.isNotEqualTo(AbstractAssert.java:152)47 at org.assertj.core.api.AbstractAssert.isNotEqualTo(AbstractAssert.java:146)48 at org.assertj.core.api.AbstractAssert.isEqualTo(AbstractAssert.java:136)49 at org.assertj.core.api.AbstractAssert.isEqualTo(AbstractAssert.java:125)50 at org.assertj.core.api.AbstractAssert.isEqualTo(AbstractAssert.java:60

Full Screen

Full Screen

withEqualsForType

Using AI Code Generation

copy

Full Screen

1import java.util.*;2import java.util.stream.Stream;3import java.util.stream.Collectors;4import java.util.Arrays;5import java.util.List;6import java.util.ArrayList;7import java.util.Map;8import java.util.HashMap;9import java.util.function.BiConsumer;10import java.util.function.BiFunction;11import java.util.function.Function;12import java.util.stream.Stream;13import java.util.stream.Collectors;14import java.util.Arrays;15import java.util.List;16import java.util.ArrayList;17import java.util.Map;18import java.util.HashMap;19import java.util.function.BiConsumer;20import java.util.function.BiFunction;21import java.util.function.Function;22import java.util.stream.Stream;23import java.util.stream.Collectors;24import java.util.Arrays;25import java.util.List;26import java.util.ArrayList;27import java.util.Map;28import java.util.HashMap;29import java.util.function.BiConsumer;30import java.util.function.BiFunction;31import java.util.function.Function;32import java.util.stream.Stream;33import java.util.stream.Collectors;34import java.util.Arrays;35import java.util.List;36import java.util.ArrayList;37import java.util.Map;38import java.util.HashMap;39import java.util.function.BiConsumer;40import java.util.function.BiFunction;41import java.util.function.Function;42import java.util.stream.Stream;43import java.util.stream.Collectors;44import java.util.Arrays;45import java.util.List;46import java.util.ArrayList;47import java.util.Map;48import java.util.HashMap;49import java.util.function.BiConsumer;50import java.util.function.BiFunction;51import java.util.function.Function;52import java.util.stream.Stream;53import java.util.stream.Collectors;54import java.util.Arrays;55import java.util.List;56import java.util.ArrayList;57import java.util.Map;58import java.util.HashMap;59import java.util.function.BiConsumer;60import java.util.function.BiFunction;61import java.util.function.Function;62import java.util.stream.Stream;63import java.util.stream.Collectors;64import java.util.Arrays;65import java.util.List;66import java.util.ArrayList;67import java.util.Map;68import java.util.HashMap;69import java.util.function.BiConsumer;70import java.util.function.BiFunction;71import java.util.function.Function;72import java.util.stream.Stream;73import java.util.stream.Collectors;74import java.util.Arrays;75import java.util.List;76import java.util.ArrayList;77import java.util.Map;78import java.util.HashMap;79import java.util.function.BiConsumer;80import java.util.function.BiFunction;81import java.util.function.Function;82import java.util.stream.Stream;83import java.util.stream.Collectors;84import85 public String toString() {86 return "Foo{" +87 '}';88 }89}90 Foo{bar='bar', baz='baz'}91 Foo{bar='bar', baz='baz'}92 at org.junit.Assert.fail(Assert.java:88)93 at org.junit.Assert.failNotEquals(Assert.java:834)94 at org.junit.Assert.assertNotEquals(Assert.java:1010)95 at org.junit.Assert.assertNotEquals(Assert.java:1031)96 at org.assertj.core.api.AbstractAssert.isNotEqualTo(AbstractAssert.java:167)97 at org.assertj.core.api.AbstractAssert.isNotEqualTo(AbstractAssert.java:152)98 at org.assertj.core.api.AbstractAssert.isNotEqualTo(AbstractAssert.java:146)99 at org.assertj.core.api.AbstractAssert.isEqualTo(AbstractAssert.java:136)100 at org.assertj.core.api.AbstractAssert.isEqualTo(AbstractAssert.java:125)101 at org.assertj.core.api.AbstractAssert.isEqualTo(AbstractAssert.java:60

Full Screen

Full Screen

withEqualsForType

Using AI Code Generation

copy

Full Screen

1import java.util.*;2import java.util.stream.Stream;3import java.util.stream.Collectors;4import java.util.Arrays;5import java.util.List;6import java.util.ArrayList;7import java.util.Map;8import java.util.HashMap;9import java.util.function.BiConsumer;10import java.util.function.BiFunction;11import java.util.function.Function;12import java.util.stream.Stream;13import java.util.stream.Collectors;14import java.util.Arrays;15import java.util.List;16import java.util.ArrayList;17import java.util.Map;18import java.util.HashMap;19import java.util.function.BiConsumer;20import java.util.function.BiFunction;21import java.util.function.Function;22import java.util.stream.Stream;23import java.util.stream.Collectors;24import java.util.Arrays;25import java.util.List;26import java.util.ArrayList;27import java.util.Map;28import java.util.HashMap;29import java.util.function.BiConsumer;30import java.util.function.BiFunction;31import java.util.function.Function;32import java.util.stream.Stream;33import java.util.stream.Collectors;34import java.util.Arrays;35import java.util.List;36import java.util.ArrayList;37import java.util.Map;38import java.util.HashMap;39import java.util.function.BiConsumer;40import java.util.function.BiFunction;41import java.util.function.Function;42import java.util.stream.Stream;43import java.util.stream.Collectors;44import java.util.Arrays;45import java.util.List;46import java.util.ArrayList;47import java.util.Map;48import java.util.HashMap;49import java.util.function.BiConsumer;50import java.util.function.BiFunction;51import java.util.function.Function;52import java.util.stream.Stream;53import java.util.stream.Collectors;54import java.util.Arrays;55import java.util.List;56import java.util.ArrayList;57import java.util.Map;58import java.util.HashMap;59import java.util.function.BiConsumer;60import java.util.function.BiFunction;61import java.util.function.Function;62import java.util.stream.Stream;63import java.util.stream.Collectors;64import java.util.Arrays;65import java.util.List;66import java.util.ArrayList;67import java.util.Map;68import java.util.HashMap;69import java.util.function.BiConsumer;70import java.util.function.BiFunction;71import java.util.function.Function;72import java.util.stream.Stream;73import java.util.stream.Collectors;74import java.util.Arrays;75import java.util.List;76import java.util.ArrayList;77import java.util.Map;78import java.util.HashMap;79import java.util.function.BiConsumer;80import java.util.function.BiFunction;81import java.util.function.Function;82import java.util.stream.Stream;83import java.util.stream.Collectors;84import

Full Screen

Full Screen

withEqualsForType

Using AI Code Generation

copy

Full Screen

1import java.util.*;2import java.util.stream.Stream;3import java.util.stream.Collectors;4import java.util.Arrays;5import java.util.List;6import java.util.ArrayList;7import java.util.Map;8import java.util.HashMap;9import java.util.function.BiConsumer;10import java.util.function.BiFunction;11import java.util.function.Function;12import java.util.stream.Stream;13import java.util.stream.Collectors;14import java.util.Arrays;15import java.util.List;16import java.util.ArrayList;17import java.util.Map;18import java.util.HashMap;19import java.util.function.BiConsumer;20import java.util.function.BiFunction;21import java.util.function.Function;22import java.util.stream.Stream;23import java.util.stream.Collectors;24import java.util.Arrays;25import java.util.List;26import java.util.ArrayList;27import java.util.Map;28import java.util.HashMap;29import java.util.function.BiConsumer;30import java.util.function.BiFunction;31import java.util.function.Function;32import java.util.stream.Stream;33import java.util.stream.Collectors;34import java.util.Arrays;35import java.util.List;36import java.util.ArrayList;37import java.util.Map;38import java.util.HashMap;39import java.util.function.BiConsumer;40import java.util.function.BiFunction;41import java.util.function.Function;42import java.util.stream.Stream;43import java.util.stream.Collectors;44import java.util.Arrays;45import java.util.List;46import java.util.ArrayList;47import java.util.Map;48import java.util.HashMap;49import java.util.function.BiConsumer;50import java.util.function.BiFunction;51import java.util.function.Function;52import java.util.stream.Stream;53import java.util.stream.Collectors;54import java.util.Arrays;55import java.util.List;56import java.util.ArrayList;57import java.util.Map;58import java.util.HashMap;59import java.util.function.BiConsumer;60import java.util.function.BiFunction;61import java.util.function.Function;62import java.util.stream.Stream;63import java.util.stream.Collectors;64import java.util.Arrays;65import java.util.List;66import java.util.ArrayList;67import java.util.Map;68import java.util.HashMap;69import java.util.function.BiConsumer;70import java.util.function.BiFunction;71import java.util.function.Function;72import java.util.stream.Stream;73import java.util.stream.Collectors;74import java.util.Arrays;75import java.util.List;76import java.util.ArrayList;77import java.util.Map;78import java.util.HashMap;79import java.util.function.BiConsumer;80import java.util.function.BiFunction;81import java.util.function.Function;82import java.util.stream.Stream;83import java.util.stream.Collectors;84importName;85 }86 public int getAge() {87 return age;88 }89 }90}

Full Screen

Full Screen

withEqualsForType

Using AI Code Generation

copy

Full Screen

1import org.assertj.core.api.Assertions;2import org.junit.Test;3public class RecursiveComparisonAssertTest {4 public void test() {5 Assertions.assertThat(new Object()).usingRecursiveComparison().withEqualsForType("abc", String.class).isEqualTo(new Object());6 }7}

Full Screen

Full Screen

withEqualsForType

Using AI Code Generation

copy

Full Screen

1import org.junit.Test;2import static org.assertj.core.api.Assertions.assertThat;3public class AssertJTest {4 public void testAssertJ() {5 Foo foo = new Foo();6 foo.setBar("bar");7 foo.setBaz("baz");8 Foo foo2 = new Foo();9 foo2.setBar("bar");10 foo2.setBaz("baz");11 assertThat(foo).usingRecursiveComparison()12 .withEqualsForType((actual, expected) -> true, String.class)13 .isEqualTo(foo2);14 assertThat(foo).usingRecursiveComparison()15 .withEqualsForType((actual, expected) -> false, String.class)16 .isEqualTo(foo2);17 }18}19public class Foo {20 String bar;21 String baz;22 public String getBar() {23 return bar;24 }25 public void setBar(String bar) {26 this.bar = bar;27 }28 public String getBaz() {29 return baz;30 }31 public void setBaz(String baz) {32 this.baz = baz;33 }34 public String toString() {35 return "Foo{" +36 '}';37 }38}39 Foo{bar='bar', baz='baz'}40 Foo{bar='bar', baz='baz'}41 at org.junit.Assert.fail(Assert.java:88)42 at org.junit.Assert.failNotEquals(Assert.java:834)43 at org.junit.Assert.assertNotEquals(Assert.java:1010)44 at org.junit.Assert.assertNotEquals(Assert.java:1031)45 at org.assertj.core.api.AbstractAssert.isNotEqualTo(AbstractAssert.java:167)46 at org.assertj.core.api.AbstractAssert.isNotEqualTo(AbstractAssert.java:152)47 at org.assertj.core.api.AbstractAssert.isNotEqualTo(AbstractAssert.java:146)48 at org.assertj.core.api.AbstractAssert.isEqualTo(AbstractAssert.java:136)49 at org.assertj.core.api.AbstractAssert.isEqualTo(AbstractAssert.java:125)50 at org.assertj.core.api.AbstractAssert.isEqualTo(AbstractAssert.java:60

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.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful