How to use getComparatorsByType method of org.assertj.core.api.AbstractObjectAssert class

Best Assertj code snippet using org.assertj.core.api.AbstractObjectAssert.getComparatorsByType

Source:AbstractObjectAssert.java Github

copy

Full Screen

...130 * @throws IntrospectionError if one of actual's field to compare can't be found in the other object.131 */132 @Deprecated133 public SELF isEqualToIgnoringNullFields(Object other) {134 objects.assertIsEqualToIgnoringNullFields(info, actual, other, comparatorByPropertyOrField, getComparatorsByType());135 return myself;136 }137 /**138 * @deprecated Use the recursive comparison by calling {@link #usingRecursiveComparison()} and specify the fields to ignore.139 * <p>140 * <b>Warning:</b> the recursive comparison does not provide a strictly equivalent feature, instead it provides several ways to ignore141 * fields in the comparison {@link RecursiveComparisonAssert#ignoringFields(String...) by specifying fields to ignore}, or142 * {@link RecursiveComparisonAssert#ignoringFieldsOfTypes(Class...) fields by type} or143 * {@link RecursiveComparisonAssert#ignoringFieldsMatchingRegexes(String...) fields matching regexes}. The idea being that it is best144 * to compare as many fields as possible and only ignore the ones that are not relevant (for example generated ids).145 * <p>146 * This method is deprecated because it only compares the first level of fields while the recursive comparison traverses all147 * fields recursively (only stopping at java types).148 * <p>149 * For example suppose actual and expected are of type A which has the following structure:150 * <pre><code class="text"> A151 * |— B b152 * | |— String s153 * | |— C c154 * | |— String s155 * | |— Date d156 * |— int i</code></pre>157 * {@code isEqualToComparingOnlyGivenFields} will compare actual and expected {@code A.b} and {@code A.i} fields but not B fields158 * (it calls B equals method instead comparing B fields).<br>159 * The recursive comparison on the other hand will introspect B fields and then C fields and will compare actual and expected160 * respective fields values, that is: {@code A.i}, {@code A.B.s}, {@code A.B.C.s} and {@code A.B.C.d}.161 * <p>162 * Assuming actual has 4 fields f1, f2, f3, f4, instead of writing:163 * <pre><code class='java'> assertThat(actual).isEqualToComparingOnlyGivenFields(expected, f1, f2);</code></pre>164 * You should write:165 * <pre><code class='java'> assertThat(actual).usingRecursiveComparison()166 * .ignoringFields(f3, f4)167 * .isEqualTo(expected);</code></pre>168 * <b>Original javadoc</b>169 * <p>170 * Asserts that the actual object is equal to the given one using a property/field by property/field comparison <b>on the given properties/fields only</b>171 * (fields can be inherited fields or nested fields). This can be handy if {@code equals} implementation of objects to compare does not suit you.172 * <p>173 * Note that comparison is <b>not</b> recursive, if one of the field is an Object, it will be compared to the other174 * field using its {@code equals} method.175 * <p>176 * If an object has a field and a property with the same name, the property value will be used over the field.177 * <p>178 * Private fields are used in comparison but this can be disabled using179 * {@link Assertions#setAllowComparingPrivateFields(boolean)}, if disabled only <b>accessible </b>fields values are180 * compared, accessible fields include directly accessible fields (e.g. public) or fields with an accessible getter.181 * <p>182 * The objects to compare can be of different types but the properties/fields used in comparison must exist in both,183 * for example if actual object has a name String field, it is expected the other object to also have one.184 * <p>185 *186 * Example:187 * <pre><code class='java'> TolkienCharacter frodo = new TolkienCharacter(&quot;Frodo&quot;, 33, HOBBIT);188 * TolkienCharacter sam = new TolkienCharacter(&quot;Sam&quot;, 38, HOBBIT);189 *190 * // frodo and sam both are hobbits, so they are equals when comparing only race191 * assertThat(frodo).isEqualToComparingOnlyGivenFields(sam, &quot;race&quot;); // OK192 *193 * // they are also equals when comparing only race name (nested field).194 * assertThat(frodo).isEqualToComparingOnlyGivenFields(sam, &quot;race.name&quot;); // OK195 *196 * // ... but not when comparing both name and race197 * assertThat(frodo).isEqualToComparingOnlyGivenFields(sam, &quot;name&quot;, &quot;race&quot;); // FAIL</code></pre>198 *199 * @param other the object to compare {@code actual} to.200 * @param propertiesOrFieldsUsedInComparison properties/fields used in comparison.201 * @return {@code this} assertion object.202 * @throws NullPointerException if the actual or other is {@code null}.203 * @throws AssertionError if the actual and the given objects are not equals property/field by property/field on given fields.204 * @throws IntrospectionError if one of actual's property/field to compare can't be found in the other object.205 * @throws IntrospectionError if a property/field does not exist in actual.206 */207 @Deprecated208 public SELF isEqualToComparingOnlyGivenFields(Object other, String... propertiesOrFieldsUsedInComparison) {209 objects.assertIsEqualToComparingOnlyGivenFields(info, actual, other, comparatorByPropertyOrField, getComparatorsByType(),210 propertiesOrFieldsUsedInComparison);211 return myself;212 }213 /**214 * @deprecated Use the recursive comparison by calling {@link #usingRecursiveComparison()} and chain with215 * {@link RecursiveComparisonAssert#ignoringFields(String...) ignoringFields(String...)}.216 * <p>217 * This method is deprecated because it only compares the first level of fields while the recursive comparison traverses all218 * fields recursively (only stopping at java types).219 * <p>220 * For example suppose actual and expected are of type A which has the following structure:221 * <pre><code class="text"> A222 * |— B b223 * | |— String s224 * | |— C c225 * | |— String s226 * | |— Date d227 * |— int i</code></pre>228 * {@code isEqualToIgnoringGivenFields} will compare actual and expected {@code A.b} and {@code A.i} fields but not B fields229 * (it calls B equals method instead comparing B fields).<br>230 * The recursive comparison on the other hand will introspect B fields and then C fields and will compare actual and expected231 * respective fields values, that is: {@code A.i}, {@code A.B.s}, {@code A.B.C.s} and {@code A.B.C.d}.232 * <p>233 * Concretely instead of writing:234 * <pre><code class='java'> assertThat(actual).isEqualToIgnoringGivenFields(expected, "i", "b.s");</code></pre>235 * You should write:236 * <pre><code class='java'> assertThat(actual).usingRecursiveComparison()237 * .ignoringFields("i", "b.s")238 * .isEqualTo(expected);</code></pre>239 * <p>240 * Note that the recursive comparison also allows to ignore fields241 * {@link RecursiveComparisonAssert#ignoringFieldsOfTypes(Class...) by type} or242 * {@link RecursiveComparisonAssert#ignoringFieldsMatchingRegexes(String...) matching regexes}.243 * <b>Original javadoc</b>244 * <p>245 * Asserts that the actual object is equal to the given one by comparing their properties/fields <b>except for the given ones</b>246 * (inherited ones are taken into account). This can be handy if {@code equals} implementation of objects to compare does not suit you.247 * <p>248 * Note that comparison is <b>not</b> recursive, if one of the property/field is an Object, it will be compared to the other249 * field using its {@code equals} method.250 * <p>251 * If an object has a field and a property with the same name, the property value will be used over the field.252 * <p>253 * Private fields are used in comparison but this can be disabled using254 * {@link Assertions#setAllowComparingPrivateFields(boolean)}, if disabled only <b>accessible </b>fields values are255 * compared, accessible fields include directly accessible fields (e.g. public) or fields with an accessible getter.256 * <p>257 * The objects to compare can be of different types but the properties/fields used in comparison must exist in both,258 * for example if actual object has a name String field, it is expected the other object to also have one.259 * <p>260 *261 * Example:262 * <pre><code class='java'> TolkienCharacter frodo = new TolkienCharacter("Frodo", 33, HOBBIT);263 * TolkienCharacter sam = new TolkienCharacter("Sam", 38, HOBBIT);264 *265 * // frodo and sam are equals when ignoring name and age since the only remaining field is race which they share as HOBBIT.266 * assertThat(frodo).isEqualToIgnoringGivenFields(sam, "name", "age"); // OK267 *268 * // ... but they are not equals if only age is ignored as their names differ.269 * assertThat(frodo).isEqualToIgnoringGivenFields(sam, "age"); // FAIL</code></pre>270 *271 * @param other the object to compare {@code actual} to.272 * @param propertiesOrFieldsToIgnore ignored properties/fields to ignore in comparison.273 * @return {@code this} assertion object.274 * @throws NullPointerException if the actual or given object is {@code null}.275 * @throws AssertionError if the actual and the given objects are not equals property/field by property/field after ignoring given fields.276 * @throws IntrospectionError if one of actual's property/field to compare can't be found in the other object.277 */278 @Deprecated279 public SELF isEqualToIgnoringGivenFields(Object other, String... propertiesOrFieldsToIgnore) {280 objects.assertIsEqualToIgnoringGivenFields(info, actual, other, comparatorByPropertyOrField, getComparatorsByType(),281 propertiesOrFieldsToIgnore);282 return myself;283 }284 /**285 * Asserts that the actual object has no null fields or properties (inherited ones are taken into account).286 * <p>287 * If an object has a field and a property with the same name, the property value will be used over the field.288 * <p>289 * Private fields are checked, but this can be disabled using {@link Assertions#setAllowComparingPrivateFields(boolean)},290 * if disabled only <b>accessible</b> fields values are291 * checked, accessible fields include directly accessible fields (e.g. public) or fields with an accessible getter.292 * <p>293 * Example:294 * <pre><code class='java'> TolkienCharacter frodo = new TolkienCharacter("Frodo", 33, HOBBIT);295 * TolkienCharacter sam = new TolkienCharacter("Sam", 38, null);296 *297 * // assertion succeeds since all frodo's fields are set298 * assertThat(frodo).hasNoNullFieldsOrProperties();299 *300 * // assertion fails because sam does not have its race set301 * assertThat(sam).hasNoNullFieldsOrProperties();</code></pre>302 *303 * @return {@code this} assertion object.304 * @throws AssertionError if the actual object is {@code null}.305 * @throws AssertionError if some fields or properties of the actual object are null.306 *307 * @since 2.5.0 / 3.5.0308 */309 public SELF hasNoNullFieldsOrProperties() {310 objects.assertHasNoNullFieldsOrPropertiesExcept(info, actual);311 return myself;312 }313 /**314 * Asserts that the actual object has only null fields or properties.315 * <p>316 * If an object has a field and a property with the same name, the property value will be used over the field.317 * <p>318 * Private fields are checked, but this can be disable using {@link Assertions#setAllowComparingPrivateFields(boolean)},319 * if disable only <b>accessible</b> fields values are checked,320 * accessible fields include directly accessible fields (e.g. public) or fields with an accessible getter.321 * <p>322 * Example:323 * <pre><code class='java'> TolkienCharacter frodo = new TolkienCharacter(null, null, null);324 * TolkienCharacter sam = new TolkienCharacter("sam", null, null);325 *326 * // assertion succeeds since all frodo's fields are null327 * assertThat(frodo).hasAllNullFieldsOrProperties();328 *329 * // assertion fails because sam has its name set330 * assertThat(sam).hasAllNullFieldsOrProperties();</code></pre>331 *332 * @return {@code this} assertion object.333 * @throws AssertionError if the actual object is {@code null}.334 * @throws AssertionError if some field or properties of the actual object are not null.335 *336 * @since 3.12.0337 */338 public SELF hasAllNullFieldsOrProperties() {339 objects.assertHasAllNullFieldsOrPropertiesExcept(info, actual);340 return myself;341 }342 /**343 * Asserts that the actual object has no null fields or properties <b>except for the given ones</b>344 * (inherited ones are taken into account).345 * <p>346 * If an object has a field and a property with the same name, the property value will be used over the field.347 * <p>348 * Private fields are checked, but this can be disabled using {@link Assertions#setAllowComparingPrivateFields(boolean)},349 * if disabled only <b>accessible</b> fields values are checked,350 * accessible fields include directly accessible fields (e.g. public) or fields with an accessible getter.351 * <p>352 * Example:353 * <pre><code class='java'>TolkienCharacter frodo = new TolkienCharacter("Frodo", 33, null);354 *355 * // assertion succeeds since frodo has only null field is race356 * assertThat(frodo).hasNoNullFieldsOrPropertiesExcept("race");357 *358 * // ... but if we require the race field, the assertion fails359 * assertThat(frodo).hasNoNullFieldsOrPropertiesExcept("name", "age");</code></pre>360 *361 * @param propertiesOrFieldsToIgnore properties/fields that won't be checked for null.362 * @return {@code this} assertion object.363 * @throws AssertionError if the actual object is {@code null}.364 * @throws AssertionError if some (non ignored) fields or properties of the actual object are null.365 *366 * @since 2.5.0 / 3.5.0367 */368 public SELF hasNoNullFieldsOrPropertiesExcept(String... propertiesOrFieldsToIgnore) {369 objects.assertHasNoNullFieldsOrPropertiesExcept(info, actual, propertiesOrFieldsToIgnore);370 return myself;371 }372 /**373 * Asserts that the actual object has only null fields or properties <b>except for the given ones</b>374 * (inherited ones are taken into account).375 * <p>376 * If an object has a field and a property with the same name, the property value will be user over the field.377 * <p>378 * Private fields are checked, but this can be disable using {@link Assertions#setAllowComparingPrivateFields(boolean)},379 * if disabled only <b>accessible</b> fields values are checked,380 * accessible fields include directly accessible fields (e.g. public) or fields with an accessible getter.381 * <p>382 * Example:383 * <pre><code class='java'> TolkienCharacter frodo = new TolkienCharacter("Frodo", null, null);384 *385 * // assertion succeeds since frodo has only non null field is name386 * assertThat(frodo).hasAllNullFieldsOrPropertiesExcept("name");387 *388 * // ... but if we specify any field other than name, the assertion fails389 * assertThat(frodo).hasAllNullFieldsOrPropertiesExcept("race");</code></pre>390 *391 * @param propertiesOrFieldsToIgnore properties/fields that won't be checked for not being null.392 * @return {@code this} assertion object.393 * @throws AssertionError if the actual object is {@code null}.394 * @throws AssertionError if some (non ignored) fields or properties of the actual object are not null.395 *396 * @since 3.12.0397 */398 public SELF hasAllNullFieldsOrPropertiesExcept(String... propertiesOrFieldsToIgnore) {399 objects.assertHasAllNullFieldsOrPropertiesExcept(info, actual, propertiesOrFieldsToIgnore);400 return myself;401 }402 /**403 * @deprecated Use the recursive comparison by calling {@link #usingRecursiveComparison()}.404 * <p>405 * This method is deprecated because it only compares the first level of fields while the recursive comparison traverses all406 * fields recursively (only stopping at java types).407 * <p>408 * For example suppose actual and expected are of type A which has the following structure:409 * <pre><code class="text"> A410 * |— B b411 * | |— String s412 * | |— C c413 * | |— String s414 * | |— Date d415 * |— int i</code> </pre>416 * {@code isEqualToComparingFieldByField} will compare actual and expected {@code A.b} and {@code A.i} fields but not B fields417 * (it calls B equals method instead comparing B fields).<br>418 * The recursive comparison on the other hand will introspect B fields and then C fields and will compare actual and expected419 * respective fields values, that is: {@code A.i}, {@code A.B.s}, {@code A.B.C.s} and {@code A.B.C.d}.420 * <p>421 * Concretely instead of writing:422 * <pre><code class='java'> assertThat(actual).isEqualToComparingFieldByField(expected);</code></pre>423 * You should write:424 * <pre><code class='java'> assertThat(actual).usingRecursiveComparison()425 * .isEqualTo(expected);</code></pre>426 * <b>Original javadoc</b>427 * <p>428 * Asserts that actual object is equal to the given object based on a property/field by property/field comparison (including429 * inherited ones). This can be handy if {@code equals} implementation of objects to compare does not suit you.430 * <p>431 * Note that comparison is <b>not</b> recursive, if one of the field is an Object, it will be compared to the other432 * field using its {@code equals} method.433 * <p>434 * If an object has a field and a property with the same name, the property value will be used over the field.435 * <p>436 * Private fields are used in comparison but this can be disabled using437 * {@link Assertions#setAllowComparingPrivateFields(boolean)}, if disabled only <b>accessible </b>fields values are438 * compared, accessible fields include directly accessible fields (e.g. public) or fields with an accessible getter.439 * <p>440 * The objects to compare can be of different types but the properties/fields used in comparison must exist in both,441 * for example if actual object has a name String field, it is expected the other object to also have one.442 * <p>443 *444 * Example:445 * <pre><code class='java'> // equals not overridden in TolkienCharacter446 * TolkienCharacter frodo = new TolkienCharacter("Frodo", 33, HOBBIT);447 * TolkienCharacter frodoClone = new TolkienCharacter("Frodo", 33, HOBBIT);448 *449 * // Fail as equals compares object references450 * assertThat(frodo).isEqualTo(frodoClone);451 *452 * // frodo and frodoClone are equals when doing a field by field comparison.453 * assertThat(frodo).isEqualToComparingFieldByField(frodoClone);</code></pre>454 *455 * @param other the object to compare {@code actual} to.456 * @return {@code this} assertions object457 * @throws AssertionError if the actual object is {@code null}.458 * @throws AssertionError if the actual and the given objects are not equals property/field by property/field.459 * @throws IntrospectionError if one of actual's property/field to compare can't be found in the other object.460 */461 @Deprecated462 public SELF isEqualToComparingFieldByField(Object other) {463 objects.assertIsEqualToIgnoringGivenFields(info, actual, other, comparatorByPropertyOrField, getComparatorsByType());464 return myself;465 }466 // lazy init TypeComparators467 protected TypeComparators getComparatorsByType() {468 if (comparatorByType == null) comparatorByType = defaultTypeComparators();469 return comparatorByType;470 }471 /**472 * Allows to set a specific comparator to compare properties or fields with the given names.473 * A typical usage is for comparing double/float fields with a given precision.474 * <p>475 * Comparators specified by this method have precedence over comparators added by {@link #usingComparatorForType}.476 * <p>477 * The comparators specified by this method are only used for field by field comparison like {@link #isEqualToComparingFieldByField(Object)}.478 * <p>479 * When used with {@link #isEqualToComparingFieldByFieldRecursively(Object)}, the fields/properties must be specified from the root object,480 * for example if Foo class as a Bar field and Bar class has an id, to set a comparator for Bar's id use {@code "bar.id"}.481 * <p>482 * Example:483 * <pre><code class='java'> public class TolkienCharacter {484 * private String name;485 * private double height;486 * // constructor omitted487 * }488 * TolkienCharacter frodo = new TolkienCharacter(&quot;Frodo&quot;, 1.2);489 * TolkienCharacter tallerFrodo = new TolkienCharacter(&quot;Frodo&quot;, 1.3);490 * TolkienCharacter reallyTallFrodo = new TolkienCharacter(&quot;Frodo&quot;, 1.9);491 *492 * Comparator&lt;Double&gt; closeEnough = new Comparator&lt;Double&gt;() {493 * double precision = 0.5;494 * public int compare(Double d1, Double d2) {495 * return Math.abs(d1 - d2) &lt;= precision ? 0 : 1;496 * }497 * };498 *499 * // assertions will pass500 * assertThat(frodo).usingComparatorForFields(closeEnough, &quot;height&quot;)501 * .isEqualToComparingFieldByField(tallerFrodo);502 *503 * assertThat(frodo).usingComparatorForFields(closeEnough, &quot;height&quot;)504 * .isEqualToIgnoringNullFields(tallerFrodo);505 *506 * assertThat(frodo).usingComparatorForFields(closeEnough, &quot;height&quot;)507 * .isEqualToIgnoringGivenFields(tallerFrodo);508 *509 * assertThat(frodo).usingComparatorForFields(closeEnough, &quot;height&quot;)510 * .isEqualToComparingOnlyGivenFields(tallerFrodo);511 *512 * // assertion will fail513 * assertThat(frodo).usingComparatorForFields(closeEnough, &quot;height&quot;)514 * .isEqualToComparingFieldByField(reallyTallFrodo);</code></pre>515 *516 * @param <T> the type of values to compare.517 * @param comparator the {@link java.util.Comparator} to use518 * @param propertiesOrFields the names of the properties and/or fields the comparator should be used for519 * @return {@code this} assertions object520 */521 @CheckReturnValue522 public <T> SELF usingComparatorForFields(Comparator<T> comparator, String... propertiesOrFields) {523 for (String propertyOrField : propertiesOrFields) {524 comparatorByPropertyOrField.put(propertyOrField, comparator);525 }526 return myself;527 }528 /**529 * Allows to set a specific comparator to compare properties or fields with the given type.530 * A typical usage is for comparing fields of numeric type at a given precision.531 * <p>532 * Comparators specified by {@link #usingComparatorForFields} have precedence over comparators specified by this method.533 * <p>534 * The comparators specified by this method are only used for field by field comparison like {@link #isEqualToComparingFieldByField(Object)}.535 * <p>536 * Example:537 * <pre><code class='java'> public class TolkienCharacter {538 * private String name;539 * private double height;540 * // constructor omitted541 * }542 * TolkienCharacter frodo = new TolkienCharacter(&quot;Frodo&quot;, 1.2);543 * TolkienCharacter tallerFrodo = new TolkienCharacter(&quot;Frodo&quot;, 1.3);544 * TolkienCharacter reallyTallFrodo = new TolkienCharacter(&quot;Frodo&quot;, 1.9);545 *546 * Comparator&lt;Double&gt; closeEnough = new Comparator&lt;Double&gt;() {547 * double precision = 0.5;548 * public int compare(Double d1, Double d2) {549 * return Math.abs(d1 - d2) &lt;= precision ? 0 : 1;550 * }551 * };552 *553 * // assertions will pass554 * assertThat(frodo).usingComparatorForType(closeEnough, Double.class)555 * .isEqualToComparingFieldByField(tallerFrodo);556 *557 * assertThat(frodo).usingComparatorForType(closeEnough, Double.class)558 * .isEqualToIgnoringNullFields(tallerFrodo);559 *560 * assertThat(frodo).usingComparatorForType(closeEnough, Double.class)561 * .isEqualToIgnoringGivenFields(tallerFrodo);562 *563 * assertThat(frodo).usingComparatorForType(closeEnough, Double.class)564 * .isEqualToComparingOnlyGivenFields(tallerFrodo);565 *566 * // assertion will fail567 * assertThat(frodo).usingComparatorForType(closeEnough, Double.class)568 * .isEqualToComparingFieldByField(reallyTallFrodo);</code></pre>569 *570 * If multiple compatible comparators have been registered for a given {@code type}, the closest in the inheritance571 * chain to the given {@code type} is chosen in the following order:572 * <ol>573 * <li>The comparator for the exact given {@code type}</li>574 * <li>The comparator of a superclass of the given {@code type}</li>575 * <li>The comparator of an interface implemented by the given {@code type}</li>576 * </ol>577 *578 * @param comparator the {@link java.util.Comparator} to use579 * @param type the {@link java.lang.Class} of the type the comparator should be used for580 * @param <T> the type of objects that the comparator should be used for581 * @return {@code this} assertions object582 */583 @CheckReturnValue584 public <T> SELF usingComparatorForType(Comparator<? super T> comparator, Class<T> type) {585 getComparatorsByType().put(type, comparator);586 return myself;587 }588 /**589 * Asserts that the actual object has the specified field or property.590 * <p>591 * Private fields are matched by default but this can be changed by calling {@link Assertions#setAllowExtractingPrivateFields(boolean) Assertions.setAllowExtractingPrivateFields(false)}.592 * <p>593 *594 * Example:595 * <pre><code class='java'> public class TolkienCharacter {596 *597 * private String name;598 * private int age;599 * // constructor omitted600 *601 * public String getName() {602 * return this.name;603 * }604 * }605 *606 * TolkienCharacter frodo = new TolkienCharacter("Frodo", 33);607 *608 * // assertions will pass :609 * assertThat(frodo).hasFieldOrProperty("name")610 * .hasFieldOrProperty("age"); // private field are matched by default611 *612 * // assertions will fail :613 * assertThat(frodo).hasFieldOrProperty("not_exists");614 * assertThat(frodo).hasFieldOrProperty(null);615 * // disable looking for private fields616 * Assertions.setAllowExtractingPrivateFields(false);617 * assertThat(frodo).hasFieldOrProperty("age"); </code></pre>618 *619 * @param name the field/property name to check620 * @return {@code this} assertion object.621 * @throws AssertionError if the actual object is {@code null}.622 * @throws IllegalArgumentException if name is {@code null}.623 * @throws AssertionError if the actual object has not the given field/property624 */625 public SELF hasFieldOrProperty(String name) {626 objects.assertHasFieldOrProperty(info, actual, name);627 return myself;628 }629 /**630 * Asserts that the actual object has the specified field or property with the given value.631 * <p>632 * Private fields are matched by default but this can be changed by calling {@link Assertions#setAllowExtractingPrivateFields(boolean) Assertions.setAllowExtractingPrivateFields(false)}.633 * <p>634 * If you are looking to chain multiple assertions on different properties in a type safe way, consider chaining {@link #returns(Object, Function)} calls.635 * <p>636 * Example:637 * <pre><code class='java'> public class TolkienCharacter {638 * private String name;639 * private int age;640 * // constructor omitted641 *642 * public String getName() {643 * return this.name;644 * }645 * }646 *647 * TolkienCharacter frodo = new TolkienCharacter("Frodo", 33);648 * TolkienCharacter noname = new TolkienCharacter(null, 33);649 *650 * // assertions will pass :651 * assertThat(frodo).hasFieldOrPropertyWithValue("name", "Frodo");652 * assertThat(frodo).hasFieldOrPropertyWithValue("age", 33);653 * assertThat(noname).hasFieldOrPropertyWithValue("name", null);654 *655 * // assertions will fail :656 * assertThat(frodo).hasFieldOrPropertyWithValue("name", "not_equals");657 * assertThat(frodo).hasFieldOrPropertyWithValue(null, 33);658 * assertThat(frodo).hasFieldOrPropertyWithValue("age", null);659 * assertThat(noname).hasFieldOrPropertyWithValue("name", "Frodo");660 * // disable extracting private fields661 * Assertions.setAllowExtractingPrivateFields(false);662 * assertThat(frodo).hasFieldOrPropertyWithValue("age", 33); </code></pre>663 *664 * @param name the field/property name to check665 * @param value the field/property expected value666 * @return {@code this} assertion object.667 * @throws AssertionError if the actual object is {@code null}.668 * @throws IllegalArgumentException if name is {@code null}.669 * @throws AssertionError if the actual object has not the given field/property670 * @throws AssertionError if the actual object has the given field/property but not with the expected value671 *672 * @see AbstractObjectAssert#hasFieldOrProperty(java.lang.String)673 */674 public SELF hasFieldOrPropertyWithValue(String name, Object value) {675 objects.assertHasFieldOrPropertyWithValue(info, actual, name, value);676 return myself;677 }678 /**679 * Extracts the values of given fields/properties from the object under test into a list, this new list becoming680 * the object under test.681 * <p>682 * If you extract "id", "name" and "email" fields/properties then the list will contain the id, name and email values683 * of the object under test, you can then perform list assertions on the extracted values.684 * <p>685 * If the object under test is a {@link Map} with {@link String} keys, extracting will extract values matching the given fields/properties.686 * <p>687 * Nested fields/properties are supported, specifying "adress.street.number" is equivalent to:688 * <pre><code class='java'> // "adress.street.number" corresponding to pojo properties689 * actual.getAdress().getStreet().getNumber();</code></pre>690 * or if address is a {@link Map}:691 * <pre><code class='java'> // "adress" is a Map property (that is getAdress() returns a Map)692 * actual.getAdress().get("street").getNumber();</code></pre>693 * <p>694 * Private fields can be extracted unless you call {@link Assertions#setAllowExtractingPrivateFields(boolean) Assertions.setAllowExtractingPrivateFields(false)}.695 * <p>696 * Example:697 * <pre><code class='java'> // Create frodo, setting its name, age and Race (Race having a name property)698 * TolkienCharacter frodo = new TolkienCharacter(&quot;Frodo&quot;, 33, HOBBIT);699 *700 * // let's verify Frodo's name, age and race name:701 * assertThat(frodo).extracting(&quot;name&quot;, &quot;age&quot;, &quot;race.name&quot;)702 * .containsExactly(&quot;Frodo&quot;, 33, &quot;Hobbit&quot;);</code></pre>703 *704 * A property with the given name is looked for first, if it doesn't exist then a field with the given name is looked705 * for, if the field is not accessible (i.e. does not exist) an {@link IntrospectionError} is thrown.706 * <p>707 * Note that the order of extracted values is consistent with the order of the given property/field.708 *709 * @param propertiesOrFields the properties/fields to extract from the initial object under test710 * @return a new assertion object whose object under test is the list containing the extracted properties/fields values711 * @throws IntrospectionError if one of the given name does not match a field or property712 */713 @CheckReturnValue714 public AbstractListAssert<?, List<?>, Object, ObjectAssert<Object>> extracting(String... propertiesOrFields) {715 Tuple values = byName(propertiesOrFields).apply(actual);716 String extractedPropertiesOrFieldsDescription = extractedDescriptionOf(propertiesOrFields);717 String description = mostRelevantDescription(info.description(), extractedPropertiesOrFieldsDescription);718 return newListAssertInstance(values.toList()).withAssertionState(myself).as(description);719 }720 /**721 * Extracts the value of given field/property from the object under test, the extracted value becoming the new object under test.722 * <p>723 * If the object under test is a {@link Map}, the {@code propertyOrField} parameter is used as a key to the map.724 * <p>725 * Nested fields/properties are supported, specifying "adress.street.number" is equivalent to:726 * <pre><code class='java'> // "adress.street.number" corresponding to pojo properties727 * actual.getAdress().getStreet().getNumber();</code></pre>728 * or if address is a {@link Map}:729 * <pre><code class='java'> // "adress" is a Map property (that is getAdress() returns a Map)730 * actual.getAdress().get("street").getNumber();</code></pre>731 * <p>732 * Private field can be extracted unless you call {@link Assertions#setAllowExtractingPrivateFields(boolean) Assertions.setAllowExtractingPrivateFields(false)}.733 * <p>734 * Note that since the value is extracted as an Object, only Object assertions can be chained after extracting.735 * <p>736 * Example:737 * <pre><code class='java'> // Create frodo, setting its name, age and Race (Race having a name property)738 * TolkienCharacter frodo = new TolkienCharacter(&quot;Frodo&quot;, 33, HOBBIT);739 *740 * // let's extract and verify Frodo's name:741 * assertThat(frodo).extracting(&quot;name&quot;)742 * .isEqualTo(&quot;Frodo&quot;);743 * // or its race name:744 * assertThat(frodo).extracting(&quot;race.name&quot;)745 * .isEqualTo(&quot;Hobbit&quot;);746 *747 * // The extracted value being a String, we would like to use String assertions but we can't due to Java generics limitations.748 * // The following assertion does NOT compile:749 * assertThat(frodo).extracting(&quot;name&quot;)750 * .startsWith(&quot;Fro&quot;);751 *752 * // To get String assertions, use {@link #extracting(String, InstanceOfAssertFactory)}:753 * assertThat(frodo).extracting(&quot;name&quot;, as(InstanceOfAssertFactories.STRING))754 * .startsWith(&quot;Fro&quot;);</code></pre>755 * <p>756 * A property with the given name is looked for first, if it doesn't exist then a field with the given name is looked757 * for, if the field is not accessible (i.e. does not exist) an {@link IntrospectionError} is thrown.758 *759 * @param propertyOrField the property/field to extract from the initial object under test760 * @return a new {@link ObjectAssert} instance whose object under test is the extracted property/field value761 * @throws IntrospectionError if one of the given name does not match a field or property762 *763 * @since 3.13.0764 * @see #extracting(String, InstanceOfAssertFactory)765 */766 @CheckReturnValue767 public AbstractObjectAssert<?, ?> extracting(String propertyOrField) {768 return super.extracting(propertyOrField, this::newObjectAssert);769 }770 /**771 * Extracts the value of given field/property from the object under test, the extracted value becoming the new object under test.772 * <p>773 * If the object under test is a {@link Map}, the {@code propertyOrField} parameter is used as a key to the map.774 * <p>775 * Nested field/property is supported, specifying "address.street.number" is equivalent to get the value776 * corresponding to actual.getAddress().getStreet().getNumber()777 * <p>778 * Private field can be extracted unless you call {@link Assertions#setAllowExtractingPrivateFields(boolean) Assertions.setAllowExtractingPrivateFields(false)}.779 * <p>780 * The {@code assertFactory} parameter allows to specify an {@link InstanceOfAssertFactory}, which is used to get the781 * assertions narrowed to the factory type.782 * <p>783 * Wrapping the given {@link InstanceOfAssertFactory} with {@link Assertions#as(InstanceOfAssertFactory)} makes the784 * assertion more readable.785 * <p>786 * Example:787 * <pre><code class='java'> // Create frodo, setting its name, age and Race (Race having a name property)788 * TolkienCharacter frodo = new TolkienCharacter(&quot;Frodo&quot;, 33, HOBBIT);789 *790 * // let's extract and verify Frodo's name:791 * assertThat(frodo).extracting(&quot;name&quot;, as(InstanceOfAssertFactories.STRING))792 * .startsWith(&quot;Fro&quot;);793 *794 * // The following assertion will fail as Frodo's name is not an Integer:795 * assertThat(frodo).extracting(&quot;name&quot;, as(InstanceOfAssertFactories.INTEGER))796 * .isZero();</code></pre>797 * <p>798 * A property with the given name is looked for first, if it doesn't exist then a field with the given name is looked799 * for, if the field is not accessible (i.e. does not exist) an {@link IntrospectionError} is thrown.800 *801 * @param <ASSERT> the type of the resulting {@code Assert}802 * @param propertyOrField the property/field to extract from the initial object under test803 * @param assertFactory the factory which verifies the type and creates the new {@code Assert}804 * @return a new narrowed {@link Assert} instance whose object under test is the extracted property/field value805 * @throws NullPointerException if the given factory is {@code null}806 * @throws IntrospectionError if one of the given name does not match a field or property807 *808 * @since 3.14.0809 */810 @CheckReturnValue811 public <ASSERT extends AbstractAssert<?, ?>> ASSERT extracting(String propertyOrField,812 InstanceOfAssertFactory<?, ASSERT> assertFactory) {813 return super.extracting(propertyOrField, this::newObjectAssert).asInstanceOf(assertFactory);814 }815 /**816 * Uses the given {@link Function}s to extract the values from the object under test into a list, this new list becoming817 * the object under test.818 * <p>819 * If the given {@link Function}s extract the id, name and email values then the list will contain the id, name and email values820 * of the object under test, you can then perform list assertions on the extracted values.821 * <p>822 * Example:823 * <pre><code class='java'> // Create frodo, setting its name, age and Race (Race having a name property)824 * TolkienCharacter frodo = new TolkienCharacter(&quot;Frodo&quot;, 33, HOBBIT);825 *826 * // let's verify Frodo's name, age and race name:827 * assertThat(frodo).extracting(TolkienCharacter::getName,828 * character -&gt; character.age, // public field829 * character -&gt; character.getRace().getName())830 * .containsExactly(&quot;Frodo&quot;, 33, "Hobbit");</code></pre>831 * <p>832 * Note that the order of extracted values is consistent with the order of given extractor functions.833 *834 * @param extractors the extractor functions to extract values from the Object under test.835 * @return a new assertion object whose object under test is the list containing the extracted values836 */837 @SuppressWarnings("unchecked")838 @CheckReturnValue839 public AbstractListAssert<?, List<?>, Object, ObjectAssert<Object>> extracting(Function<? super ACTUAL, ?>... extractors) {840 requireNonNull(extractors, shouldNotBeNull("extractors").create());841 List<Object> values = Stream.of(extractors)842 .map(extractor -> extractor.apply(actual))843 .collect(toList());844 return newListAssertInstance(values).withAssertionState(myself);845 }846 /**847 * Uses the given {@link Function} to extract a value from the object under test, the extracted value becoming the new object under test.848 * <p>849 * Note that since the value is extracted as an Object, only Object assertions can be chained after extracting.850 * <p>851 * Example:852 * <pre><code class='java'> // Create frodo, setting its name, age and Race853 * TolkienCharacter frodo = new TolkienCharacter(&quot;Frodo&quot;, 33, HOBBIT);854 *855 * // let's extract and verify Frodo's name:856 * assertThat(frodo).extracting(TolkienCharacter::getName)857 * .isEqualTo(&quot;Frodo&quot;);858 *859 * // The extracted value being a String, we would like to use String assertions but we can't due to Java generics limitations.860 * // The following assertion does NOT compile:861 * assertThat(frodo).extracting(TolkienCharacter::getName)862 * .startsWith(&quot;Fro&quot;);863 *864 * // To get String assertions, use {@link #extracting(Function, InstanceOfAssertFactory)}:865 * assertThat(frodo).extracting(TolkienCharacter::getName, as(InstanceOfAssertFactories.STRING))866 * .startsWith(&quot;Fro&quot;);</code></pre>867 *868 * @param <T> the expected extracted value type.869 * @param extractor the extractor function used to extract the value from the object under test.870 * @return a new {@link ObjectAssert} instance whose object under test is the extracted value871 *872 * @since 3.11.0873 * @see #extracting(Function, InstanceOfAssertFactory)874 */875 @CheckReturnValue876 public <T> AbstractObjectAssert<?, T> extracting(Function<? super ACTUAL, T> extractor) {877 return super.extracting(extractor, this::newObjectAssert);878 }879 /**880 * Uses the given {@link Function} to extract a value from the object under test, the extracted value becoming the new object under test.881 * <p>882 * Note that since the value is extracted as an Object, only Object assertions can be chained after extracting.883 * <p>884 * The {@code assertFactory} parameter allows to specify an {@link InstanceOfAssertFactory}, which is used to get the885 * assertions narrowed to the factory type.886 * <p>887 * Wrapping the given {@link InstanceOfAssertFactory} with {@link Assertions#as(InstanceOfAssertFactory)} makes the888 * assertion more readable.889 * <p>890 * Example:891 * <pre><code class='java'> // Create frodo, setting its name, age and Race892 * TolkienCharacter frodo = new TolkienCharacter(&quot;Frodo&quot;, 33, HOBBIT);893 *894 * // let's extract and verify Frodo's name:895 * assertThat(frodo).extracting(TolkienCharacter::getName, as(InstanceOfAssertFactories.STRING))896 * .startsWith(&quot;Fro&quot;);897 *898 * // The following assertion will fail as Frodo's name is not an Integer:899 * assertThat(frodo).extracting(TolkienCharacter::getName, as(InstanceOfAssertFactories.INTEGER))900 * .isZero();</code></pre>901 *902 * @param <T> the expected extracted value type903 * @param <ASSERT> the type of the resulting {@code Assert}904 * @param extractor the extractor function used to extract the value from the object under test905 * @param assertFactory the factory which verifies the type and creates the new {@code Assert}906 * @return a new narrowed {@link Assert} instance whose object under test is the extracted value907 * @throws NullPointerException if the given factory is {@code null}908 *909 * @since 3.14.0910 */911 @CheckReturnValue912 public <T, ASSERT extends AbstractAssert<?, ?>> ASSERT extracting(Function<? super ACTUAL, T> extractor,913 InstanceOfAssertFactory<?, ASSERT> assertFactory) {914 return super.extracting(extractor, this::newObjectAssert).asInstanceOf(assertFactory);915 }916 /**917 * @deprecated Prefer calling {@link #usingRecursiveComparison()} for comparing objects field by field as it offers more flexibility, better reporting and an easier to use API.918 *919 * Asserts that the object under test (actual) is equal to the given object based on a recursive property/field by property/field comparison (including920 * inherited ones). This can be useful if actual's {@code equals} implementation does not suit you.921 * The recursive property/field comparison is <b>not</b> applied on fields having a custom {@code equals} implementation, i.e.922 * the overridden {@code equals} method will be used instead of a field by field comparison.923 * <p>924 * The recursive comparison handles cycles. By default {@code floats} are compared with a precision of 1.0E-6 and {@code doubles} with 1.0E-15.925 * <p>926 * You can specify a custom comparator per (nested) fields or type with respectively {@link #usingComparatorForFields(Comparator, String...) usingComparatorForFields(Comparator, String...)}927 * and {@link #usingComparatorForType(Comparator, Class)}.928 * <p>929 * The objects to compare can be of different types but must have the same properties/fields. For example if actual object has a name String field, it is expected the other object to also have one.930 * If an object has a field and a property with the same name, the property value will be used over the field.931 * <p>932 * Example:933 * <pre><code class='java'> public class Person {934 * public String name;935 * public double height;936 * public Home home = new Home();937 * public Person bestFriend;938 * // constructor with name and height omitted for brevity939 * }940 *941 * public class Home {942 * public Address address = new Address();943 * }944 *945 * public static class Address {946 * public int number = 1;947 * }948 *949 * Person jack = new Person("Jack", 1.80);950 * jack.home.address.number = 123;951 *952 * Person jackClone = new Person("Jack", 1.80);953 * jackClone.home.address.number = 123;954 *955 * // cycle are handled in comparison956 * jack.bestFriend = jackClone;957 * jackClone.bestFriend = jack;958 *959 * // will fail as equals compares object references960 * assertThat(jack).isEqualTo(jackClone);961 *962 * // jack and jackClone are equals when doing a recursive field by field comparison963 * assertThat(jack).isEqualToComparingFieldByFieldRecursively(jackClone);964 *965 * // any type/field can be compared with a a specific comparator.966 * // let's change jack's height a little bit967 * jack.height = 1.81;968 *969 * // assertion fails because of the height difference970 * // (the default precision comparison for double is 1.0E-15)971 * assertThat(jack).isEqualToComparingFieldByFieldRecursively(jackClone);972 *973 * // this succeeds because we allow a 0.5 tolerance on double974 * assertThat(jack).usingComparatorForType(new DoubleComparator(0.5), Double.class)975 * .isEqualToComparingFieldByFieldRecursively(jackClone);976 *977 * // you can set a comparator on specific fields (nested fields are supported)978 * assertThat(jack).usingComparatorForFields(new DoubleComparator(0.5), "height")979 * .isEqualToComparingFieldByFieldRecursively(jackClone);</code></pre>980 *981 * @param other the object to compare {@code actual} to.982 * @return {@code this} assertion object.983 * @throws AssertionError if the actual object is {@code null}.984 * @throws AssertionError if the actual and the given objects are not deeply equal property/field by property/field.985 * @throws IntrospectionError if one property/field to compare can not be found.986 */987 @Deprecated988 public SELF isEqualToComparingFieldByFieldRecursively(Object other) {989 objects.assertIsEqualToComparingFieldByFieldRecursively(info, actual, other, comparatorByPropertyOrField,990 getComparatorsByType());991 return myself;992 }993 /**994 * Verifies that the object under test returns the given expected value from the given {@link Function},995 * a typical usage is to pass a method reference to assert object's property.996 * <p>997 * Wrapping the given {@link Function} with {@link Assertions#from(Function)} makes the assertion more readable.998 * <p>999 * Example:1000 * <pre><code class="java"> // from is not mandatory but it makes the assertions more readable1001 * assertThat(frodo).returns("Frodo", from(TolkienCharacter::getName))1002 * .returns("Frodo", TolkienCharacter::getName) // no from :(1003 * .returns(HOBBIT, from(TolkienCharacter::getRace));</code></pre>1004 *...

Full Screen

Full Screen

Source:CustomAssertJ.java Github

copy

Full Screen

...41// return (AbstractObjectAssert) super.as(description, args);42// }43//44// public SELF isEqualToIgnoringNullFields(Object other) {45// this.objects.assertIsEqualToIgnoringNullFields(this.info, this.actual, other, this.comparatorByPropertyOrField, this.getComparatorsByType());46// return (AbstractObjectAssert) this.myself;47// }48//49// public SELF isEqualToComparingOnlyGivenFields(Object other, String... propertiesOrFieldsUsedInComparison) {50// this.objects.assertIsEqualToComparingOnlyGivenFields(this.info, this.actual, other, this.comparatorByPropertyOrField, this.getComparatorsByType(), propertiesOrFieldsUsedInComparison);51// return (AbstractObjectAssert) this.myself;52// }53//54// public SELF isEqualToIgnoringGivenFields(Object other, String... propertiesOrFieldsToIgnore) {55// this.objects.assertIsEqualToIgnoringGivenFields(this.info, this.actual, other, this.comparatorByPropertyOrField, this.getComparatorsByType(), propertiesOrFieldsToIgnore);56// return (AbstractObjectAssert) this.myself;57// }58//59// public SELF hasNoNullFieldsOrProperties() {60// this.objects.assertHasNoNullFieldsOrPropertiesExcept(this.info, this.actual, new String[0]);61// return (AbstractObjectAssert) this.myself;62// }63//64// public SELF hasAllNullFieldsOrProperties() {65// this.objects.assertHasAllNullFieldsOrPropertiesExcept(this.info, this.actual, new String[0]);66// return (AbstractObjectAssert) this.myself;67// }68//69// public SELF hasNoNullFieldsOrPropertiesExcept(String... propertiesOrFieldsToIgnore) {70// this.objects.assertHasNoNullFieldsOrPropertiesExcept(this.info, this.actual, propertiesOrFieldsToIgnore);71// return (AbstractObjectAssert) this.myself;72// }73//74// public SELF hasAllNullFieldsOrPropertiesExcept(String... propertiesOrFieldsToIgnore) {75// this.objects.assertHasAllNullFieldsOrPropertiesExcept(this.info, this.actual, propertiesOrFieldsToIgnore);76// return (AbstractObjectAssert) this.myself;77// }78//79// public SELF isEqualToComparingFieldByField(Object other) {80// this.objects.assertIsEqualToIgnoringGivenFields(this.info, this.actual, other, this.comparatorByPropertyOrField, this.getComparatorsByType(), new String[0]);81// return (AbstractObjectAssert) this.myself;82// }83//84// protected TypeComparators getComparatorsByType() {85// if (this.comparatorByType == null) {86// this.comparatorByType = TypeComparators.defaultTypeComparators();87// }88//89// return this.comparatorByType;90// }91//92// @CheckReturnValue93// public <T> SELF usingComparatorForFields(Comparator<T> comparator, String... propertiesOrFields) {94// String[] var3 = propertiesOrFields;95// int var4 = propertiesOrFields.length;96//97// for (int var5 = 0; var5 < var4; ++var5) {98// String propertyOrField = var3[var5];99// this.comparatorByPropertyOrField.put(propertyOrField, comparator);100// }101//102// return (AbstractObjectAssert) this.myself;103// }104//105// @CheckReturnValue106// public <T> SELF usingComparatorForType(Comparator<? super T> comparator, Class<T> type) {107// this.getComparatorsByType().put(type, comparator);108// return (AbstractObjectAssert) this.myself;109// }110//111// public SELF hasFieldOrProperty(String name) {112// this.objects.assertHasFieldOrProperty(this.info, this.actual, name);113// return (AbstractObjectAssert) this.myself;114// }115//116// public SELF hasFieldOrPropertyWithValue(String name, Object value) {117// this.objects.assertHasFieldOrPropertyWithValue(this.info, this.actual, name, value);118// return (AbstractObjectAssert) this.myself;119// }120//121// @CheckReturnValue122// public AbstractListAssert<?, List<?>, Object, ObjectAssert<Object>> extracting(String... propertiesOrFields) {123// Tuple values = (Tuple) Extractors.byName(propertiesOrFields).apply(this.actual);124// String extractedPropertiesOrFieldsDescription = Extractors.extractedDescriptionOf(propertiesOrFields);125// String description = Description.mostRelevantDescription(this.info.description(), extractedPropertiesOrFieldsDescription);126// return this.newListAssertInstance(values.toList()).as(description, new Object[0]);127// }128//129// @CheckReturnValue130// public AbstractObjectAssert<?, ?> extracting(String propertyOrField) {131// Object value = Extractors.byName(propertyOrField).apply(this.actual);132// String extractedPropertyOrFieldDescription = Extractors.extractedDescriptionOf(new String[]{propertyOrField});133// String description = Description.mostRelevantDescription(this.info.description(), extractedPropertyOrFieldDescription);134// return this.newObjectAssert(value).as(description);135// }136//137// @CheckReturnValue138// public AbstractListAssert<?, List<?>, Object, ObjectAssert<Object>> extracting(Function<? super ACTUAL, ?>... extractors) {139// Objects.requireNonNull(extractors, ShouldNotBeNull.shouldNotBeNull("extractors").create());140// List<Object> values = (List) Stream.of(extractors).map((extractor) -> {141// return extractor.apply(this.actual);142// }).collect(Collectors.toList());143// return (AbstractListAssert) this.newListAssertInstance(values).withAssertionState(this.myself);144// }145//146// @CheckReturnValue147// public <T> AbstractObjectAssert<?, T> extracting(Function<? super ACTUAL, T> extractor) {148// Objects.requireNonNull(extractor, ShouldNotBeNull.shouldNotBeNull("extractor").create());149// T extractedValue = extractor.apply(this.actual);150// return this.newObjectAssert(extractedValue).withAssertionState(this.myself);151// }152//153//154// public <T> SELF returns(T expected, Function<ACTUAL, T> from) {155// Objects.requireNonNull(from, "The given getter method/Function must not be null");156// this.objects.assertEqual(this.info, from.apply(this.actual), expected);157// return (AbstractObjectAssert) this.myself;158// }159//160// @Beta161// public RecursiveComparisonAssert<?> usingRecursiveComparison() {162// return this.usingRecursiveComparison(new RecursiveComparisonConfiguration());163// }164//165// @Beta166// public RecursiveComparisonAssert<?> usingRecursiveComparison(RecursiveComparisonConfiguration recursiveComparisonConfiguration) {167// return ((RecursiveComparisonAssert) (new RecursiveComparisonAssert(this.actual, recursiveComparisonConfiguration)).withAssertionState(this.myself)).withTypeComparators(this.comparatorByType);168// }169//170// protected <T> AbstractObjectAssert<?, T> newObjectAssert(T objectUnderTest) {171// return new ObjectAssert(objectUnderTest);172// }173//174// SELF withAssertionState(AbstractAssert assertInstance) {175// if (assertInstance instanceof AbstractObjectAssert) {176// AbstractObjectAssert objectAssert = (AbstractObjectAssert) assertInstance;177// return ((AbstractObjectAssert) super.withAssertionState(assertInstance)).withTypeComparator(objectAssert.comparatorByType).withComparatorByPropertyOrField(objectAssert.comparatorByPropertyOrField);178// } else {179// return (AbstractObjectAssert) super.withAssertionState(assertInstance);180// }181// }182//183// SELF withTypeComparator(TypeComparators comparatorsByType) {184// this.comparatorByType = comparatorsByType;185// return (AbstractObjectAssert) this.myself;186// }187//188// public SELF isEqualToComparingFieldByFieldRecursively(Object other) {189// this.objects.assertIsEqualToComparingFieldByFieldRecursively(this.info, this.actual, other, this.comparatorByPropertyOrField, this.getComparatorsByType());190// return (AbstractObjectAssert) this.myself;191// }192//193// SELF withComparatorByPropertyOrField(Map<String, Comparator<?>> comparatorsToPropaget) {194// this.comparatorByPropertyOrField = comparatorsToPropaget;195// return (AbstractObjectAssert) this.myself;196// }197//198//199//}...

Full Screen

Full Screen

getComparatorsByType

Using AI Code Generation

copy

Full Screen

1import org.assertj.core.api.Assertions;2import org.assertj.core.api.AbstractObjectAssert;3import org.assertj.core.api.ObjectAssert;4import org.assertj.core.api.ObjectAssertBaseTest;5import org.assertj.core.util.ComparatorBasedComparisonStrategy;6import org.assertj.core.util.Comparators;7import org.assertj.core.util.introspection.IntrospectionError;8import org.junit.Test;9import static org.assertj.core.api.Assertions.assertThat;10import java.util.Comparator;11import java.util.List;12import java.util.ArrayList;13import java.util.Collections;14import java.util.Arrays;15import java.util.Set;16import java.util.HashSet;17import java.util.Map;18import java.util.HashMap;19import java.util.LinkedHashMap;20import java.util.LinkedHashSet;21import java.util.TreeMap;22import java.util.TreeSet;23import java.util.SortedMap;24import java.util.SortedSet;25import java.util.NavigableMap;26import java.util.NavigableSet;27import java.util.concurrent.ConcurrentMap;28import java.util.concurrent.ConcurrentNavigableMap;29import java.util.concurrent.ConcurrentNavigableSet;30import java.util.concurrent.ConcurrentSkipListMap;31import java.util.concurrent.ConcurrentSkipListSet;32import static org.assertj.core.api.Assertions.assertThat;33import static org.assertj.core.util.AssertionsUtil.assertThatAssertionErrorIsThrownBy;34class MyObject {35 private String name;36 public MyObject(String name) {37 this.name = name;38 }39 public String getName() {40 return name;41 }42}43class MyObjectComparator implements Comparator<MyObject> {44 public int compare(MyObject o1, MyObject o2) {45 return o1.getName().compareTo(o2.getName());46 }47}48class MyObjectComparator2 implements Comparator<MyObject> {49 public int compare(MyObject o1, MyObject o2) {50 return o1.getName().compareTo(o2.getName());51 }52}53public class AssertJTest {54 public void test() {55 MyObject myObject1 = new MyObject("myObject1");56 MyObject myObject2 = new MyObject("myObject2");57 MyObject myObject3 = new MyObject("myObject3");58 MyObject myObject4 = new MyObject("myObject4");59 MyObject myObject5 = new MyObject("myObject5");60 MyObject myObject6 = new MyObject("myObject6");61 MyObject myObject7 = new MyObject("myObject7");

Full Screen

Full Screen

getComparatorsByType

Using AI Code Generation

copy

Full Screen

1package com.puppycrawl.tools.checkstyle.api;2import static org.assertj.core.api.Assertions.assertThat;3import static org.junit.Assert.assertEquals;4import java.util.Comparator;5import java.util.List;6import org.junit.Test;7public class AbstractObjectAssertTest {8 public void testGetComparatorsByType() {9 final AbstractObjectAssert<?, ?> abstractObjectAssert = assertThat("test");10 final List<Comparator<?>> comparators = abstractObjectAssert.getComparatorsByType();11 assertEquals(0, comparators.size());12 }13}14package org.assertj.core.api;15import java.util.Comparator;16import java.util.List;17 extends AbstractAssert<S, A> {18 protected AbstractObjectAssert(A actual, Class<?> selfType) {19 super(actual, selfType);20 }21 public List<Comparator<?>> getComparatorsByType() {

Full Screen

Full Screen

getComparatorsByType

Using AI Code Generation

copy

Full Screen

1package org.example;2import java.util.List;3import org.assertj.core.api.AbstractObjectAssert;4import org.assertj.core.api.Assertions;5public class App {6 public static void main(String[] args) {7 AbstractObjectAssert<?, ?> objectAssert = Assertions.assertThat("test");8 List<AbstractObjectAssert<?, ?>> comparatorsByType = objectAssert.getComparatorsByType();9 System.out.println(comparatorsByType);10 }11}

Full Screen

Full Screen

getComparatorsByType

Using AI Code Generation

copy

Full Screen

1import org.assertj.core.api.AbstractObjectAssert;2public class GetComparatorsByType {3 public static void main(String[] args) {4 AbstractObjectAssert<?, ?> assertObject = new AbstractObjectAssert<Object, Object>(null) {5 protected void isNotNull() {6 }7 };8 assertObject.getComparatorsByType();9 }10}

Full Screen

Full Screen

getComparatorsByType

Using AI Code Generation

copy

Full Screen

1package org.example;2import static org.assertj.core.api.Assertions.assertThat;3import java.util.Comparator;4import org.junit.jupiter.api.Test;5public class Test1 {6 public void test1() {7 assertThat(new Object()).usingComparatorForType(new Comparator<Object>() {8 public int compare(Object o1, Object o2) {9 return 0;10 }11 }, Object.class).isEqualTo(new Object());12 }13}14package org.example;15import static org.assertj.core.api.Assertions.assertThat;16import java.util.Comparator;17import org.junit.jupiter.api.Test;18public class Test2 {19 public void test2() {20 assertThat(new Object()).usingComparatorForType(new Comparator<Object>() {21 public int compare(Object o1, Object o2) {22 return 0;23 }24 }, Object.class).isEqualTo(new Object());25 }26}27package org.example;28import static org.assertj.core.api.Assertions.assertThat;29import java.util.Comparator;30import org.junit.jupiter.api.Test;31public class Test3 {32 public void test3() {33 assertThat(new Object()).usingComparatorForType(new Comparator<Object>() {34 public int compare(Object o1, Object o2) {35 return 0;36 }37 }, Object.class).isEqualTo(new Object());38 }39}40package org.example;41import static org.assertj.core.api.Assertions.assertThat;42import java.util.Comparator;43import org.junit.jupiter.api.Test;44public class Test4 {45 public void test4() {46 assertThat(new Object()).usingComparatorForType(new Comparator<Object>() {47 public int compare(Object o1, Object o2) {48 return 0;49 }50 }, Object.class).isEqualTo(new Object());51 }52}53package org.example;54import static org.assertj.core.api.Assertions.assertThat;55import java.util.Comparator;56import org.junit.jupiter.api.Test;

Full Screen

Full Screen

getComparatorsByType

Using AI Code Generation

copy

Full Screen

1public class AssertJTest {2 public static void main(String[] args) {3 ObjectAssert objectAssert = new ObjectAssert(new Object());4 objectAssert.getComparatorsByType();5 }6}7public class AssertJTest {8 public static void main(String[] args) {9 Assert objectAssert = new Assert(new Object());10 objectAssert.getComparatorsByType();11 }12}132.java:6: error: getComparatorsByType() is not public in Assert; cannot be accessed from outside package14 objectAssert.getComparatorsByType();

Full Screen

Full Screen

getComparatorsByType

Using AI Code Generation

copy

Full Screen

1import org.junit.Test;2import java.util.Comparator;3import static org.assertj.core.api.Assertions.assertThat;4public class AssertJExample1 {5 public void getComparatorsByTypeTest() {6 assertThat(1).usingComparatorForType(Comparator.naturalOrder(), Integer.class).isEqualTo(1);7 }8}9Recommended Posts: AssertJ | Using getComparatorsByType() method10AssertJ | Using getComparator() method11AssertJ | Using usingDefaultComparator() method12AssertJ | Using usingComparator() method

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