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

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

Source:AbstractIterableAssert.java Github

copy

Full Screen

...2274 * assertThat(doctors).usingRecursiveFieldByFieldElementComparator()2275 * .contains(howard);</code></pre>2276 * <p>2277 * Another point worth mentioning: <b>elements order does matter if the expected iterable is ordered</b>, for example comparing a {@code Set<Person>} to a {@code List<Person>} fails as {@code List} is ordered and {@code Set} is not.<br>2278 * The ordering can be ignored by calling {@link RecursiveComparisonAssert#ignoringCollectionOrder ignoringCollectionOrder} allowing ordered/unordered iterable comparison, note that {@link RecursiveComparisonAssert#ignoringCollectionOrder ignoringCollectionOrder} is applied recursively on any nested iterable fields, if this behavior is too generic,2279 * use the more fine grained {@link RecursiveComparisonAssert#ignoringCollectionOrderInFields(String...) ignoringCollectionOrderInFields} or2280 * {@link RecursiveComparisonAssert#ignoringCollectionOrderInFieldsMatchingRegexes(String...) ignoringCollectionOrderInFieldsMatchingRegexes}.2281 *2282 * @return {@code this} assertion object.2283 * @since 2.5.0 / 3.5.0 - breaking change in 3.20.02284 * @see RecursiveComparisonConfiguration2285 * @see #usingRecursiveFieldByFieldElementComparator(RecursiveComparisonConfiguration)2286 */2287 @CheckReturnValue2288 public SELF usingRecursiveFieldByFieldElementComparator() {2289 return usingRecursiveFieldByFieldElementComparator(new RecursiveComparisonConfiguration());2290 }2291 /**2292 * Enable using a recursive field by field comparison strategy similar to {@link #usingRecursiveComparison()} but contrary to the latter <b>you can chain any iterable assertions after this method</b> (this is why this method exists).2293 * <p>2294 * The given {@link RecursiveComparisonConfiguration} is used to tweak the comparison behavior, for example by {@link RecursiveComparisonConfiguration#ignoreCollectionOrder(boolean) ignoring collection order}.2295 * <p>2296 * <b>Warning:</b> the comparison won't use any comparators set with:2297 * <ul>2298 * <li>{@link #usingComparatorForType(Comparator, Class)}</li>2299 * <li>{@link #withTypeComparators(TypeComparators)}</li>2300 * <li>{@link #usingComparatorForElementFieldsWithType(Comparator, Class)}</li>2301 * <li>{@link #withComparatorsForElementPropertyOrFieldTypes(TypeComparators)}</li>2302 * <li>{@link #usingComparatorForElementFieldsWithNames(Comparator, String...)}</li>2303 * <li>{@link #withComparatorsForElementPropertyOrFieldNames(Map)}</li>2304 * </ul>2305 * <p>2306 * These features (and many more) are provided through {@link RecursiveComparisonConfiguration} with:2307 * <ul>2308 * <li>{@link RecursiveComparisonConfiguration#registerComparatorForType(Comparator, Class) registerComparatorForType(Comparator, Class)} / {@link RecursiveComparisonConfiguration.Builder#withComparatorForType(Comparator, Class) withComparatorForType(Comparator, Class)} (using {@link RecursiveComparisonConfiguration.Builder})</li>2309 * <li>{@link RecursiveComparisonConfiguration#registerEqualsForType(java.util.function.BiPredicate, Class) registerEqualsForType(BiPredicate, Class)} / {@link RecursiveComparisonConfiguration.Builder#withComparatorForType(Comparator, Class) withComparatorForType(Comparator, Class)} (using {@link RecursiveComparisonConfiguration.Builder})</li>2310 * <li>{@link RecursiveComparisonConfiguration#registerComparatorForFields(Comparator, String...) registerComparatorForFields(Comparator comparator, String... fields)} / {@link RecursiveComparisonConfiguration.Builder#withComparatorForFields(Comparator, String...) withComparatorForField(Comparator comparator, String... fields)} (using {@link RecursiveComparisonConfiguration.Builder})</li>2311 * </ul>2312 * <p>2313 * RecursiveComparisonConfiguration exposes a {@link RecursiveComparisonConfiguration.Builder builder} to ease setting the comparison behaviour,2314 * call {@link RecursiveComparisonConfiguration#builder() RecursiveComparisonConfiguration.builder()} to start building your configuration.2315 * <p>2316 * There are differences between this approach and {@link #usingRecursiveComparison()}:2317 * <ul>2318 * <li>contrary to {@link RecursiveComparisonAssert}, you can chain any iterable assertions after this method.</li>2319 * <li>no comparators registered with {@link AbstractIterableAssert#usingComparatorForType(Comparator, Class)} will be used, you need to register them in the configuration object.</li>2320 * <li>the assertion errors won't be as detailed as {@link RecursiveComparisonAssert#isEqualTo(Object)} which shows the field differences.</li>2321 * </ul>2322 * <p>2323 * This last point makes sense, take the {@link #contains(Object...)} assertion, it would not be relevant to report the differences of all the iterable's elements differing from the values to look for.2324 * <p>2325 * Example:2326 * <pre><code class='java'> public class Person {2327 * String name;2328 * boolean hasPhd;2329 * }2330 *2331 * public class Doctor {2332 * String name;2333 * boolean hasPhd;2334 * }2335 *2336 * Doctor drSheldon = new Doctor("Sheldon Cooper", true);2337 * Doctor drLeonard = new Doctor("Leonard Hofstadter", true);2338 * Doctor drRaj = new Doctor("Raj Koothrappali", true);2339 *2340 * Person sheldon = new Person("Sheldon Cooper", false);2341 * Person leonard = new Person("Leonard Hofstadter", false);2342 * Person raj = new Person("Raj Koothrappali", false);2343 * Person howard = new Person("Howard Wolowitz", false);2344 *2345 * List&lt;Doctor&gt; doctors = list(drSheldon, drLeonard, drRaj);2346 * List&lt;Person&gt; people = list(sheldon, leonard, raj);2347 *2348 * RecursiveComparisonConfiguration configuration = RecursiveComparisonConfiguration.builder()2349 * .withIgnoredFields​("hasPhd");2350 *2351 * // assertion succeeds as both lists contains equivalent items in order.2352 * assertThat(doctors).usingRecursiveFieldByFieldElementComparator(configuration)2353 * .contains(sheldon);2354 *2355 * // assertion fails because leonard names are different.2356 * leonard.setName("Leonard Ofstater");2357 * assertThat(doctors).usingRecursiveFieldByFieldElementComparator(configuration)2358 * .contains(leonard);2359 *2360 * // assertion fails because howard is missing and leonard is not expected.2361 * people = list(howard, sheldon, raj)2362 * assertThat(doctors).usingRecursiveFieldByFieldElementComparator(configuration)2363 * .contains(howard);</code></pre>2364 *2365 * A detailed documentation for the recursive comparison is available here: <a href="https://assertj.github.io/doc/#assertj-core-recursive-comparison">https://assertj.github.io/doc/#assertj-core-recursive-comparison</a>.2366 * <p>2367 * The default recursive comparison behavior is {@link RecursiveComparisonConfiguration configured} as follows:2368 * <ul>2369 * <li> different types of iterable can be compared by default, this allows to compare for example an {@code List<Person>} and a {@code LinkedHashSet<PersonDto>}.<br>2370 * This behavior can be turned off by calling {@link RecursiveComparisonAssert#withStrictTypeChecking() withStrictTypeChecking}.</li>2371 * <li>overridden equals methods are used in the comparison (unless stated otherwise - see <a href="https://assertj.github.io/doc/#assertj-core-recursive-comparison-ignoring-equals">https://assertj.github.io/doc/#assertj-core-recursive-comparison-ignoring-equals</a>)</li>2372 * <li>the following types are compared with these comparators:2373 * <ul>2374 * <li>{@code java.lang.Double}: {@code DoubleComparator} with precision of 1.0E-15</li>2375 * <li>{@code java.lang.Float}: {@code FloatComparator }with precision of 1.0E-6</li>2376 * </ul>2377 * </li>2378 * </ul>2379 * <p>2380 * Another point worth mentioning: <b>elements order does matter if the expected iterable is ordered</b>, for example comparing a {@code Set<Person>} to a {@code List<Person>} fails as {@code List} is ordered and {@code Set} is not.<br>2381 * The ordering can be ignored by calling {@link RecursiveComparisonAssert#ignoringCollectionOrder ignoringCollectionOrder} allowing ordered/unordered iterable comparison, note that {@link RecursiveComparisonAssert#ignoringCollectionOrder ignoringCollectionOrder} is applied recursively on any nested iterable fields, if this behavior is too generic,2382 * use the more fine grained {@link RecursiveComparisonAssert#ignoringCollectionOrderInFields(String...) ignoringCollectionOrderInFields} or2383 * {@link RecursiveComparisonAssert#ignoringCollectionOrderInFieldsMatchingRegexes(String...) ignoringCollectionOrderInFieldsMatchingRegexes}.2384 *2385 * @param configuration the recursive comparison configuration.2386 *2387 * @return {@code this} assertion object.2388 * @since 3.17.02389 * @see RecursiveComparisonConfiguration2390 */2391 public SELF usingRecursiveFieldByFieldElementComparator(RecursiveComparisonConfiguration configuration) {2392 return usingElementComparator(new ConfigurableRecursiveFieldByFieldComparator(configuration));2393 }2394 /**2395 * Enable using a recursive field by field comparison strategy when calling the chained {@link RecursiveComparisonAssert},2396 * <p>2397 * There are differences between this approach and {@link #usingRecursiveFieldByFieldElementComparator(RecursiveComparisonConfiguration)}:2398 * <ul>2399 * <li>you can only chain {@link RecursiveComparisonAssert} assertions (basically {@link RecursiveComparisonAssert#isEqualTo(Object) isEqualTo}) and (basically {@link RecursiveComparisonAssert#isNotEqualTo(Object) isNotEqualTo}), no iterable assertions.</li>2400 * <li>{@link RecursiveComparisonAssert#isEqualTo(Object) isEqualTo} assertion error will report all field differences (very detailed).</li>2401 * <li>no comparators registered with {@link AbstractIterableAssert#usingComparatorForType(Comparator, Class)} will be used, you need to register them in chained call like {@link RecursiveComparisonAssert#withComparatorForType(Comparator, Class)}.</li>2402 * </ul>2403 * <p>2404 * If you need to chain iterable assertions using recursive comparisons call {@link #usingRecursiveFieldByFieldElementComparator(RecursiveComparisonConfiguration)} instead.2405 * <p>2406 * Example:2407 * <pre><code class='java'> public class Person {2408 * String name;2409 * boolean hasPhd;2410 * }2411 *2412 * public class Doctor {2413 * String name;2414 * boolean hasPhd;2415 * }2416 *2417 * Doctor drSheldon = new Doctor("Sheldon Cooper", true);2418 * Doctor drLeonard = new Doctor("Leonard Hofstadter", true);2419 * Doctor drRaj = new Doctor("Raj Koothrappali", true);2420 *2421 * Person sheldon = new Person("Sheldon Cooper", true);2422 * Person leonard = new Person("Leonard Hofstadter", true);2423 * Person raj = new Person("Raj Koothrappali", true);2424 * Person howard = new Person("Howard Wolowitz", false);2425 *2426 * List&lt;Doctor&gt; doctors = Arrays.asList(drSheldon, drLeonard, drRaj);2427 * List&lt;Person&gt; people = Arrays.asList(sheldon, leonard, raj);2428 *2429 * // assertion succeeds as both lists contains equivalent items in order.2430 * assertThat(doctors).usingRecursiveComparison()2431 * .isEqualTo(people);2432 *2433 * // assertion fails because leonard names are different.2434 * leonard.setName("Leonard Ofstater");2435 * assertThat(doctors).usingRecursiveComparison()2436 * .isEqualTo(people);2437 *2438 * // assertion fails because howard is missing and leonard is not expected.2439 * people = Arrays.asList(howard, sheldon, raj)2440 * assertThat(doctors).usingRecursiveComparison()2441 * .isEqualTo(people);</code></pre>2442 *2443 * A detailed documentation for the recursive comparison is available here: <a href="https://assertj.github.io/doc/#assertj-core-recursive-comparison">https://assertj.github.io/doc/#assertj-core-recursive-comparison</a>.2444 * <p>2445 * The default recursive comparison behavior is {@link RecursiveComparisonConfiguration configured} as follows:2446 * <ul>2447 * <li> different types of iterable can be compared by default, this allows to compare for example an {@code List<Person>} and a {@code LinkedHashSet<PersonDto>}.<br>2448 * This behavior can be turned off by calling {@link RecursiveComparisonAssert#withStrictTypeChecking() withStrictTypeChecking}.</li>2449 * <li>overridden equals methods are used in the comparison (unless stated otherwise - see <a href="https://assertj.github.io/doc/#assertj-core-recursive-comparison-ignoring-equals">https://assertj.github.io/doc/#assertj-core-recursive-comparison-ignoring-equals</a>)</li>2450 * <li>the following types are compared with these comparators:2451 * <ul>2452 * <li>{@code java.lang.Double}: {@code DoubleComparator} with precision of 1.0E-15</li>2453 * <li>{@code java.lang.Float}: {@code FloatComparator }with precision of 1.0E-6</li>2454 * </ul>2455 * </li>2456 * </ul>2457 * <p>2458 * Another point worth mentioning: <b>elements order does matter if the expected iterable is ordered</b>, for example comparing a {@code Set<Person>} to a {@code List<Person>} fails as {@code List} is ordered and {@code Set} is not.<br>2459 * The ordering can be ignored by calling {@link RecursiveComparisonAssert#ignoringCollectionOrder ignoringCollectionOrder} allowing ordered/unordered iterable comparison, note that {@link RecursiveComparisonAssert#ignoringCollectionOrder ignoringCollectionOrder} is applied recursively on any nested iterable fields, if this behavior is too generic,2460 * use the more fine grained {@link RecursiveComparisonAssert#ignoringCollectionOrderInFields(String...) ignoringCollectionOrderInFields} or2461 * {@link RecursiveComparisonAssert#ignoringCollectionOrderInFieldsMatchingRegexes(String...) ignoringCollectionOrderInFieldsMatchingRegexes}.2462 * <p>2463 * At the moment, only `isEqualTo` can be chained after this method but there are plans to provide assertions.2464 *2465 * @return a new {@link RecursiveComparisonAssert} instance2466 * @see RecursiveComparisonConfiguration RecursiveComparisonConfiguration2467 * @since 3.15.02468 */2469 @Beta2470 @Override2471 public RecursiveComparisonAssert<?> usingRecursiveComparison() {2472 // overridden for javadoc and to make this method public2473 return super.usingRecursiveComparison();2474 }2475 /**...

Full Screen

Full Screen

Source:RecursiveComparisonConfiguration.java Github

copy

Full Screen

...268 }269 /**270 * Sets whether to ignore collection order in the comparison.271 * <p>272 * See {@link RecursiveComparisonAssert#ignoringCollectionOrder()} for code examples.273 *274 * @param ignoreCollectionOrder whether to ignore collection order in the comparison.275 */276 public void ignoreCollectionOrder(boolean ignoreCollectionOrder) {277 this.ignoreCollectionOrder = ignoreCollectionOrder;278 }279 /**280 * Adds the given fields to the list of the object under test fields to ignore collection order in the recursive comparison.281 * <p>282 * See {@link RecursiveComparisonAssert#ignoringCollectionOrderInFields(String...) RecursiveComparisonAssert#ignoringCollectionOrderInFields(String...)} for examples.283 *284 * @param fieldsToIgnoreCollectionOrder the fields of the object under test to ignore collection order in the comparison.285 */286 public void ignoreCollectionOrderInFields(String... fieldsToIgnoreCollectionOrder) {287 List<String> fieldLocations = list(fieldsToIgnoreCollectionOrder);288 ignoredCollectionOrderInFields.addAll(fieldLocations);289 }290 /**291 * Returns the list of the object under test fields to ignore collection order in the recursive comparison.292 *293 * @return the list of the object under test fields to ignore collection order in the recursive comparison.294 */295 public Set<String> getIgnoredCollectionOrderInFields() {296 return ignoredCollectionOrderInFields;297 }298 /**299 * 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.300 * <p>301 * See {@link RecursiveComparisonAssert#ignoringCollectionOrderInFieldsMatchingRegexes(String...) RecursiveComparisonAssert#ignoringCollectionOrderInFieldsMatchingRegexes(String...)} for examples.302 *303 * @param regexes regexes used to find the object under test fields to ignore collection order in in the comparison.304 */305 public void ignoreCollectionOrderInFieldsMatchingRegexes(String... regexes) {306 ignoredCollectionOrderInFieldsMatchingRegexes.addAll(Stream.of(regexes)307 .map(Pattern::compile)308 .collect(toList()));309 }310 /**311 * Returns the list of regexes used to find the object under test fields to ignore collection order in the recursive comparison.312 *313 * @return the list of regexes used to find the object under test fields to ignore collection order in the recursive comparison.314 */315 public List<Pattern> getIgnoredCollectionOrderInFieldsMatchingRegexes() {316 return ignoredCollectionOrderInFieldsMatchingRegexes;317 }318 /**319 * Registers the given {@link Comparator} to compare the fields with the given type.320 * <p>321 * Comparators registered with this method have less precedence than comparators registered with {@link #registerComparatorForFields(Comparator, String...)}.322 * <p>323 * Note that registering a {@link Comparator} for a given type will override the previously registered BiPredicate/Comparator (if any).324 * <p>325 * See {@link RecursiveComparisonAssert#withComparatorForType(Comparator, Class)} for examples.326 *327 * @param <T> the class type to register a comparator for328 * @param comparator the {@link java.util.Comparator Comparator} to use to compare the given type329 * @param type the type to be compared with the given comparator.330 * @throws NullPointerException if the given comparator is null.331 */332 public <T> void registerComparatorForType(Comparator<? super T> comparator, Class<T> type) {333 requireNonNull(comparator, "Expecting a non null Comparator");334 typeComparators.put(type, comparator);335 }336 /**337 * Registers the given {@link BiPredicate} to compare the fields with the given type.338 * <p>339 * BiPredicates specified with this method have less precedence than the ones registered with {@link #registerEqualsForFields(BiPredicate, String...)}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>...

Full Screen

Full Screen

ignoringCollectionOrder

Using AI Code Generation

copy

Full Screen

1public class AssertionDemo {2 public static void main(String[] args) {3 List<Integer> list1 = Arrays.asList(1, 2, 3);4 List<Integer> list2 = Arrays.asList(2, 1, 3);5 List<Integer> list3 = Arrays.asList(1, 2, 3);6 assertThat(list1).usingRecursiveComparison().ignoringCollectionOrder().isEqualTo(list2);7 assertThat(list1).usingRecursiveComparison().ignoringCollectionOrder().isEqualTo(list3);8 }9}10at org.assertj.core.api.recursive.comparison.RecursiveComparisonAssert.isEqualTo(RecursiveComparisonAssert.java:235)11at org.assertj.core.api.recursive.comparison.RecursiveComparisonAssert.isEqualTo(RecursiveComparisonAssert.java:43)12at AssertionDemo.main(AssertionDemo.java:12)13at org.assertj.core.api.recursive.comparison.RecursiveComparisonAssert.isEqualTo(RecursiveComparisonAssert.java:235)14at org.assertj.core.api.recursive.comparison.RecursiveComparisonAssert.isEqualTo(RecursiveComparisonAssert.java:43)15at AssertionDemo.main(AssertionDemo.java:13)16public class AssertionDemo {17 public static void main(String[] args) {18 List<Integer> list1 = Arrays.asList(1, 2, 3);19 List<Integer> list2 = Arrays.asList(2, 1, 3);20 List<Integer> list3 = Arrays.asList(1, 2, 3);21 assertThat(list1).usingRecursiveComparison().ignoringCollectionOrder().isEqualTo

Full Screen

Full Screen

ignoringCollectionOrder

Using AI Code Generation

copy

Full Screen

1import org.assertj.core.api.RecursiveComparisonAssert;2import org.assertj.core.api.Assertions;3import java.util.List;4import java.util.ArrayList;5public class 1 {6 public static void main(String[] args) {7 List<String> list1 = new ArrayList<>();8 list1.add("one");9 list1.add("two");10 list1.add("three");11 List<String> list2 = new ArrayList<>();12 list2.add("three");13 list2.add("two");14 list2.add("one");15 RecursiveComparisonAssert<String> recursiveComparisonAssert = Assertions.assertThat(list1);16 recursiveComparisonAssert.ignoringCollectionOrder();17 recursiveComparisonAssert.isEqualTo(list2);18 }19}

Full Screen

Full Screen

ignoringCollectionOrder

Using AI Code Generation

copy

Full Screen

1import org.assertj.core.api.RecursiveComparisonAssert;2import org.assertj.core.api.RecursiveComparisonConfiguration;3import org.assertj.core.api.Assertions;4import java.util.Arrays;5import java.util.List;6import java.util.ArrayList;7import java.util.Map;8import java.util.HashMap;9import java.util.Set;10import java.util.HashSet;11import java.util.Arrays;12import java.util.Collections;13import java.util.Comparator;14import java.util.Objects;15import java.util.stream.Collectors;16import java.util.stream.Stream;17import java.util.function.Function;18import java.util.function.Predicate;19import java.util.function.Supplier;20import java.util.function.Consumer;21import java.util.function.BiFunction;22import java.util.function.BiConsumer;23import java.util.function.BiPredicate;24import java.util.function.BinaryOperator;25import java.util.function.UnaryOperator;26import java.util.function.IntFunction;27import java.util.function.IntPredicate;28import java.util.function.IntConsumer;29import java.util.function.IntSupplier;30import java.util.function.IntToLongFunction;31import java.util.function.IntToDoubleFunction;32import java.util.function.LongFunction;33import java.util.function.LongPredicate;34import java.util.function.LongConsumer;35import java.util.function.LongSupplier;36import java.util.function.LongToIntFunction;37import java.util.function.LongToDoubleFunction;38import java.util.function.DoubleFunction;39import java.util.function.DoublePredicate;40import java.util.function.DoubleConsumer;41import java.util.function.DoubleSupplier;42import java.util.function.DoubleToIntFunction;43import java.util.function.DoubleToLongFunction;44import java.util.function.ToIntFunction;45import java.util.function.ToLongFunction;46import java.util.function.ToDoubleFunction;47import java.util.function.ObjIntConsumer;48import java.util.function.ObjLongConsumer;49import java.util.function.ObjDoubleConsumer;50import java.util.function.BiFunction;51import java.util.function.BiConsumer;52import java.util.function.BiPredicate;53import java.util.function.BinaryOperator;54import java.util.function.UnaryOperator;55import java.util.function.IntFunction;56import java.util.function.IntPredicate;57import java.util.function.IntConsumer;58import java.util.function.IntSupplier;59import java.util.function.IntToLongFunction;60import java.util.function.IntToDoubleFunction;61import java.util.function.LongFunction;62import java.util.function.LongPredicate;63import java.util.function.LongConsumer;64import java.util.function.LongSupplier;65import java.util.function.LongToIntFunction;66import java.util.function.LongToDoubleFunction;67import java.util.function.DoubleFunction;68import java.util.function.DoublePredicate;69import java.util.function.DoubleConsumer;70import java.util.function.DoubleSupplier;

Full Screen

Full Screen

ignoringCollectionOrder

Using AI Code Generation

copy

Full Screen

1import org.assertj.core.api.RecursiveComparisonAssert;2import org.assertj.core.api.RecursiveComparisonConfiguration;3import org.assertj.core.util.introspection.IntrospectionError;4import org.assertj.core.internal.objects.data.Employee;5import org.assertj.core.internal.objects.data.EmployeeDto;6import org.assertj.core.internal.objects.data.EmployeeDtoBuilder;7import org.assertj.core.internal.objects.data.EmployeeWithDifferentId;8import org.assertj.core.internal.objects.data.EmployeeWithDifferentIdDto;9import org.assertj.core.internal.objects.data.EmployeeWithDifferentIdDtoBuilder;10import org.assertj.core.util.introspection.PropertyOrFieldSupport;11import org.assertj.core.util.introspection.PropertyOrFieldSupport.ExpectedType;12public class RecursiveComparisonAssertIgnoringCollectionOrder {13 public static void main(String[] args) {14 RecursiveComparisonConfiguration recursiveComparisonConfiguration = new RecursiveComparisonConfiguration();15 recursiveComparisonConfiguration.ignoreCollectionOrder(true);16 RecursiveComparisonAssert recursiveComparisonAssert = new RecursiveComparisonAssert(EmployeeDtoBuilder.employeeDto());17 EmployeeWithDifferentIdDto employeeWithDifferentIdDto = EmployeeWithDifferentIdDtoBuilder.employeeWithDifferentIdDto();18 recursiveComparisonAssert.ignoringCollectionOrder(recursiveComparisonConfiguration).isEqualTo(employeeWithDifferentIdDto);19 }20}

Full Screen

Full Screen

ignoringCollectionOrder

Using AI Code Generation

copy

Full Screen

1import org.junit.Test;2import static org.assertj.core.api.Assertions.assertThat;3import java.util.List;4import java.util.Arrays;5public class RecursiveComparisonAssertIgnoringCollectionOrder {6 public void testIgnoringCollectionOrder() {7 List<String> list1 = Arrays.asList("a", "b", "c");8 List<String> list2 = Arrays.asList("c", "b", "a");9 assertThat(list1).usingRecursiveComparison()10 .ignoringCollectionOrder()11 .isEqualTo(list2);12 }13}14org.assertj.core.api.RecursiveComparisonAssertIgnoringCollectionOrder > testIgnoringCollectionOrder() PASSED15import org.junit.Test;16import static org.assertj.core.api.Assertions.assertThat;17import java.util.List;18import java.util.Arrays;19public class RecursiveComparisonAssertIgnoringCollectionOrder {20 public void testIgnoringCollectionOrder() {21 List<String> list1 = Arrays.asList("a", "b", "c");22 List<String> list2 = Arrays.asList("c", "b", "a");23 assertThat(list1).usingRecursiveComparison()24 .ignoringCollectionOrder()25 .isEqualTo(list2);26 }27}28org.assertj.core.api.RecursiveComparisonAssertIgnoringCollectionOrder > testIgnoringCollectionOrder() PASSED

Full Screen

Full Screen

ignoringCollectionOrder

Using AI Code Generation

copy

Full Screen

1import org.assertj.core.api.RecursiveComparisonAssert;2import org.junit.Test;3import java.util.ArrayList;4import java.util.List;5public class RecursiveComparisonAssertTest {6 public void test() {7 List<String> list1 = new ArrayList<>();8 list1.add("one");9 list1.add("two");10 list1.add("three");11 List<String> list2 = new ArrayList<>();12 list2.add("three");13 list2.add("two");14 list2.add("one");15 RecursiveComparisonAssert.recursiveComparison()16 .ignoringCollectionOrder()17 .isEqualTo(list1, list2);18 }19}20at org.assertj.core.api.RecursiveComparisonAssert.recursiveComparison(RecursiveComparisonAssert.java:64)21at org.assertj.core.api.Assertions.recursiveComparison(Assertions.java:184)22at org.assertj.core.api.Assertions.recursiveComparison(Assertions.java:178)23at RecursiveComparisonAssertTest.test(RecursiveComparisonAssertTest.java:21)24at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)25at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)26at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)27at java.lang.reflect.Method.invoke(Method.java:498)28at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)29at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)30at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)31at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)32at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)33at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)34at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)35at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)36at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)37at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)38at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)

Full Screen

Full Screen

ignoringCollectionOrder

Using AI Code Generation

copy

Full Screen

1import java.util.*;2import org.assertj.core.api.*;3import org.assertj.core.api.recursive.comparison.*;4import org.assertj.core.util.*;5public class RecursiveComparisonAssertIgnoringCollectionOrder {6 public static void main(String[] args) {7 List<String> list1 = Arrays.asList("one", "two", "three", "four", "five");8 List<String> list2 = Arrays.asList("five", "one", "two", "three", "four");9 RecursiveComparisonAssert recursiveComparisonAssert = Assertions.assertThat(list1);10 RecursiveComparisonAssert recursiveComparisonAssert2 = recursiveComparisonAssert.ignoringCollectionOrder();11 recursiveComparisonAssert2.isEqualTo(list2);12 System.out.println("Both lists are equal");13 }14}

Full Screen

Full Screen

ignoringCollectionOrder

Using AI Code Generation

copy

Full Screen

1import org.assertj.core.api.Assertions;2import org.assertj.core.api.RecursiveComparisonAssert;3import java.util.*;4public class AssertjExample1 {5 public static void main(String[] args) {6 List<Integer> list1 = new ArrayList<>();7 list1.add(1);8 list1.add(2);9 List<Integer> list2 = new ArrayList<>();10 list2.add(2);11 list2.add(1);12 RecursiveComparisonAssert recursiveComparisonAssert = Assertions.assertThat(list1);13 recursiveComparisonAssert.ignoringCollectionOrder();14 recursiveComparisonAssert.isEqualTo(list2);15 }16}17at org.assertj.core.api.RecursiveComparisonAssert.recursiveComparison(RecursiveComparisonAssert.java:284)18at org.assertj.core.api.RecursiveComparisonAssert.isEqualTo(RecursiveComparisonAssert.java:274)19at AssertjExample1.main(AssertjExample1.java:17)20import org.assertj.core.api.Assertions;21import org.assertj.core.api.RecursiveComparisonAssert;22public class AssertjExample2 {23 public static void main(String[] args) {24 Employee employee1 = new Employee();25 employee1.setEmpId(1);26 employee1.setEmpName("John");27 employee1.setEmpAddress("New York");28 Employee employee2 = new Employee();29 employee2.setEmpId(1);30 employee2.setEmpName("John");31 employee2.setEmpAddress("New York");32 RecursiveComparisonAssert recursiveComparisonAssert = Assertions.assertThat(employee1);33 recursiveComparisonAssert.ignoringCollectionOrder();34 recursiveComparisonAssert.isEqualTo(employee2);35 }36}

Full Screen

Full Screen

ignoringCollectionOrder

Using AI Code Generation

copy

Full Screen

1import org.assertj.core.api.*;2import org.assertj.core.api.RecursiveComparisonAssert.*;3import java.util.*;4import java.util.List;5import java.util.ArrayList;6import java.util.Arrays;7public class RecursiveComparisonAssert_ignoringCollectionOrder {8 public static void main(String[] args) {9 List<String> list1 = new ArrayList<String>();10 list1.add("one");11 list1.add("two");12 list1.add("three");13 list1.add("four");14 List<String> list2 = new ArrayList<String>();15 list2.add("two");16 list2.add("one");17 list2.add("four");18 list2.add("three");19 Assertions.assertThat(list1).usingRecursiveComparison().ignoringCollectionOrder().isEqualTo(list2);20 }21}

Full Screen

Full Screen

ignoringCollectionOrder

Using AI Code Generation

copy

Full Screen

1import org.assertj.core.api.*;2import java.util.*;3public class RecursiveComparisonAssertIgnoringCollectionOrderExample {4 public static void main(String[] args) {5 List<List<Integer>> list1 = new ArrayList<>();6 list1.add(Arrays.asList(1, 2, 3));7 list1.add(Arrays.asList(4, 5, 6));8 List<List<Integer>> list2 = new ArrayList<>();9 list2.add(Arrays.asList(4, 5, 6));10 list2.add(Arrays.asList(1, 2, 3));11 RecursiveComparisonAssert.ignoringCollectionOrder()12 .isEqualTo(list1, list2);13 System.out.println("list1 and list2 are equal");14 }15}16Recommended Posts: AssertJ | RecursiveComparisonAssert.ignoringActualNullFields() method17AssertJ | RecursiveComparisonAssert.ignoringExpectedNullFields() method18AssertJ | RecursiveComparisonAssert.ignoringFields() method19AssertJ | RecursiveComparisonAssert.ignoringOverriddenEqualsForFields() method20AssertJ | RecursiveComparisonAssert.ignoringOverriddenEqualsForType() method21AssertJ | RecursiveComparisonAssert.ignoringAllOverriddenEquals() method22AssertJ | RecursiveComparisonAssert.ignoringCollectionOrder() method23AssertJ | RecursiveComparisonAssert.ignoringActualNullFieldsForType() method24AssertJ | RecursiveComparisonAssert.ignoringExpectedNullFieldsForType() method25AssertJ | RecursiveComparisonAssert.ignoringFieldsOfTypes() method26AssertJ | RecursiveComparisonAssert.ignoringAllOverriddenEqualsForType() method27AssertJ | RecursiveComparisonAssert.ignoringAllOverriddenEqualsForTypes() method28AssertJ | RecursiveComparisonAssert.ignoringActualNullFieldsForTypes() method29AssertJ | RecursiveComparisonAssert.ignoringExpectedNullFieldsForTypes() method30AssertJ | RecursiveComparisonAssert.ignoringFields(String...) method31AssertJ | RecursiveComparisonAssert.ignoringFields(String, String, String) method32AssertJ | RecursiveComparisonAssert.ignoringFields(String, String)

Full Screen

Full Screen

ignoringCollectionOrder

Using AI Code Generation

copy

Full Screen

1import org.assertj.core.api.*;2import org.assertj.core.api.RecursiveComparisonAssert.*;3import java.util.*;4import java.util.List;5import java.util.ArrayList;6import java.util.Arrays;7public class RecursiveComparisonAssert_ignoringCollectionOrder {8 public static void main(String[] args) {9 List<String> list1 = new ArrayList<String>();10 list1.add("one");11 list1.add("two");12 list1.add("three");13 list1.add("four");14 List<String> list2 = new ArrayList<String>();15 list2.add("two");16 list2.add("one");17 list2.add("four");18 list2.add("three");19 Assertions.assertThat(list1).usingRecursiveComparison().ignoringCollectionOrder().isEqualTo(list2);20 }21}

Full Screen

Full Screen

ignoringCollectionOrder

Using AI Code Generation

copy

Full Screen

1import org.assertj.core.api.*;2import java.util.*;3public class RecursiveComparisonAssertIgnoringCollectionOrderExample {4 public static void main(String[] args) {5 List<List<Integer>> list1 = new ArrayList<>();6 list1.add(Arrays.asList(1, 2, 3));7 list1.add(Arrays.asList(4, 5, 6));8 List<List<Integer>> list2 = new ArrayList<>();9 list2.add(Arrays.asList(4, 5, 6));10 list2.add(Arrays.asList(1, 2, 3));11 RecursiveComparisonAssert.ignoringCollectionOrder()12 .isEqualTo(list1, list2);13 System.out.println("list1 and list2 are equal");14 }15}16Recommended Posts: AssertJ | RecursiveComparisonAssert.ignoringActualNullFields() method17AssertJ | RecursiveComparisonAssert.ignoringExpectedNullFields() method18AssertJ | RecursiveComparisonAssert.ignoringFields() method19AssertJ | RecursiveComparisonAssert.ignoringOverriddenEqualsForFields() method20AssertJ | RecursiveComparisonAssert.ignoringOverriddenEqualsForType() method21AssertJ | RecursiveComparisonAssert.ignoringAllOverriddenEquals() method22AssertJ | RecursiveComparisonAssert.ignoringCollectionOrder() method23AssertJ | RecursiveComparisonAssert.ignoringActualNullFieldsForType() method24AssertJ | RecursiveComparisonAssert.ignoringExpectedNullFieldsForType() method25AssertJ | RecursiveComparisonAssert.ignoringFieldsOfTypes() method26AssertJ | RecursiveComparisonAssert.ignoringAllOverriddenEqualsForType() method27AssertJ | RecursiveComparisonAssert.ignoringAllOverriddenEqualsForTypes() method28AssertJ | RecursiveComparisonAssert.ignoringActualNullFieldsForTypes() method29AssertJ | RecursiveComparisonAssert.ignoringExpectedNullFieldsForTypes() method30AssertJ | RecursiveComparisonAssert.ignoringFields(String...) method31AssertJ | RecursiveComparisonAssert.ignoringFields(String, String, String) method32AssertJ | RecursiveComparisonAssert.ignoringFields(String, String)

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