Best Assertj code snippet using org.assertj.core.api.RecursiveComparisonAssert.withEqualsForFieldsMatchingRegexes
Source:RecursiveComparisonConfiguration.java
...390 * ones registered with exact location match: {@link #registerEqualsForFields(BiPredicate, String...)} or {@link #registerComparatorForFields(Comparator, String...)}391 * <p>392 * If registered regexes for different {@link BiPredicate} match a given field, the latest registered regexes {@link BiPredicate} wins.393 * <p>394 * Example: see {@link RecursiveComparisonAssert#withEqualsForFieldsMatchingRegexes(BiPredicate, String...)}395 *396 * @param equals the {@link BiPredicate} to use to compare the fields matching the given regexes397 * @param regexes the regexes from the root object of the fields location the BiPredicate should be used for398 *399 * @throws NullPointerException if the given BiPredicate is null.400 * @since 3.24.0401 */402 public void registerEqualsForFieldsMatchingRegexes(BiPredicate<?, ?> equals, String... regexes) {403 fieldComparators.registerComparatorForFieldsMatchingRegexes(regexes, toComparator(equals));404 }405 /**406 * Registers the giving message which would be shown when differences in the given fields while comparison occurred.407 * <p>408 * The fields must be specified from the root object, for example if {@code Foo} has a {@code Bar} field and both409 * have an {@code id} field, one can register a message for Foo and Bar's {@code id} by calling:410 * <pre><code class='java'> registerErrorMessageForFields("some message", "foo.id", "foo.bar.id")</code></pre>411 * <p>412 * Messages registered with this method have precedence over the ones registered with {@link #registerErrorMessageForType(String, Class)}.413 * <p>414 * In case of {@code null} as message the default error message will be used (See {@link ComparisonDifference#DEFAULT_TEMPLATE}).415 *416 * @param message the error message that will be thrown when comparison error occurred417 * @param fieldLocations the field locations the error message should be used for418 */419 public void registerErrorMessageForFields(String message, String... fieldLocations) {420 Stream.of(fieldLocations).forEach(fieldLocation -> fieldMessages.registerMessage(fieldLocation, message));421 }422 /**423 * Registers the giving message which would be shown when differences for the giving type while comparison424 * occurred.425 * <p>426 * Message registered with this method have less precedence than the ones registered with {@link #registerErrorMessageForFields(String, String...)}.427 * <p>428 * In case of {@code null} as message the default error message will be used (See {@link ComparisonDifference#DEFAULT_TEMPLATE}).429 *430 * @param message the error message that will be thrown when comparison error occurred431 * @param clazz the type the error message should be used for432 */433 public void registerErrorMessageForType(String message, Class<?> clazz) {434 typeMessages.registerMessage(clazz, message);435 }436 /**437 * Sets whether the recursive comparison will check that actual's type is compatible with expected's type (the same applies for each field).438 * Compatible means that the expected's type is the same or a subclass of actual's type.439 * <p>440 * See {@link RecursiveComparisonAssert#withStrictTypeChecking()} for code examples.441 *442 * @param strictTypeChecking whether the recursive comparison will check that actual's type is compatible with expected's type.443 */444 public void strictTypeChecking(boolean strictTypeChecking) {445 this.strictTypeChecking = strictTypeChecking;446 }447 public boolean isInStrictTypeCheckingMode() {448 return strictTypeChecking;449 }450 public List<Class<?>> getIgnoredOverriddenEqualsForTypes() {451 return ignoredOverriddenEqualsForTypes;452 }453 public List<String> getIgnoredOverriddenEqualsForFields() {454 return ignoredOverriddenEqualsForFields;455 }456 public List<Pattern> getIgnoredOverriddenEqualsForFieldsMatchingRegexes() {457 return ignoredOverriddenEqualsForFieldsMatchingRegexes;458 }459 public Stream<Entry<String, Comparator<?>>> comparatorByFields() {460 return fieldComparators.comparatorByFields();461 }462 @Override463 public String toString() {464 return multiLineDescription(CONFIGURATION_PROVIDER.representation());465 }466 @Override467 public int hashCode() {468 return java.util.Objects.hash(fieldComparators, ignoreAllActualEmptyOptionalFields, ignoreAllActualNullFields,469 ignoreAllExpectedNullFields, ignoreAllOverriddenEquals, ignoreCollectionOrder,470 ignoredCollectionOrderInFields, ignoredCollectionOrderInFieldsMatchingRegexes,471 getIgnoredFields(),472 getIgnoredFieldsRegexes(), ignoredOverriddenEqualsForFields,473 ignoredOverriddenEqualsForTypes,474 ignoredOverriddenEqualsForFieldsMatchingRegexes, getIgnoredTypes(), strictTypeChecking,475 typeComparators, comparedFields, fieldMessages, typeMessages);476 }477 @Override478 public boolean equals(Object obj) {479 if (this == obj) return true;480 if (obj == null) return false;481 if (getClass() != obj.getClass()) return false;482 RecursiveComparisonConfiguration other = (RecursiveComparisonConfiguration) obj;483 return java.util.Objects.equals(fieldComparators, other.fieldComparators)484 && ignoreAllActualEmptyOptionalFields == other.ignoreAllActualEmptyOptionalFields485 && ignoreAllActualNullFields == other.ignoreAllActualNullFields486 && ignoreAllExpectedNullFields == other.ignoreAllExpectedNullFields487 && ignoreAllOverriddenEquals == other.ignoreAllOverriddenEquals488 && ignoreCollectionOrder == other.ignoreCollectionOrder489 && java.util.Objects.equals(ignoredCollectionOrderInFields, other.ignoredCollectionOrderInFields)490 && java.util.Objects.equals(getIgnoredFields(), other.getIgnoredFields())491 && java.util.Objects.equals(comparedFields, other.comparedFields)492 && java.util.Objects.equals(getIgnoredFieldsRegexes(), other.getIgnoredFieldsRegexes())493 && java.util.Objects.equals(ignoredOverriddenEqualsForFields, other.ignoredOverriddenEqualsForFields)494 && java.util.Objects.equals(ignoredOverriddenEqualsForTypes, other.ignoredOverriddenEqualsForTypes)495 && java.util.Objects.equals(ignoredOverriddenEqualsForFieldsMatchingRegexes,496 other.ignoredOverriddenEqualsForFieldsMatchingRegexes)497 && java.util.Objects.equals(getIgnoredTypes(), other.getIgnoredTypes())498 && strictTypeChecking == other.strictTypeChecking499 && java.util.Objects.equals(typeComparators, other.typeComparators)500 && java.util.Objects.equals(ignoredCollectionOrderInFieldsMatchingRegexes,501 other.ignoredCollectionOrderInFieldsMatchingRegexes)502 && java.util.Objects.equals(fieldMessages, other.fieldMessages)503 && java.util.Objects.equals(typeMessages, other.typeMessages);504 }505 public String multiLineDescription(Representation representation) {506 StringBuilder description = new StringBuilder();507 describeIgnoreAllActualNullFields(description);508 describeIgnoreAllActualEmptyOptionalFields(description);509 describeIgnoreAllExpectedNullFields(description);510 describeComparedFields(description);511 describeIgnoredFields(description);512 describeIgnoredFieldsRegexes(description);513 describeIgnoredFieldsForTypes(description);514 describeOverriddenEqualsMethodsUsage(description, representation);515 describeIgnoreCollectionOrder(description);516 describeIgnoredCollectionOrderInFields(description);517 describeIgnoredCollectionOrderInFieldsMatchingRegexes(description);518 describeRegisteredComparatorByTypes(description);519 describeRegisteredComparatorForFields(description);520 describeTypeCheckingStrictness(description);521 describeRegisteredErrorMessagesForFields(description);522 describeRegisteredErrorMessagesForTypes(description);523 return description.toString();524 }525 boolean shouldIgnore(DualValue dualValue) {526 return shouldIgnoreFieldBasedOnFieldLocation(dualValue.fieldLocation) || shouldIgnoreFieldBasedOnFieldValue(dualValue);527 }528 private boolean shouldBeCompared(FieldLocation fieldLocation) {529 // empty comparedFields <=> no restriction on compared fields <=> must be compared530 if (comparedFields.isEmpty()) return true;531 return comparedFields.stream().anyMatch(matchesComparedField(fieldLocation));532 }533 private static Predicate<FieldLocation> matchesComparedField(FieldLocation field) {534 // a field f must be compared if any compared fields is f itself (obviously), a parent of f or a child of f.535 // - "name.first" must be compared if "name" is a compared field so will other "name" subfields like "name.last"536 // - "name" must be compared if "name.first" is a compared field otherwise "name" is ignored and "name.first" too537 return comparedField -> field.matches(comparedField) // exact match538 || field.hasParent(comparedField) // ex: field "name.first" and "name" compared field539 || field.hasChild(comparedField); // ex: field "name" and "name.first" compared field540 }541 Set<String> getActualFieldNamesToCompare(DualValue dualValue) {542 Set<String> actualFieldsNames = Objects.getFieldsNames(dualValue.actual.getClass());543 // we are doing the same as shouldIgnore(DualValue dualValue) but in two steps for performance reasons:544 // - we filter first ignored field by names that don't need building DualValues545 // - then we filter field DualValues with the remaining criteria that need to get the field value546 // DualValues are built introspecting fields which is expensive.547 return actualFieldsNames.stream()548 // evaluate field name ignoring criteria on dualValue field location + field name549 .filter(fieldName -> !shouldIgnoreFieldBasedOnFieldLocation(dualValue.fieldLocation.field(fieldName)))550 .map(fieldName -> dualValueForField(dualValue, fieldName))551 // evaluate field value ignoring criteria552 .filter(fieldDualValue -> !shouldIgnoreFieldBasedOnFieldValue(fieldDualValue))553 // back to field name554 .map(DualValue::getFieldName)555 .filter(fieldName -> !fieldName.isEmpty())556 .collect(toSet());557 }558 // non accessible stuff559 private boolean shouldIgnoreFieldBasedOnFieldValue(DualValue dualValue) {560 return matchesAnIgnoredNullField(dualValue)561 || matchesAnIgnoredFieldType(dualValue)562 || matchesAnIgnoredEmptyOptionalField(dualValue);563 }564 private boolean shouldIgnoreFieldBasedOnFieldLocation(FieldLocation fieldLocation) {565 return !shouldBeCompared(fieldLocation) || matchesAnIgnoredField(fieldLocation) || matchesAnIgnoredFieldRegex(fieldLocation);566 }567 private static DualValue dualValueForField(DualValue parentDualValue, String fieldName) {568 Object actualFieldValue = COMPARISON.getSimpleValue(fieldName, parentDualValue.actual);569 // no guarantees we have a field in expected named as fieldName570 Object expectedFieldValue;571 try {572 expectedFieldValue = COMPARISON.getSimpleValue(fieldName, parentDualValue.expected);573 } catch (@SuppressWarnings("unused") Exception e) {574 // set the field to null to express it is absent, this not 100% accurate as the value could be null,575 // but it works to evaluate if dualValue should be ignored with matchesAnIgnoredFieldType576 expectedFieldValue = null;577 }578 FieldLocation fieldLocation = parentDualValue.fieldLocation.field(fieldName);579 return new DualValue(fieldLocation, actualFieldValue, expectedFieldValue);580 }581 boolean hasCustomComparator(DualValue dualValue) {582 String fieldName = dualValue.getConcatenatedPath();583 if (hasComparatorForField(fieldName)) return true;584 if (dualValue.actual == null && dualValue.expected == null) return false;585 // best effort assuming actual and expected have the same type (not 100% true as we can compare object of different types)586 Class<?> valueType = dualValue.actual != null ? dualValue.actual.getClass() : dualValue.expected.getClass();587 return hasComparatorForType(valueType);588 }589 boolean shouldIgnoreOverriddenEqualsOf(DualValue dualValue) {590 // we must compare java basic types otherwise the recursive comparison loops infinitely!591 if (dualValue.isActualJavaType()) return false;592 // enums don't have fields, comparing them field by field makes no sense, we need to use equals which is overridden and final593 if (dualValue.isActualAnEnum()) return false;594 return ignoreAllOverriddenEquals595 || matchesAnIgnoredOverriddenEqualsField(dualValue.fieldLocation)596 || (dualValue.actual != null && shouldIgnoreOverriddenEqualsOf(dualValue.actual.getClass()));597 }598 @VisibleForTesting599 boolean shouldIgnoreOverriddenEqualsOf(Class<?> clazz) {600 return matchesAnIgnoredOverriddenEqualsType(clazz);601 }602 boolean shouldIgnoreCollectionOrder(FieldLocation fieldLocation) {603 return ignoreCollectionOrder604 || matchesAnIgnoredCollectionOrderInField(fieldLocation)605 || matchesAnIgnoredCollectionOrderInFieldRegex(fieldLocation);606 }607 private void describeComparedFields(StringBuilder description) {608 if (!comparedFields.isEmpty())609 description.append(format("- the comparison was performed on the following fields: %s%n", describeComparedFields()));610 }611 private void describeIgnoredFieldsForTypes(StringBuilder description) {612 if (!getIgnoredTypes().isEmpty())613 description.append(format("- the following types were ignored in the comparison: %s%n", describeIgnoredTypes()));614 }615 protected void describeIgnoreAllActualNullFields(StringBuilder description) {616 if (ignoreAllActualNullFields) description.append(format("- all actual null fields were ignored in the comparison%n"));617 }618 protected void describeIgnoreAllActualEmptyOptionalFields(StringBuilder description) {619 if (ignoreAllActualEmptyOptionalFields)620 description.append(format(621 "- all actual empty optional fields were ignored in the comparison (including Optional, OptionalInt, OptionalLong and OptionalDouble)%n"));622 }623 private void describeIgnoreAllExpectedNullFields(StringBuilder description) {624 if (ignoreAllExpectedNullFields) description.append(format("- all expected null fields were ignored in the comparison%n"));625 }626 private void describeOverriddenEqualsMethodsUsage(StringBuilder description, Representation representation) {627 String header = ignoreAllOverriddenEquals628 ? "- no overridden equals methods were used in the comparison (except for java types)"629 : "- overridden equals methods were used in the comparison";630 description.append(header);631 if (isConfiguredToIgnoreSomeButNotAllOverriddenEqualsMethods()) {632 description.append(format(" except for:%n"));633 describeIgnoredOverriddenEqualsMethods(description, representation);634 } else {635 description.append(format("%n"));636 }637 }638 private void describeIgnoredOverriddenEqualsMethods(StringBuilder description, Representation representation) {639 if (!ignoredOverriddenEqualsForFields.isEmpty())640 description.append(format("%s the following fields: %s%n", INDENT_LEVEL_2,641 describeIgnoredOverriddenEqualsForFields()));642 if (!ignoredOverriddenEqualsForTypes.isEmpty())643 description.append(format("%s the following types: %s%n", INDENT_LEVEL_2,644 describeIgnoredOverriddenEqualsForTypes(representation)));645 if (!ignoredOverriddenEqualsForFieldsMatchingRegexes.isEmpty())646 description.append(format("%s the fields matching the following regexes: %s%n", INDENT_LEVEL_2,647 describeRegexes(ignoredOverriddenEqualsForFieldsMatchingRegexes)));648 }649 private String describeIgnoredOverriddenEqualsForTypes(Representation representation) {650 List<String> fieldsDescription = ignoredOverriddenEqualsForTypes.stream()651 .map(representation::toStringOf)652 .collect(toList());653 return join(fieldsDescription);654 }655 private String describeIgnoredOverriddenEqualsForFields() {656 return join(ignoredOverriddenEqualsForFields);657 }658 private void describeIgnoreCollectionOrder(StringBuilder description) {659 if (ignoreCollectionOrder) description.append(format("- collection order was ignored in all fields in the comparison%n"));660 }661 private void describeIgnoredCollectionOrderInFields(StringBuilder description) {662 if (!ignoredCollectionOrderInFields.isEmpty())663 description.append(format("- collection order was ignored in the following fields in the comparison: %s%n",664 describeIgnoredCollectionOrderInFields()));665 }666 private void describeIgnoredCollectionOrderInFieldsMatchingRegexes(StringBuilder description) {667 if (!ignoredCollectionOrderInFieldsMatchingRegexes.isEmpty())668 description.append(669 format("- collection order was ignored in the fields matching the following regexes in the comparison: %s%n",670 describeRegexes(ignoredCollectionOrderInFieldsMatchingRegexes)));671 }672 private boolean matchesAnIgnoredOverriddenEqualsRegex(FieldLocation fieldLocation) {673 if (ignoredOverriddenEqualsForFieldsMatchingRegexes.isEmpty()) return false; // shortcut674 String pathToUseInRules = fieldLocation.getPathToUseInRules();675 return ignoredOverriddenEqualsForFieldsMatchingRegexes.stream().anyMatch(regex -> regex.matcher(pathToUseInRules).matches());676 }677 private boolean matchesAnIgnoredOverriddenEqualsType(Class<?> clazz) {678 return ignoredOverriddenEqualsForTypes.contains(clazz);679 }680 private boolean matchesAnIgnoredOverriddenEqualsField(FieldLocation fieldLocation) {681 return ignoredOverriddenEqualsForFields.stream().anyMatch(fieldLocation::matches)682 || matchesAnIgnoredOverriddenEqualsRegex(fieldLocation);683 }684 private boolean matchesAnIgnoredNullField(DualValue dualValue) {685 return (ignoreAllActualNullFields && dualValue.actual == null)686 || (ignoreAllExpectedNullFields && dualValue.expected == null);687 }688 private boolean matchesAnIgnoredEmptyOptionalField(DualValue dualValue) {689 return ignoreAllActualEmptyOptionalFields690 && dualValue.isActualFieldAnEmptyOptionalOfAnyType();691 }692 private boolean matchesAnIgnoredFieldType(DualValue dualValue) {693 Object actual = dualValue.actual;694 if (actual != null) return getIgnoredTypes().contains(actual.getClass());695 Object expected = dualValue.expected;696 // actual is null => we can't evaluate its type, we can only reliably check dualValue.expected's type if697 // strictTypeChecking is enabled which guarantees expected is of the same type.698 if (strictTypeChecking && expected != null) return getIgnoredTypes().contains(expected.getClass());699 // if strictTypeChecking is disabled, we can't safely ignore the field (if we did, we would ignore all null fields!).700 return false;701 }702 private boolean matchesAnIgnoredCollectionOrderInField(FieldLocation fieldLocation) {703 return ignoredCollectionOrderInFields.stream().anyMatch(fieldLocation::matches);704 }705 private boolean matchesAnIgnoredCollectionOrderInFieldRegex(FieldLocation fieldLocation) {706 String pathToUseInRules = fieldLocation.getPathToUseInRules();707 return ignoredCollectionOrderInFieldsMatchingRegexes.stream().anyMatch(regex -> regex.matcher(pathToUseInRules).matches());708 }709 private String describeComparedFields() {710 return join(comparedFields.stream().map(FieldLocation::shortDescription).collect(toList()));711 }712 private String describeIgnoredCollectionOrderInFields() {713 return join(ignoredCollectionOrderInFields);714 }715 private boolean isConfiguredToIgnoreSomeButNotAllOverriddenEqualsMethods() {716 boolean ignoreSomeOverriddenEqualsMethods = !ignoredOverriddenEqualsForFieldsMatchingRegexes.isEmpty()717 || !ignoredOverriddenEqualsForTypes.isEmpty()718 || !ignoredOverriddenEqualsForFields.isEmpty();719 return !ignoreAllOverriddenEquals && ignoreSomeOverriddenEqualsMethods;720 }721 private void describeRegisteredComparatorByTypes(StringBuilder description) {722 if (!typeComparators.isEmpty()) {723 description.append(format("- these types were compared with the following comparators:%n"));724 describeComparatorForTypes(description);725 }726 }727 private void describeComparatorForTypes(StringBuilder description) {728 typeComparators.comparatorByTypes()729 .map(this::formatRegisteredComparatorByType)730 .forEach(description::append);731 }732 private String formatRegisteredComparatorByType(Entry<Class<?>, Comparator<?>> next) {733 return format("%s %s -> %s%n", INDENT_LEVEL_2, next.getKey().getName(), next.getValue());734 }735 private void describeRegisteredComparatorForFields(StringBuilder description) {736 if (!fieldComparators.isEmpty()) {737 if (fieldComparators.hasFieldComparators()) {738 description.append(format("- these fields were compared with the following comparators:%n"));739 describeComparatorForFields(description);740 }741 if (fieldComparators.hasRegexFieldComparators()) {742 description.append(format("- the fields matching these regexes were compared with the following comparators:%n"));743 describeComparatorForRegexFields(description);744 }745 if (fieldComparators.hasFieldComparators() && fieldComparators.hasRegexFieldComparators()) {746 description.append(format("- field comparators take precedence over regex field matching comparators.%n"));747 }748 if (!typeComparators.isEmpty()) {749 description.append(format("- field comparators take precedence over type comparators.%n"));750 }751 }752 }753 private void describeComparatorForFields(StringBuilder description) {754 fieldComparators.comparatorByFields()755 .map(this::formatRegisteredComparatorForField)756 .forEach(description::append);757 }758 private void describeComparatorForRegexFields(StringBuilder description) {759 fieldComparators.comparatorByRegexFields()760 .map(this::formatRegisteredComparatorForRegexFields)761 .sorted()762 .forEach(description::append);763 }764 private String formatRegisteredComparatorForField(Entry<String, Comparator<?>> comparatorForField) {765 return format("%s %s -> %s%n", INDENT_LEVEL_2, comparatorForField.getKey(), comparatorForField.getValue());766 }767 private String formatRegisteredComparatorForRegexFields(Entry<List<Pattern>, Comparator<?>> comparatorForRegexFields) {768 return format("%s %s -> %s%n", INDENT_LEVEL_2, comparatorForRegexFields.getKey(), comparatorForRegexFields.getValue());769 }770 private void describeTypeCheckingStrictness(StringBuilder description) {771 String str = strictTypeChecking772 ? "- 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"773 : "- 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";774 description.append(format(str));775 }776 private void describeRegisteredErrorMessagesForFields(StringBuilder description) {777 if (!fieldMessages.isEmpty()) {778 description.append(format("- these fields had overridden error messages:%n"));779 describeErrorMessagesForFields(description);780 if (!typeMessages.isEmpty()) {781 description.append(format("- field custom messages take precedence over type messages.%n"));782 }783 }784 }785 private void describeErrorMessagesForFields(StringBuilder description) {786 String fields = fieldMessages.messageByFields()787 .map(Entry::getKey)788 .collect(joining(DEFAULT_DELIMITER));789 description.append(format("%s %s%n", INDENT_LEVEL_2, fields));790 }791 private void describeRegisteredErrorMessagesForTypes(StringBuilder description) {792 if (!typeMessages.isEmpty()) {793 description.append("- these types had overridden error messages:%n");794 describeErrorMessagesForType(description);795 }796 }797 private void describeErrorMessagesForType(StringBuilder description) {798 String types = typeMessages.messageByTypes()799 .map(it -> it.getKey().getName())800 .collect(joining(DEFAULT_DELIMITER));801 description.append(format("%s %s%n", INDENT_LEVEL_2, types));802 }803 /**804 * Creates builder to build {@link RecursiveComparisonConfiguration}.805 * @return created builder806 */807 public static Builder builder() {808 return new Builder();809 }810 /**811 * Builder to build {@link RecursiveComparisonConfiguration}.812 */813 public static final class Builder extends AbstractBuilder<Builder> {814 private boolean strictTypeChecking;815 private boolean ignoreAllActualNullFields;816 private boolean ignoreAllActualEmptyOptionalFields;817 private boolean ignoreAllExpectedNullFields;818 private FieldLocation[] comparedFields = {};819 private Class<?>[] ignoredOverriddenEqualsForTypes = {};820 private String[] ignoredOverriddenEqualsForFields = {};821 private String[] ignoredOverriddenEqualsForFieldsMatchingRegexes = {};822 private boolean ignoreAllOverriddenEquals = DEFAULT_IGNORE_ALL_OVERRIDDEN_EQUALS;823 private boolean ignoreCollectionOrder;824 private String[] ignoredCollectionOrderInFields = {};825 private String[] ignoredCollectionOrderInFieldsMatchingRegexes = {};826 private final TypeComparators typeComparators = defaultTypeComparators();827 private final FieldComparators fieldComparators = new FieldComparators();828 private final FieldMessages fieldMessages = new FieldMessages();829 private final TypeMessages typeMessages = new TypeMessages();830 private Builder() {831 super(Builder.class);832 }833 /**834 * Sets whether the recursive comparison will check that actual's type is compatible with expected's type (the same applies for each field).835 * Compatible means that the expected's type is the same or a subclass of actual's type.836 * <p>837 * See {@link RecursiveComparisonAssert#withStrictTypeChecking()} for code examples.838 *839 * @param strictTypeChecking whether the recursive comparison will check that actual's type is compatible with expected's type.840 * @return this builder.841 */842 public Builder withStrictTypeChecking(boolean strictTypeChecking) {843 this.strictTypeChecking = strictTypeChecking;844 return this;845 }846 /**847 * Sets whether actual null fields are ignored in the recursive comparison.848 * <p>849 * See {@link RecursiveComparisonAssert#ignoringActualNullFields()} for code examples.850 *851 * @param ignoreAllActualNullFields whether to ignore actual null fields in the recursive comparison852 * @return this builder.853 */854 public Builder withIgnoreAllActualNullFields(boolean ignoreAllActualNullFields) {855 this.ignoreAllActualNullFields = ignoreAllActualNullFields;856 return this;857 }858 /**859 * Sets whether actual empty optional fields are ignored in the recursive comparison.860 * <p>861 * See {@link RecursiveComparisonAssert#ignoringActualEmptyOptionalFields()} for code examples.862 *863 * @param ignoreAllActualEmptyOptionalFields whether to ignore actual empty optional fields in the recursive comparison864 * @return this builder.865 */866 public Builder withIgnoreAllActualEmptyOptionalFields(boolean ignoreAllActualEmptyOptionalFields) {867 this.ignoreAllActualEmptyOptionalFields = ignoreAllActualEmptyOptionalFields;868 return this;869 }870 /**871 * Sets whether expected null fields are ignored in the recursive comparison.872 * <p>873 * See {@link RecursiveComparisonAssert#ignoringExpectedNullFields()} for code examples.874 *875 * @param ignoreAllExpectedNullFields whether to ignore expected null fields in the recursive comparison876 * @return this builder.877 */878 public Builder withIgnoreAllExpectedNullFields(boolean ignoreAllExpectedNullFields) {879 this.ignoreAllExpectedNullFields = ignoreAllExpectedNullFields;880 return this;881 }882 /**883 * Adds the given fields to the set of fields from the object under test to compare in the recursive comparison.884 * <p>885 * Nested fields can be specified like this: home.address.street.886 * <p>887 * See {@link RecursiveComparisonAssert#comparingOnlyFields(String...)} for examples.888 *889 * @param fieldsToCompare the fields of the object under test to compare.890 * @return this builder.891 */892 public Builder withComparedFields(String... fieldsToCompare) {893 this.comparedFields = Stream.of(fieldsToCompare).map(FieldLocation::new).toArray(FieldLocation[]::new);894 return this;895 }896 /**897 * Adds the given types to the list of types to force a recursive comparison on.898 * <p>899 * See {@link RecursiveComparisonAssert#ignoringOverriddenEqualsForTypes(Class...) RecursiveComparisonAssert#ignoringOverriddenEqualsForTypes(Class...)} for examples.900 *901 * @param types the types to the list of types to force a recursive comparison on.902 * @return this builder.903 */904 public Builder withIgnoredOverriddenEqualsForTypes(Class<?>... types) {905 this.ignoredOverriddenEqualsForTypes = types;906 return this;907 }908 /**909 * Adds the given fields to the list of fields to force a recursive comparison on.910 * <p>911 * See {@link RecursiveComparisonAssert#ignoringOverriddenEqualsForFields(String...) RecursiveComparisonAssert#ignoringOverriddenEqualsForFields(String...)} for examples.912 *913 * @param fields the fields to force a recursive comparison on.914 * @return this builder.915 */916 public Builder withIgnoredOverriddenEqualsForFields(String... fields) {917 this.ignoredOverriddenEqualsForFields = fields;918 return this;919 }920 /**921 * Adds the given regexes to the list of regexes used find the fields to force a recursive comparison on.922 * <p>923 * See {@link RecursiveComparisonAssert#ignoringOverriddenEqualsForFieldsMatchingRegexes(String...) RecursiveComparisonAssert#ignoringOverriddenEqualsForFieldsMatchingRegexes(String...)} for examples.924 *925 * @param regexes regexes used to specify the fields we want to force a recursive comparison on.926 * @return this builder.927 */928 public Builder withIgnoredOverriddenEqualsForFieldsMatchingRegexes(String... regexes) {929 this.ignoredOverriddenEqualsForFieldsMatchingRegexes = regexes;930 return this;931 }932 /**933 * Force a recursive comparison on all fields (except java types) if true.934 * <p>935 * See {@link RecursiveComparisonAssert#ignoringAllOverriddenEquals()} for examples.936 *937 * @param ignoreAllOverriddenEquals whether to force a recursive comparison on all fields (except java types) or not.938 * @return this builder.939 */940 public Builder withIgnoreAllOverriddenEquals(boolean ignoreAllOverriddenEquals) {941 this.ignoreAllOverriddenEquals = ignoreAllOverriddenEquals;942 return this;943 }944 /**945 * Sets whether to ignore collection order in the comparison.946 * <p>947 * See {@link RecursiveComparisonAssert#ignoringCollectionOrder()} for code examples.948 *949 * @param ignoreCollectionOrder whether to ignore collection order in the comparison.950 * @return this builder.951 */952 public Builder withIgnoreCollectionOrder(boolean ignoreCollectionOrder) {953 this.ignoreCollectionOrder = ignoreCollectionOrder;954 return this;955 }956 /**957 * Adds the given fields to the list fields from the object under test to ignore collection order in the recursive comparison.958 * <p>959 * See {@link RecursiveComparisonAssert#ignoringCollectionOrderInFields(String...) RecursiveComparisonAssert#ignoringCollectionOrderInFields(String...)} for examples.960 *961 * @param fieldsToIgnoreCollectionOrder the fields of the object under test to ignore collection order in the comparison.962 * @return this builder.963 */964 public Builder withIgnoredCollectionOrderInFields(String... fieldsToIgnoreCollectionOrder) {965 this.ignoredCollectionOrderInFields = fieldsToIgnoreCollectionOrder;966 return this;967 }968 /**969 * 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.970 * <p>971 * See {@link RecursiveComparisonAssert#ignoringCollectionOrderInFieldsMatchingRegexes(String...) RecursiveComparisonAssert#ignoringCollectionOrderInFieldsMatchingRegexes(String...)} for examples.972 *973 * @param regexes regexes used to find the object under test fields to ignore collection order in the comparison.974 * @return this builder.975 */976 public Builder withIgnoredCollectionOrderInFieldsMatchingRegexes(String... regexes) {977 this.ignoredCollectionOrderInFieldsMatchingRegexes = regexes;978 return this;979 }980 /**981 * Registers the given {@link Comparator} to compare the fields with the given type.982 * <p>983 * Comparators registered with this method have less precedence than comparators registered with {@link #withComparatorForFields(Comparator, String...)}984 * or BiPredicate registered with {@link #withEqualsForFields(BiPredicate, String...)}.985 * <p>986 * Note that registering a {@link Comparator} for a given type will override the previously registered BiPredicate/Comparator (if any).987 * <p>988 * See {@link RecursiveComparisonAssert#withComparatorForType(Comparator, Class)} for examples.989 *990 * @param <T> the class type to register a comparator for991 * @param comparator the {@link java.util.Comparator Comparator} to use to compare the given field992 * @param type the type to be compared with the given comparator.993 * @return this builder.994 * @throws NullPointerException if the given Comparator is null.995 */996 public <T> Builder withComparatorForType(Comparator<? super T> comparator, Class<T> type) {997 requireNonNull(comparator, "Expecting a non null Comparator");998 this.typeComparators.registerComparator(type, comparator);999 return this;1000 }1001 /**1002 * Registers the given {@link BiPredicate} to compare the fields with the given type.1003 * <p>1004 * BiPredicates registered with this method have less precedence than the ones registered with {@link #withEqualsForFields(BiPredicate, String...)}1005 * or the comparators registered with {@link #withComparatorForFields(Comparator, String...)}.1006 * <p>1007 * Note that registering a {@link BiPredicate} for a given type will override the previously registered BiPredicate/Comparator (if any).1008 * <p>1009 * See {@link RecursiveComparisonAssert#withEqualsForType(BiPredicate, Class)} for examples.1010 *1011 * @param <T> the class type to register a BiPredicate for1012 * @param equals the {@link BiPredicate} to use to compare the given field1013 * @param type the type to be compared with the given comparator.1014 * @return this builder.1015 * @since 3.17.01016 * @throws NullPointerException if the given BiPredicate is null.1017 */1018 @SuppressWarnings("unchecked")1019 public <T> Builder withEqualsForType(BiPredicate<? super T, ? super T> equals, Class<T> type) {1020 return withComparatorForType(toComparator(equals), type);1021 }1022 /**1023 * Registers the given {@link Comparator} to compare the fields at the given locations.1024 * <p>1025 * 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,1026 * one can register a comparator for Foo and Bar's {@code id} by calling:1027 * <pre><code class='java'> registerComparatorForFields(idComparator, "foo.id", "foo.bar.id")</code></pre>1028 * <p>1029 * Comparators registered with this method have precedence over comparators registered with {@link #withComparatorForType(Comparator, Class)}1030 * or BiPredicate registered with {@link #withEqualsForType(BiPredicate, Class)}.1031 * <p>1032 * Note that registering a {@link Comparator} for a given field will override the previously registered BiPredicate/Comparator (if any).1033 * <p>1034 * See {@link RecursiveComparisonAssert#withComparatorForFields(Comparator, String...) RecursiveComparisonAssert#withComparatorForFields(Comparator comparator, String...fields)} for examples.1035 *1036 * @param comparator the {@link java.util.Comparator Comparator} to use to compare the given field1037 * @param fields the fields the comparator should be used for1038 * @return this builder.1039 * @throws NullPointerException if the given Comparator is null.1040 */1041 public Builder withComparatorForFields(Comparator<?> comparator, String... fields) {1042 requireNonNull(comparator, "Expecting a non null Comparator");1043 Stream.of(fields).forEach(fieldLocation -> fieldComparators.registerComparator(fieldLocation, comparator));1044 return this;1045 }1046 /**1047 * Registers the given {@link BiPredicate} to compare the fields at the given locations.1048 * <p>1049 * 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,1050 * one can register a BiPredicate for Foo and Bar's {@code id} by calling:1051 * <pre><code class='java'> withEqualsForFields(idBiPredicate, "foo.id", "foo.bar.id")</code></pre>1052 * <p>1053 * BiPredicates registered with this method have precedence over the ones registered with {@link #withEqualsForType(BiPredicate, Class)}1054 * or the comparators registered with {@link #withComparatorForType(Comparator, Class)}.1055 * <p>1056 * Note that registering a {@link BiPredicate} for a given field will override the previously registered BiPredicate/Comparator (if any).1057 * <p>1058 * See {@link RecursiveComparisonAssert#withEqualsForFields(BiPredicate, String...) RecursiveComparisonAssert#withEqualsForFields(BiPredicate equals, String...fields)} for examples.1059 *1060 * @param equals the {@link BiPredicate} to use to compare the given fields1061 * @param fields the fields the BiPredicate should be used for1062 * @return this builder.1063 * @since 3.17.01064 * @throws NullPointerException if the given BiPredicate is null.1065 */1066 public Builder withEqualsForFields(BiPredicate<?, ?> equals, String... fields) {1067 return withComparatorForFields(toComparator(equals), fields);1068 }1069 /**1070 * Allows to register a {@link BiPredicate} to compare fields whose location matches the given regexes.1071 * A typical usage is to compare double/float fields with a given precision.1072 * <p>1073 * The fields are evaluated from the root object, for example if {@code Foo} has a {@code Bar} field and both have an {@code id} field,1074 * one can register a BiPredicate for Foo and Bar's {@code id} by calling:1075 * <pre><code class='java'> withEqualsForFields(idBiPredicate, ".*id")</code></pre>1076 * or1077 * <pre><code class='java'> withEqualsForFields(idBiPredicate, "foo.*id")</code></pre>1078 * <p>1079 * BiPredicates registered with this method have precedence over the ones registered with {@link #registerEqualsForType(BiPredicate, Class)}1080 * or the comparators registered with {@link #registerComparatorForType(Comparator, Class)} but don't have precedence over the1081 * ones registered with exact location match: {@link #registerEqualsForFields(BiPredicate, String...)} or {@link #registerComparatorForFields(Comparator, String...)}1082 * <p>1083 * If registered regexes for different {@link BiPredicate} match a given field, the latest registered regexes {@link BiPredicate} wins.1084 * <p>1085 * See {@link RecursiveComparisonAssert#withEqualsForFieldsMatchingRegexes(BiPredicate, String...)} RecursiveComparisonAssert#withEqualsForFieldsMatchingRegexes(BiPredicate equals, String...fields)} for examples.1086 *1087 * @param equals the {@link BiPredicate} to use to compare the fields matching the given regexes1088 * @param regexes the regexes to match fields against1089 * @return this builder.1090 * @since 3.24.01091 * @throws NullPointerException if the given BiPredicate is null.1092 */1093 public Builder withEqualsForFieldsMatchingRegexes(BiPredicate<?, ?> equals, String... regexes) {1094 fieldComparators.registerComparatorForFieldsMatchingRegexes(regexes, toComparator(equals));1095 return this;1096 }1097 /**1098 * Registers the giving message which would be shown when differences in the given fields while comparison occurred.1099 * <p>1100 * The fields must be specified from the root object, for example if {@code Foo} has a {@code Bar} field and both1101 * have an {@code id} field, one can register a message for Foo and Bar's {@code id} by calling:1102 * <pre><code class='java'> withErrorMessageForFields("some message", "foo.id", "foo.bar.id")</code></pre>1103 * <p>1104 * Messages registered with this method have precedence over the ones registered with {@link #withErrorMessageForType(String, Class)}.1105 * <p>1106 * In case of {@code null} as message the default error message will be used (See1107 * {@link ComparisonDifference#DEFAULT_TEMPLATE})....
Source:RecursiveComparisonAssert.java
...1070 * A typical usage is to compare double/float fields with a given precision.1071 * <p>1072 * The fields are evaluated from the root object, for example if {@code Foo} has a {@code Bar} field and both have an {@code id} field,1073 * one can register a BiPredicate for Foo and Bar's {@code id} by calling:1074 * <pre><code class='java'> withEqualsForFieldsMatchingRegexes(idBiPredicate, ".*id")</code></pre>1075 * or1076 * <pre><code class='java'> withEqualsForFieldsMatchingRegexes(idBiPredicate, "foo.*id")</code></pre>1077 * <p>1078 * BiPredicates registered with this method have precedence over the ones registered with {@link #withEqualsForType(BiPredicate, Class)}1079 * or the comparators registered with {@link #withComparatorForType(Comparator, Class)} but don't have precedence over the1080 * ones registered with exact location match: {@link #withEqualsForFields(BiPredicate, String...)} or {@link #withComparatorForFields(Comparator, String...)}1081 * <p>1082 * If registered regexes for different {@link BiPredicate} match a given field, the latest registered regexes {@link BiPredicate} wins.1083 * <p>1084 * Example:1085 * <pre><code class='java'> public class TolkienCharacter {1086 * String name;1087 * double height;1088 * double weight;1089 * }1090 *1091 * TolkienCharacter frodo = new TolkienCharacter("Frodo", 1.2, 40);1092 * TolkienCharacter tallerFrodo = new TolkienCharacter("Frodo", 1.3, 40.5);1093 * TolkienCharacter reallyTallFrodo = new TolkienCharacter("Frodo", 1.9, 45);1094 *1095 * BiPredicate<Double, Double> closeEnough = (d1, d2) -> Math.abs(d1 - d2) <= 0.5;1096 *1097 * // assertion succeeds1098 * assertThat(frodo).usingRecursiveComparison()1099 * .withEqualsForFieldsMatchingRegexes(closeEnough, ".eight")1100 * .isEqualTo(tallerFrodo);1101 *1102 * // assertion fails1103 * assertThat(frodo).usingRecursiveComparison()1104 * .withEqualsForFieldsMatchingRegexes(closeEnough, ".eight")1105 * .isEqualTo(reallyTallFrodo);</code></pre>1106 *1107 * @param equals the {@link BiPredicate} to use to compare the fields matching the given regexes1108 * @param regexes the regexes from the root object of the fields location the BiPredicate should be used for1109 *1110 * @return this {@link RecursiveComparisonAssert} to chain other methods.1111 * @throws NullPointerException if the given BiPredicate is null.1112 * @since 3.24.01113 */1114 public SELF withEqualsForFieldsMatchingRegexes(BiPredicate<?, ?> equals, String... regexes) {1115 recursiveComparisonConfiguration.registerEqualsForFieldsMatchingRegexes(equals, regexes);1116 return myself;1117 }1118 /**1119 * Allows to register a comparator to compare fields with the given locations.1120 * A typical usage is to compare double/float fields with a given precision.1121 * <p>1122 * Comparators registered with this method have precedence over comparators registered with {@link #withComparatorForType(Comparator, Class)}1123 * or {@link BiPredicate} registered with {@link #withEqualsForType(BiPredicate, Class)}.1124 * <p>1125 * The field locations must be specified from the root object,1126 * 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:1127 * <pre><code class='java'> withComparatorForFields(idComparator, "foo.id", "foo.bar.id")</code></pre>1128 * <p>...
withEqualsForFieldsMatchingRegexes
Using AI Code Generation
1import org.assertj.core.api.RecursiveComparisonAssert;2public class RecursiveComparisonAssertExample {3 public static void main(String[] args) {4 Person person1 = new Person("John", 30);5 Person person2 = new Person("John", 30);6 RecursiveComparisonAssert<Person> recursiveComparisonAssert = new RecursiveComparisonAssert<>(person1);7 recursiveComparisonAssert.usingRecursiveComparison().withEqualsForFieldsMatchingRegexes(".*name");8 recursiveComparisonAssert.isEqualTo(person2);9 }10}11public class Person {12 private String name;13 private int age;14 public Person(String name, int age) {15 this.name = name;16 this.age = age;17 }18 public String getName() {19 return name;20 }21 public int getAge() {22 return age;23 }24 public boolean equals(Object o) {25 if (this == o) return true;26 if (o == null || getClass() != o.getClass()) return false;27 Person person = (Person) o;28 return age == person.age;29 }30 public int hashCode() {31 return Objects.hash(age);32 }33}34at org.assertj.core.api.RecursiveComparisonAssert.isEqualTo(RecursiveComparisonAssert.java:122)35at org.assertj.core.api.RecursiveComparisonAssert.isEqualTo(RecursiveComparisonAssert.java:31)36at org.assertj.core.api.AbstractAssert.isEqualTo(AbstractAssert.java:82)37at RecursiveComparisonAssertExample.main(RecursiveComparisonAssertExample.java:17)
withEqualsForFieldsMatchingRegexes
Using AI Code Generation
1import org.assertj.core.api.RecursiveComparisonAssert;2import java.util.Arrays;3import java.util.regex.Pattern;4public class RecursiveComparisonAssertWithEqualsForFieldsMatchingRegexes {5 public static void main(String[] args) {6 String[] list = {"abc", "def", "ghi"};7 String[] list2 = {"abc", "def", "ghi"};8 String[] list3 = {"abc", "def", "ghi"};9 String[] list4 = {"abc", "def", "ghi"};10 String[] list5 = {"abc", "def", "ghi"};11 String[] list6 = {"abc", "def", "ghi"};12 String[] list7 = {"abc", "def", "ghi"};13 String[] list8 = {"abc", "def", "ghi"};14 String[] list9 = {"abc", "def", "ghi"};15 String[] list10 = {"abc", "def", "ghi"};16 String[] list11 = {"abc", "def", "ghi"};17 String[] list12 = {"abc", "def", "ghi"};18 String[] list13 = {"abc", "def", "ghi"};19 String[] list14 = {"abc", "def", "ghi"};20 String[] list15 = {"abc", "def", "ghi"};21 String[] list16 = {"abc", "def", "ghi"};22 String[] list17 = {"abc", "def", "ghi"};23 String[] list18 = {"abc", "def", "ghi"};24 String[] list19 = {"abc", "def", "ghi"};25 String[] list20 = {"abc", "
withEqualsForFieldsMatchingRegexes
Using AI Code Generation
1package org.assertj.core.api;2import org.junit.jupiter.api.Test;3import static org.assertj.core.api.Assertions.assertThat;4public class AssertJTest {5 public void test() {6 assertThat(new User("John", "Doe", 25)).usingRecursiveComparison()7 .withEqualsForFieldsMatchingRegexes(".*ame")8 .isEqualTo(new User("John", "Doe", 26));9 }10 static class User {11 private String firstName;12 private String lastName;13 private int age;14 public User(String firstName, String lastName, int age) {15 this.firstName = firstName;16 this.lastName = lastName;17 this.age = age;18 }19 public String getFirstName() {20 return firstName;21 }22 public void setFirstName(String firstName) {23 this.firstName = firstName;24 }25 public String getLastName() {26 return lastName;27 }28 public void setLastName(String lastName) {29 this.lastName = lastName;30 }31 public int getAge() {32 return age;33 }34 public void setAge(int age) {35 this.age = age;36 }37 }38}39 User(firstName=John, lastName=Doe, age=25)40 User(firstName=John, lastName=Doe, age=26)41at org.assertj.core.api.RecursiveComparisonAssert.isEqualTo(RecursiveComparisonAssert.java:183)42at org.assertj.core.api.RecursiveComparisonAssert.isEqualTo(RecursiveComparisonAssert.java:175)43at org.assertj.core.api.RecursiveComparisonAssert.isEqualTo(RecursiveComparisonAssert.java:37)44at org.assertj.core.api.AbstractAssert.isEqualTo(AbstractAssert.java:82)45at org.assertj.core.api.RecursiveComparisonAssert.isEqualTo(RecursiveComparisonAssert.java:183)46at org.assertj.core.api.RecursiveComparisonAssert.isEqualTo(RecursiveComparisonAssert.java:175)47at org.assertj.core.api.RecursiveComparisonAssert.isEqualTo(RecursiveComparisonAssert.java:37)48at org.assertj.core.api.AbstractAssert.isEqualTo(AbstractAssert.java:82
withEqualsForFieldsMatchingRegexes
Using AI Code Generation
1import org.junit.Test;2import static org.assertj.core.api.Assertions.assertThat;3public class AssertjTest {4 public void test() {5 Person person1 = new Person("John", 35, new Address("Street 1", 1000));6 Person person2 = new Person("John", 35, new Address("Street 1", 1000));7 assertThat(person1).usingRecursiveComparison().withEqualsForFieldsMatchingRegexes(".*").isEqualTo(person2);8 }9}10 Person(name=John, age=35, address=Address(street=Street 1, number=1000))11 Person(name=John, age=35, address=Address(street=Street 1, number=1000))12when recursively comparing field by field, but found the following difference(s):13- actual value : Address(street=Street 1, number=1000)14- expected value : Address(street=Street 1, number=1000)15at org.assertj.core.api.RecursiveComparisonAssert.recursiveComparison(RecursiveComparisonAssert.java:105)16at org.assertj.core.api.RecursiveComparisonAssert.recursiveComparison(RecursiveComparisonAssert.java:1)
withEqualsForFieldsMatchingRegexes
Using AI Code Generation
1public class RecursiveComparisonAssert_withEqualsForFieldsMatchingRegexes {2 public static void main(String[] args) {3 Employee employee1 = new Employee("John", "Doe", 1000);4 Employee employee2 = new Employee("John", "Doe", 1000);5 Company company1 = new Company("ABC", employee1);6 Company company2 = new Company("ABC", employee2);7 Company company3 = new Company("ABC", employee1);8 Company company4 = new Company("ABC", employee2);9 Address address1 = new Address("USA", company1);10 Address address2 = new Address("USA", company2);11 Address address3 = new Address("USA", company3);12 Address address4 = new Address("USA", company4);13 assertThat(address1).usingRecursiveComparison().withEqualsForFieldsMatchingRegexes(".*").isEqualTo(address2);14 assertThat(address3).usingRecursiveComparison().withEqualsForFieldsMatchingRegexes(".*").isEqualTo(address4);15 }16}17public class Employee {18 private String firstName;19 private String lastName;20 private int age;21 public Employee(String firstName, String lastName, int age) {22 this.firstName = firstName;23 this.lastName = lastName;24 this.age = age;25 }26 public String getFirstName() {27 return firstName;28 }29 public String getLastName() {30 return lastName;31 }32 public int getAge() {33 return age;34 }35}36public class Company {37 private String name;38 private Employee employee;39 public Company(String name, Employee employee) {40 this.name = name;41 this.employee = employee;42 }43 public String getName() {44 return name;45 }46 public Employee getEmployee() {47 return employee;48 }49}50public class Address {51 private String country;52 private Company company;53 public Address(String country, Company company) {54 this.country = country;55 this.company = company;56 }57 public String getCountry() {58 return country;
withEqualsForFieldsMatchingRegexes
Using AI Code Generation
1public class RecursiveComparisonAssert_useWithEqualsForFieldsMatchingRegexes {2 public static void main(String[] args) {3 Employee employee = new Employee(1, "John", new Address("1", "Street1"));4 Employee employee2 = new Employee(1, "John", new Address("2", "Street2"));5 RecursiveComparisonAssert<Employee> recursiveComparisonAssert = assertThat(employee);6 recursiveComparisonAssert.usingRecursiveComparison().withEqualsForFieldsMatchingRegexes("id", "name").isEqualTo(employee2);7 }8}9 <Employee(id=1, name=John, address=Address(id=1, street=Street1))>10 <Employee(id=1, name=John, address=Address(id=2, street=Street2))>11at org.opentest4j.AssertionFailedError.<init>(AssertionFailedError.java:33)12at org.assertj.core.api.RecursiveComparisonAssert.recursiveComparison(RecursiveComparisonAssert.java:119)13at org.assertj.core.api.RecursiveComparisonAssert.recursiveComparison(RecursiveComparisonAssert.java:1)14at org.assertj.core.api.RecursiveComparisonAssert.isEqualTo(RecursiveComparisonAssert.java:97)15at org.assertj.core.api.RecursiveComparisonAssert.isEqualTo(RecursiveComparisonAssert.java:1)16at com.baeldung.recursiveassertions.RecursiveComparisonAssert_useWithEqualsForFieldsMatchingRegexes.main(RecursiveComparisonAssert_useWithEqualsForFieldsMatchingRegexes.java:22)
withEqualsForFieldsMatchingRegexes
Using AI Code Generation
1public class AssertjRecursiveComparisonAssert {2 public static void main(String[] args) {3 Person person1 = new Person("John", "Doe", 30);4 Person person2 = new Person("John", "Doe", 30);5 assertThat(person1).usingRecursiveComparison().withEqualsForFieldsMatchingRegexes("age").isEqualTo(person2);6 }7}8public class Person {9 private String firstName;10 private String lastName;11 private int age;12 public Person(String firstName, String lastName, int age) {13 this.firstName = firstName;14 this.lastName = lastName;15 this.age = age;16 }17 public String getFirstName() {18 return firstName;19 }20 public String getLastName() {21 return lastName;22 }23 public int getAge() {24 return age;25 }26}
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!