How to use getComparatorsForElementPropertyOrFieldTypes method of org.assertj.core.api.AbstractIterableAssert class

Best Assertj code snippet using org.assertj.core.api.AbstractIterableAssert.getComparatorsForElementPropertyOrFieldTypes

Source:AbstractIterableAssert.java Github

copy

Full Screen

...1713 * @since 2.5.0 / 3.5.01714 */1715 @CheckReturnValue1716 public <T> SELF usingComparatorForElementFieldsWithType(Comparator<T> comparator, Class<T> type) {1717 getComparatorsForElementPropertyOrFieldTypes().put(type, comparator);1718 return myself;1719 }1720 /**1721 * Allows to set a specific comparator for the given type of elements or their fields.1722 * Extends {@link #usingComparatorForElementFieldsWithType} by applying comparator specified for given type1723 * to elements themselves, not only to their fields.1724 * <p>1725 * Usage of this method affects comparators set by next methods:1726 * <ul>1727 * <li>{@link #usingFieldByFieldElementComparator}</li>1728 * <li>{@link #usingElementComparatorOnFields}</li>1729 * <li>{@link #usingElementComparatorIgnoringFields}</li>1730 * <li>{@link #usingRecursiveFieldByFieldElementComparator}</li>1731 * </ul>1732 * <p>1733 * Example:1734 * <pre><code class='java'>1735 * // assertion will pass1736 * assertThat(asList("some", new BigDecimal("4.2")))1737 * .usingComparatorForType(BIG_DECIMAL_COMPARATOR, BigDecimal.class)1738 * .contains(new BigDecimal("4.20"));1739 * </code></pre>1740 *1741 * @param <T> the type of elements to compare.1742 * @param comparator the {@link java.util.Comparator} to use1743 * @param type the {@link java.lang.Class} of the type of the element or element fields the comparator should be used for1744 * @return {@code this} assertions object1745 * @since 2.9.0 / 3.9.01746 */1747 @CheckReturnValue1748 public <T> SELF usingComparatorForType(Comparator<T> comparator, Class<T> type) {1749 if (iterables.getComparator() == null) {1750 usingElementComparator(new ExtendedByTypesComparator(getComparatorsByType()));1751 }1752 getComparatorsForElementPropertyOrFieldTypes().put(type, comparator);1753 getComparatorsByType().put(type, comparator);1754 return myself;1755 }1756 /**1757 * Use field/property by field/property comparison (including inherited fields/properties) instead of relying on1758 * actual type A <code>equals</code> method to compare group elements for incoming assertion checks. Private fields1759 * are included but this can be disabled using {@link Assertions#setAllowExtractingPrivateFields(boolean)}.1760 * <p>1761 * This can be handy if <code>equals</code> method of the objects to compare does not suit you.1762 * <p>1763 * Note that the comparison is <b>not</b> recursive, if one of the fields/properties is an Object, it will be compared1764 * to the other field/property using its <code>equals</code> method.1765 * <p>1766 * You can specify a custom comparator per name or type of element field with1767 * {@link #usingComparatorForElementFieldsWithNames(Comparator, String...)}1768 * and {@link #usingComparatorForElementFieldsWithType(Comparator, Class)}.1769 * <p>1770 * Example:1771 * <pre><code class='java'> TolkienCharacter frodo = new TolkienCharacter("Frodo", 33, HOBBIT);1772 * TolkienCharacter frodoClone = new TolkienCharacter("Frodo", 33, HOBBIT);1773 *1774 * // Fail if equals has not been overridden in TolkienCharacter as equals default implementation only compares references1775 * assertThat(newArrayList(frodo)).contains(frodoClone);1776 *1777 * // frodo and frodoClone are equals when doing a field by field comparison.1778 * assertThat(newArrayList(frodo)).usingFieldByFieldElementComparator().contains(frodoClone);</code></pre>1779 *1780 * @return {@code this} assertion object.1781 */1782 @CheckReturnValue1783 public SELF usingFieldByFieldElementComparator() {1784 return usingExtendedByTypesElementComparator(new FieldByFieldComparator(comparatorsForElementPropertyOrFieldNames,1785 getComparatorsForElementPropertyOrFieldTypes()));1786 }1787 /**1788 * Use a recursive field/property by field/property comparison (including inherited fields/properties)1789 * instead of relying on actual type <code>equals</code> method to compare group elements for incoming1790 * assertion checks. This can be useful if actual's {@code equals} implementation does not suit you.1791 * <p>1792 * The recursive property/field comparison is <b>not</b> applied on fields having a custom {@code equals}1793 * implementation, i.e. the overridden {@code equals} method will be used instead of a field/property by field/property1794 * comparison.1795 * <p>1796 * The recursive comparison handles cycles.1797 * <p>1798 * You can specify a custom comparator per (nested) name or type of element field with1799 * {@link #usingComparatorForElementFieldsWithNames(Comparator, String...) usingComparatorForElementFieldsWithNames}1800 * and {@link #usingComparatorForElementFieldsWithType(Comparator, Class) usingComparatorForElementFieldsWithType}.1801 * <p>1802 * The objects to compare can be of different types but must have the same properties/fields. For example if actual1803 * object has a {@code name} String field, the other object must also have one.1804 * <p>1805 * If an object has a field and a property with the same name, the property value will be used over the field.1806 * <p>1807 * Example:1808 * <pre><code class='java'> TolkienCharacter frodo = new TolkienCharacter("Frodo", 33, HOBBIT);1809 * TolkienCharacter pippin = new TolkienCharacter("Pippin", 28, HOBBIT);1810 * frodo.setFriend(pippin);1811 * pippin.setFriend(frodo);1812 *1813 * TolkienCharacter frodoClone = new TolkienCharacter("Frodo", 33, HOBBIT);1814 * TolkienCharacter pippinClone = new TolkienCharacter("Pippin", 28, HOBBIT);1815 * frodoClone.setFriend(pippinClone);1816 * pippinClone.setFriend(frodoClone);1817 *1818 * List&lt;TolkienCharacter&gt; hobbits = Arrays.asList(frodo, pippin);1819 *1820 * // fails if equals has not been overridden in TolkienCharacter as it would compares object references1821 * assertThat(hobbits).contains(frodoClone, pippinClone);1822 *1823 * // frodo/frodoClone and pippin/pippinClone are equals when doing a recursive property/field by property/field comparison1824 * assertThat(hobbits).usingRecursiveFieldByFieldElementComparator()1825 * .contains(frodoClone, pippinClone);</code>1826 * </pre>1827 *1828 * @return {@code this} assertion object.1829 * @since 2.5.0 / 3.5.01830 */1831 @CheckReturnValue1832 public SELF usingRecursiveFieldByFieldElementComparator() {1833 return usingExtendedByTypesElementComparator(new RecursiveFieldByFieldComparator(comparatorsForElementPropertyOrFieldNames,1834 getComparatorsForElementPropertyOrFieldTypes()));1835 }1836 /**1837 * 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).1838 * <p>1839 * The given {@link RecursiveComparisonConfiguration} is used to tweak the comparison behavior, for example by {@link RecursiveComparisonConfiguration#ignoreCollectionOrder(boolean) ignoring collection order}.1840 * <p>1841 * <b>Warning:</b> the comparison won't use any comparators set with:1842 * <ul>1843 * <li>{@link #usingComparatorForType(Comparator, Class)}</li>1844 * <li>{@link #withTypeComparators(TypeComparators)}</li>1845 * <li>{@link #usingComparatorForElementFieldsWithType(Comparator, Class)}</li>1846 * <li>{@link #withComparatorsForElementPropertyOrFieldTypes(TypeComparators)}</li>1847 * <li>{@link #usingComparatorForElementFieldsWithNames(Comparator, String...)}</li>1848 * <li>{@link #withComparatorsForElementPropertyOrFieldNames(Map)}</li>1849 * </ul>1850 * <p>1851 * These features (and many more) are provided through {@link RecursiveComparisonConfiguration} with:1852 * <ul>1853 * <li>{@link RecursiveComparisonConfiguration#registerComparatorForType(Comparator, Class) registerComparatorForType(Comparator, Class)} / {@link RecursiveComparisonConfiguration.Builder#withComparatorForType(Comparator, Class) withComparatorForType(Comparator, Class)} (using {@link RecursiveComparisonConfiguration.Builder})</li>1854 * <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>1855 * <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>1856 * </ul>1857 * <p>1858 * RecursiveComparisonConfiguration exposes a {@link RecursiveComparisonConfiguration.Builder builder} to ease setting the comparison behaviour,1859 * call {@link RecursiveComparisonConfiguration#builder() RecursiveComparisonConfiguration.builder()} to start building your configuration.1860 * <p>1861 * There are differences between this approach and {@link #usingRecursiveComparison()}:1862 * <ul>1863 * <li>contrary to {@link RecursiveComparisonAssert}, you can chain any iterable assertions after this method.</li>1864 * <li>no comparators registered with {@link AbstractIterableAssert#usingComparatorForType(Comparator, Class)} will be used, you need to register them in the configuration object.</li>1865 * <li>the assertion errors won't be as detailed as {@link RecursiveComparisonAssert#isEqualTo(Object)} which shows the field differences.</li>1866 * </ul>1867 * <p>1868 * 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.1869 * <p>1870 * Example:1871 * <pre><code class='java'> public class Person {1872 * String name;1873 * boolean hasPhd;1874 * }1875 *1876 * public class Doctor {1877 * String name;1878 * boolean hasPhd;1879 * }1880 *1881 * Doctor drSheldon = new Doctor("Sheldon Cooper", true);1882 * Doctor drLeonard = new Doctor("Leonard Hofstadter", true);1883 * Doctor drRaj = new Doctor("Raj Koothrappali", true);1884 *1885 * Person sheldon = new Person("Sheldon Cooper", false);1886 * Person leonard = new Person("Leonard Hofstadter", false);1887 * Person raj = new Person("Raj Koothrappali", false);1888 * Person howard = new Person("Howard Wolowitz", false);1889 *1890 * List&lt;Doctor&gt; doctors = list(drSheldon, drLeonard, drRaj);1891 * List&lt;Person&gt; people = list(sheldon, leonard, raj);1892 *1893 * RecursiveComparisonConfiguration configuration = RecursiveComparisonConfiguration.builder()1894 * .withIgnoredFields​("hasPhd");1895 *1896 * // assertion succeeds as both lists contains equivalent items in order.1897 * assertThat(doctors).usingRecursiveFieldByFieldElementComparator(configuration)1898 * .contains(sheldon);1899 *1900 * // assertion fails because leonard names are different.1901 * leonard.setName("Leonard Ofstater");1902 * assertThat(doctors).usingRecursiveFieldByFieldElementComparator(configuration)1903 * .contains(leonard);1904 *1905 * // assertion fails because howard is missing and leonard is not expected.1906 * people = list(howard, sheldon, raj)1907 * assertThat(doctors).usingRecursiveFieldByFieldElementComparator(configuration)1908 * .contains(howard);</code></pre>1909 *1910 * 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>.1911 * <p>1912 * The default recursive comparison behavior is {@link RecursiveComparisonConfiguration configured} as follows:1913 * <ul>1914 * <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>1915 * This behavior can be turned off by calling {@link RecursiveComparisonAssert#withStrictTypeChecking() withStrictTypeChecking}.</li>1916 * <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>1917 * <li>the following types are compared with these comparators:1918 * <ul>1919 * <li>{@code java.lang.Double}: {@code DoubleComparator} with precision of 1.0E-15</li>1920 * <li>{@code java.lang.Float}: {@code FloatComparator }with precision of 1.0E-6</li>1921 * </ul>1922 * </li>1923 * </ul>1924 * <p>1925 * 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>1926 * 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,1927 * use the more fine grained {@link RecursiveComparisonAssert#ignoringCollectionOrderInFields(String...) ignoringCollectionOrderInFields} or1928 * {@link RecursiveComparisonAssert#ignoringCollectionOrderInFieldsMatchingRegexes(String...) ignoringCollectionOrderInFieldsMatchingRegexes}.1929 *1930 * @param configuration the recursive comparison configuration.1931 *1932 * @return {@code this} assertion object.1933 * @since 3.17.01934 * @see RecursiveComparisonConfiguration1935 */1936 public SELF usingRecursiveFieldByFieldElementComparator(RecursiveComparisonConfiguration configuration) {1937 return usingElementComparator(new ConfigurableRecursiveFieldByFieldComparator(configuration));1938 }1939 /**1940 * Enable using a recursive field by field comparison strategy when calling the chained {@link RecursiveComparisonAssert},1941 * <p>1942 * There are differences between this approach and {@link #usingRecursiveFieldByFieldElementComparator(RecursiveComparisonConfiguration)}:1943 * <ul>1944 * <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>1945 * <li>{@link RecursiveComparisonAssert#isEqualTo(Object) isEqualTo} assertion error will report all field differences (very detailed).</li>1946 * <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>1947 * </ul>1948 * <p>1949 * If you need to chain iterable assertions using recursive comparisons call {@link #usingRecursiveFieldByFieldElementComparator(RecursiveComparisonConfiguration)} instead.1950 * <p>1951 * Example:1952 * <pre><code class='java'> public class Person {1953 * String name;1954 * boolean hasPhd;1955 * }1956 *1957 * public class Doctor {1958 * String name;1959 * boolean hasPhd;1960 * }1961 *1962 * Doctor drSheldon = new Doctor("Sheldon Cooper", true);1963 * Doctor drLeonard = new Doctor("Leonard Hofstadter", true);1964 * Doctor drRaj = new Doctor("Raj Koothrappali", true);1965 *1966 * Person sheldon = new Person("Sheldon Cooper", true);1967 * Person leonard = new Person("Leonard Hofstadter", true);1968 * Person raj = new Person("Raj Koothrappali", true);1969 * Person howard = new Person("Howard Wolowitz", false);1970 *1971 * List&lt;Doctor&gt; doctors = Arrays.asList(drSheldon, drLeonard, drRaj);1972 * List&lt;Person&gt; people = Arrays.asList(sheldon, leonard, raj);1973 *1974 * // assertion succeeds as both lists contains equivalent items in order.1975 * assertThat(doctors).usingRecursiveComparison()1976 * .isEqualTo(people);1977 *1978 * // assertion fails because leonard names are different.1979 * leonard.setName("Leonard Ofstater");1980 * assertThat(doctors).usingRecursiveComparison()1981 * .isEqualTo(people);1982 *1983 * // assertion fails because howard is missing and leonard is not expected.1984 * people = Arrays.asList(howard, sheldon, raj)1985 * assertThat(doctors).usingRecursiveComparison()1986 * .isEqualTo(people);</code></pre>1987 *1988 * 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>.1989 * <p>1990 * The default recursive comparison behavior is {@link RecursiveComparisonConfiguration configured} as follows:1991 * <ul>1992 * <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>1993 * This behavior can be turned off by calling {@link RecursiveComparisonAssert#withStrictTypeChecking() withStrictTypeChecking}.</li>1994 * <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>1995 * <li>the following types are compared with these comparators:1996 * <ul>1997 * <li>{@code java.lang.Double}: {@code DoubleComparator} with precision of 1.0E-15</li>1998 * <li>{@code java.lang.Float}: {@code FloatComparator }with precision of 1.0E-6</li>1999 * </ul>2000 * </li>2001 * </ul>2002 * <p>2003 * 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>2004 * 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,2005 * use the more fine grained {@link RecursiveComparisonAssert#ignoringCollectionOrderInFields(String...) ignoringCollectionOrderInFields} or2006 * {@link RecursiveComparisonAssert#ignoringCollectionOrderInFieldsMatchingRegexes(String...) ignoringCollectionOrderInFieldsMatchingRegexes}.2007 * <p>2008 * At the moment, only `isEqualTo` can be chained after this method but there are plans to provide assertions.2009 *2010 * @return a new {@link RecursiveComparisonAssert} instance2011 * @see RecursiveComparisonConfiguration RecursiveComparisonConfiguration2012 */2013 @Override2014 @Beta2015 public RecursiveComparisonAssert<?> usingRecursiveComparison() {2016 // overridden for javadoc and to make this method public2017 return super.usingRecursiveComparison();2018 }2019 /**2020 * Same as {@link #usingRecursiveComparison()} but allows to specify your own {@link RecursiveComparisonConfiguration}.2021 * <p>2022 * Another difference is that any comparators previously registered with {@link AbstractIterableAssert#usingComparatorForType(Comparator, Class)} will be used in the comparison.2023 *2024 * @param recursiveComparisonConfiguration the {@link RecursiveComparisonConfiguration} used in the chained {@link RecursiveComparisonAssert#isEqualTo(Object) isEqualTo} assertion.2025 *2026 * @return a new {@link RecursiveComparisonAssert} instance built with the given {@link RecursiveComparisonConfiguration}.2027 */2028 @Override2029 public RecursiveComparisonAssert<?> usingRecursiveComparison(RecursiveComparisonConfiguration recursiveComparisonConfiguration) {2030 return super.usingRecursiveComparison(recursiveComparisonConfiguration).withTypeComparators(comparatorsByType);2031 }2032 /**2033 * Use field/property by field/property comparison on the <b>given fields/properties only</b> (including inherited2034 * fields/properties) instead of relying on actual type A <code>equals</code> method to compare group elements for2035 * incoming assertion checks. Private fields are included but this can be disabled using2036 * {@link Assertions#setAllowExtractingPrivateFields(boolean)}.2037 * <p>2038 * This can be handy if <code>equals</code> method of the objects to compare does not suit you.2039 * <p>2040 * You can specify a custom comparator per name or type of element field with2041 * {@link #usingComparatorForElementFieldsWithNames(Comparator, String...)}2042 * and {@link #usingComparatorForElementFieldsWithType(Comparator, Class)}.2043 * <p>2044 * Note that the comparison is <b>not</b> recursive, if one of the fields/properties is an Object, it will be compared2045 * to the other field/property using its <code>equals</code> method.2046 * </p>2047 * Example:2048 * <pre><code class='java'> TolkienCharacter frodo = new TolkienCharacter("Frodo", 33, HOBBIT);2049 * TolkienCharacter sam = new TolkienCharacter("Sam", 38, HOBBIT);2050 *2051 * // frodo and sam both are hobbits, so they are equals when comparing only race2052 * assertThat(newArrayList(frodo)).usingElementComparatorOnFields("race").contains(sam); // OK2053 *2054 * // ... but not when comparing both name and race2055 * assertThat(newArrayList(frodo)).usingElementComparatorOnFields("name", "race").contains(sam); // FAIL</code></pre>2056 *2057 * @param fields the fields/properties to compare using element comparators2058 * @return {@code this} assertion object.2059 */2060 @CheckReturnValue2061 public SELF usingElementComparatorOnFields(String... fields) {2062 return usingExtendedByTypesElementComparator(new OnFieldsComparator(comparatorsForElementPropertyOrFieldNames,2063 getComparatorsForElementPropertyOrFieldTypes(),2064 fields));2065 }2066 protected SELF usingComparisonStrategy(ComparisonStrategy comparisonStrategy) {2067 iterables = new Iterables(comparisonStrategy);2068 return myself;2069 }2070 /**2071 * Use field/property by field/property comparison on all fields/properties <b>except</b> the given ones (including inherited2072 * fields/properties) instead of relying on actual type A <code>equals</code> method to compare group elements for2073 * incoming assertion checks. Private fields are included but this can be disabled using2074 * {@link Assertions#setAllowExtractingPrivateFields(boolean)}.2075 * <p>2076 * This can be handy if <code>equals</code> method of the objects to compare does not suit you.2077 * <p>2078 * You can specify a custom comparator per name or type of element field with2079 * {@link #usingComparatorForElementFieldsWithNames(Comparator, String...)}2080 * and {@link #usingComparatorForElementFieldsWithType(Comparator, Class)}.2081 * <p>2082 * Note that the comparison is <b>not</b> recursive, if one of the fields/properties is an Object, it will be compared2083 * to the other field/property using its <code>equals</code> method.2084 * </p>2085 * Example:2086 * <pre><code class='java'> TolkienCharacter frodo = new TolkienCharacter("Frodo", 33, HOBBIT);2087 * TolkienCharacter sam = new TolkienCharacter("Sam", 38, HOBBIT);2088 *2089 * // frodo and sam both are hobbits, so they are equals when comparing only race (i.e. ignoring all other fields)2090 * assertThat(newArrayList(frodo)).usingElementComparatorIgnoringFields("name", "age").contains(sam); // OK2091 *2092 * // ... but not when comparing both name and race2093 * assertThat(newArrayList(frodo)).usingElementComparatorIgnoringFields("age").contains(sam); // FAIL</code></pre>2094 *2095 * @param fields the fields/properties to compare using element comparators2096 * @return {@code this} assertion object.2097 */2098 @CheckReturnValue2099 public SELF usingElementComparatorIgnoringFields(String... fields) {2100 return usingExtendedByTypesElementComparator(new IgnoringFieldsComparator(comparatorsForElementPropertyOrFieldNames,2101 getComparatorsForElementPropertyOrFieldTypes(),2102 fields));2103 }2104 /**2105 * Enable hexadecimal representation of Iterable elements instead of standard representation in error messages.2106 * <p>2107 * It can be useful to better understand what the error was with a more meaningful error message.2108 * <p>2109 * Example2110 * <pre><code class='java'> final List&lt;Byte&gt; bytes = newArrayList((byte) 0x10, (byte) 0x20);</code></pre>2111 *2112 * With standard error message:2113 * <pre><code class='java'> assertThat(bytes).contains((byte) 0x30);2114 *2115 * Expecting:2116 * &lt;[16, 32]&gt;2117 * to contain:2118 * &lt;[48]&gt;2119 * but could not find:2120 * &lt;[48]&gt;</code></pre>2121 *2122 * With Hexadecimal error message:2123 * <pre><code class='java'> assertThat(bytes).inHexadecimal().contains((byte) 0x30);2124 *2125 * Expecting:2126 * &lt;[0x10, 0x20]&gt;2127 * to contain:2128 * &lt;[0x30]&gt;2129 * but could not find:2130 * &lt;[0x30]&gt;</code></pre>2131 *2132 * @return {@code this} assertion object.2133 */2134 @Override2135 @CheckReturnValue2136 public SELF inHexadecimal() {2137 return super.inHexadecimal();2138 }2139 /**2140 * Enable binary representation of Iterable elements instead of standard representation in error messages.2141 * <p>2142 * Example:2143 * <pre><code class='java'> final List&lt;Byte&gt; bytes = newArrayList((byte) 0x10, (byte) 0x20);</code></pre>2144 *2145 * With standard error message:2146 * <pre><code class='java'> assertThat(bytes).contains((byte) 0x30);2147 *2148 * Expecting:2149 * &lt;[16, 32]&gt;2150 * to contain:2151 * &lt;[48]&gt;2152 * but could not find:2153 * &lt;[48]&gt;</code></pre>2154 *2155 * With binary error message:2156 * <pre><code class='java'> assertThat(bytes).inBinary().contains((byte) 0x30);2157 *2158 * Expecting:2159 * &lt;[0b00010000, 0b00100000]&gt;2160 * to contain:2161 * &lt;[0b00110000]&gt;2162 * but could not find:2163 * &lt;[0b00110000]&gt;</code></pre>2164 *2165 * @return {@code this} assertion object.2166 */2167 @Override2168 @CheckReturnValue2169 public SELF inBinary() {2170 return super.inBinary();2171 }2172 /**2173 * Filters the iterable under test keeping only elements having a property or field equal to {@code expectedValue}, the2174 * property/field is specified by {@code propertyOrFieldName} parameter.2175 * <p>2176 * The filter first tries to get the value from a property (named {@code propertyOrFieldName}), if no such property2177 * exists it tries to read the value from a field. Reading private fields is supported by default, this can be2178 * globally disabled by calling {@link Assertions#setAllowExtractingPrivateFields(boolean)2179 * Assertions.setAllowExtractingPrivateFields(false)}.2180 * <p>2181 * When reading <b>nested</b> property/field, if an intermediate value is null the whole nested property/field is2182 * considered to be null, thus reading "address.street.name" value will return null if "street" value is null.2183 * <p>2184 *2185 * As an example, let's check all employees 800 years old (yes, special employees):2186 * <pre><code class='java'> Employee yoda = new Employee(1L, new Name("Yoda"), 800);2187 * Employee obiwan = new Employee(2L, new Name("Obiwan"), 800);2188 * Employee luke = new Employee(3L, new Name("Luke", "Skywalker"), 26);2189 * Employee noname = new Employee(4L, null, 50);2190 *2191 * List&lt;Employee&gt; employees = newArrayList(yoda, luke, obiwan, noname);2192 *2193 * assertThat(employees).filteredOn("age", 800)2194 * .containsOnly(yoda, obiwan);</code></pre>2195 *2196 * Nested properties/fields are supported:2197 * <pre><code class='java'> // Name is bean class with 'first' and 'last' String properties2198 *2199 * // name is null for noname =&gt; it does not match the filter on "name.first"2200 * assertThat(employees).filteredOn("name.first", "Luke")2201 * .containsOnly(luke);2202 *2203 * assertThat(employees).filteredOn("name.last", "Vader")2204 * .isEmpty();</code></pre>2205 * <p>2206 * If you want to filter on null value, use {@link #filteredOnNull(String)} as Java will resolve the call to2207 * {@link #filteredOn(String, FilterOperator)} instead of this method.2208 * <p>2209 * An {@link IntrospectionError} is thrown if the given propertyOrFieldName can't be found in one of the iterable2210 * elements.2211 * <p>2212 * You can chain filters:2213 * <pre><code class='java'> // fellowshipOfTheRing is a list of TolkienCharacter having race and name fields2214 * // 'not' filter is statically imported from Assertions.not2215 *2216 * assertThat(fellowshipOfTheRing).filteredOn("race.name", "Man")2217 * .filteredOn("name", not("Boromir"))2218 * .containsOnly(aragorn);</code></pre>2219 *2220 * If you need more complex filter, use {@link #filteredOn(Predicate)} or {@link #filteredOn(Condition)}.2221 *2222 * @param propertyOrFieldName the name of the property or field to read2223 * @param expectedValue the value to compare element's property or field with2224 * @return a new assertion object with the filtered iterable under test2225 * @throws IllegalArgumentException if the given propertyOrFieldName is {@code null} or empty.2226 * @throws IntrospectionError if the given propertyOrFieldName can't be found in one of the iterable elements.2227 */2228 @CheckReturnValue2229 public SELF filteredOn(String propertyOrFieldName, Object expectedValue) {2230 Filters<? extends ELEMENT> filter = filter((Iterable<? extends ELEMENT>) actual);2231 Iterable<? extends ELEMENT> filteredIterable = filter.with(propertyOrFieldName, expectedValue).get();2232 return newAbstractIterableAssert(filteredIterable).withAssertionState(myself);2233 }2234 /**2235 * Filters the iterable under test keeping only elements whose property or field specified by2236 * {@code propertyOrFieldName} is null.2237 * <p>2238 * The filter first tries to get the value from a property (named {@code propertyOrFieldName}), if no such property2239 * exists it tries to read the value from a field. Reading private fields is supported by default, this can be2240 * globally disabled by calling {@link Assertions#setAllowExtractingPrivateFields(boolean)2241 * Assertions.setAllowExtractingPrivateFields(false)}.2242 * <p>2243 * When reading <b>nested</b> property/field, if an intermediate value is null the whole nested property/field is2244 * considered to be null, thus reading "address.street.name" value will return null if "street" value is null.2245 * <p>2246 * As an example, let's check all employees 800 years old (yes, special employees):2247 * <pre><code class='java'> Employee yoda = new Employee(1L, new Name("Yoda"), 800);2248 * Employee obiwan = new Employee(2L, new Name("Obiwan"), 800);2249 * Employee luke = new Employee(3L, new Name("Luke", "Skywalker"), 26);2250 * Employee noname = new Employee(4L, null, 50);2251 *2252 * List&lt;Employee&gt; employees = newArrayList(yoda, luke, obiwan, noname);2253 *2254 * assertThat(employees).filteredOnNull("name")2255 * .containsOnly(noname);</code></pre>2256 *2257 * Nested properties/fields are supported:2258 * <pre><code class='java'> // Name is bean class with 'first' and 'last' String properties2259 *2260 * assertThat(employees).filteredOnNull("name.last")2261 * .containsOnly(yoda, obiwan, noname);</code></pre>2262 *2263 * An {@link IntrospectionError} is thrown if the given propertyOrFieldName can't be found in one of the iterable2264 * elements.2265 * <p>2266 * If you need more complex filter, use {@link #filteredOn(Predicate)} or {@link #filteredOn(Condition)}.2267 *2268 * @param propertyOrFieldName the name of the property or field to read2269 * @return a new assertion object with the filtered iterable under test2270 * @throws IntrospectionError if the given propertyOrFieldName can't be found in one of the iterable elements.2271 */2272 @CheckReturnValue2273 public SELF filteredOnNull(String propertyOrFieldName) {2274 // can't call filteredOn(String propertyOrFieldName, null) as it does not work with soft assertions proxying2275 // mechanism, it would lead to double proxying which is not handle properly (improvements needed in our proxy mechanism)2276 Filters<? extends ELEMENT> filter = filter((Iterable<? extends ELEMENT>) actual);2277 Iterable<? extends ELEMENT> filteredIterable = filter.with(propertyOrFieldName, null).get();2278 return newAbstractIterableAssert(filteredIterable).withAssertionState(myself);2279 }2280 /**2281 * Filters the iterable under test keeping only elements having a property or field matching the filter expressed with2282 * the {@link FilterOperator}, the property/field is specified by {@code propertyOrFieldName} parameter.2283 * <p>2284 * The existing filters are :2285 * <ul>2286 * <li> {@link Assertions#not(Object) not(Object)}</li>2287 * <li> {@link Assertions#in(Object...) in(Object...)}</li>2288 * <li> {@link Assertions#notIn(Object...) notIn(Object...)}</li>2289 * </ul>2290 * <p>2291 * Whatever filter is applied, it first tries to get the value from a property (named {@code propertyOrFieldName}), if2292 * no such property exists it tries to read the value from a field. Reading private fields is supported by default,2293 * this can be globally disabled by calling {@link Assertions#setAllowExtractingPrivateFields(boolean)2294 * Assertions.setAllowExtractingPrivateFields(false)}.2295 * <p>2296 * When reading <b>nested</b> property/field, if an intermediate value is null the whole nested property/field is2297 * considered to be null, thus reading "address.street.name" value will return null if "street" value is null.2298 * <p>2299 *2300 * As an example, let's check stuff on some special employees :2301 * <pre><code class='java'> Employee yoda = new Employee(1L, new Name("Yoda"), 800);2302 * Employee obiwan = new Employee(2L, new Name("Obiwan"), 800);2303 * Employee luke = new Employee(3L, new Name("Luke", "Skywalker"), 26);2304 *2305 * List&lt;Employee&gt; employees = newArrayList(yoda, luke, obiwan, noname);2306 *2307 * // 'not' filter is statically imported from Assertions.not2308 * assertThat(employees).filteredOn("age", not(800))2309 * .containsOnly(luke);2310 *2311 * // 'in' filter is statically imported from Assertions.in2312 * // Name is bean class with 'first' and 'last' String properties2313 * assertThat(employees).filteredOn("name.first", in("Yoda", "Luke"))2314 * .containsOnly(yoda, luke);2315 *2316 * // 'notIn' filter is statically imported from Assertions.notIn2317 * assertThat(employees).filteredOn("name.first", notIn("Yoda", "Luke"))2318 * .containsOnly(obiwan);</code></pre>2319 *2320 * An {@link IntrospectionError} is thrown if the given propertyOrFieldName can't be found in one of the iterable2321 * elements.2322 * <p>2323 * Note that combining filter operators is not supported, thus the following code is not correct:2324 * <pre><code class='java'> // Combining filter operators like not(in(800)) is NOT supported2325 * // -&gt; throws UnsupportedOperationException2326 * assertThat(employees).filteredOn("age", not(in(800)))2327 * .contains(luke);</code></pre>2328 * <p>2329 * You can chain filters:2330 * <pre><code class='java'> // fellowshipOfTheRing is a list of TolkienCharacter having race and name fields2331 * // 'not' filter is statically imported from Assertions.not2332 *2333 * assertThat(fellowshipOfTheRing).filteredOn("race.name", "Man")2334 * .filteredOn("name", not("Boromir"))2335 * .containsOnly(aragorn);</code></pre>2336 *2337 * If you need more complex filter, use {@link #filteredOn(Predicate)} or {@link #filteredOn(Condition)}.2338 *2339 * @param propertyOrFieldName the name of the property or field to read2340 * @param filterOperator the filter operator to apply2341 * @return a new assertion object with the filtered iterable under test2342 * @throws IllegalArgumentException if the given propertyOrFieldName is {@code null} or empty.2343 */2344 @CheckReturnValue2345 public SELF filteredOn(String propertyOrFieldName, FilterOperator<?> filterOperator) {2346 checkNotNull(filterOperator);2347 Filters<? extends ELEMENT> filter = filter((Iterable<? extends ELEMENT>) actual).with(propertyOrFieldName);2348 filterOperator.applyOn(filter);2349 return newAbstractIterableAssert(filter.get()).withAssertionState(myself);2350 }2351 /**2352 * Filters the iterable under test keeping only elements matching the given {@link Condition}.2353 * <p>2354 * If you prefer {@link Predicate} over {@link Condition}, use {@link #filteredOn(Predicate)}.2355 * <p>2356 * Example : check old employees whose age &gt; 100:2357 * <pre><code class='java'> Employee yoda = new Employee(1L, new Name("Yoda"), 800);2358 * Employee obiwan = new Employee(2L, new Name("Obiwan"), 800);2359 * Employee luke = new Employee(3L, new Name("Luke", "Skywalker"), 26);2360 * Employee noname = new Employee(4L, null, 50);2361 *2362 * List&lt;Employee&gt; employees = newArrayList(yoda, luke, obiwan, noname);2363 *2364 * // old employee condition, "old employees" describes the condition in error message2365 * // you just have to implement 'matches' method2366 * Condition&lt;Employee&gt; oldEmployees = new Condition&lt;Employee&gt;("old employees") {2367 * {@literal @}Override2368 * public boolean matches(Employee employee) {2369 * return employee.getAge() &gt; 100;2370 * }2371 * };2372 * }2373 * assertThat(employees).filteredOn(oldEmployees)2374 * .containsOnly(yoda, obiwan);</code></pre>2375 *2376 * You can combine {@link Condition} with condition operator like {@link Not}:2377 * <pre><code class='java'> // 'not' filter is statically imported from Assertions.not2378 * assertThat(employees).filteredOn(not(oldEmployees))2379 * .contains(luke, noname);</code></pre>2380 *2381 * @param condition the filter condition / predicate2382 * @return a new assertion object with the filtered iterable under test2383 * @throws IllegalArgumentException if the given condition is {@code null}.2384 */2385 @CheckReturnValue2386 public SELF filteredOn(Condition<? super ELEMENT> condition) {2387 Filters<? extends ELEMENT> filter = filter((Iterable<? extends ELEMENT>) actual);2388 Iterable<? extends ELEMENT> filteredIterable = filter.being(condition).get();2389 return newAbstractIterableAssert(filteredIterable).withAssertionState(myself);2390 }2391 /**2392 * Filters the iterable under test keeping only elements for which the result of the {@code function} is equal to {@code expectedValue}.2393 * <p>2394 * It allows to filter elements more safely than by using {@link #filteredOn(String, Object)} as it doesn't utilize introspection.2395 * <p>2396 * As an example, let's check all employees 800 years old (yes, special employees):2397 * <pre><code class='java'> Employee yoda = new Employee(1L, new Name("Yoda"), 800);2398 * Employee obiwan = new Employee(2L, new Name("Obiwan"), 800);2399 * Employee luke = new Employee(3L, new Name("Luke", "Skywalker"), 26);2400 * Employee noname = new Employee(4L, null, 50);2401 *2402 * List&lt;Employee&gt; employees = newArrayList(yoda, luke, obiwan, noname);2403 *2404 * assertThat(employees).filteredOn(Employee::getAge, 800)2405 * .containsOnly(yoda, obiwan);2406 *2407 * assertThat(employees).filteredOn(e -&gt; e.getName(), null)2408 * .containsOnly(noname);</code></pre>2409 *2410 * If you need more complex filter, use {@link #filteredOn(Predicate)} or {@link #filteredOn(Condition)}.2411 *2412 * @param <T> result type of the filter function2413 * @param function the filter function2414 * @param expectedValue the expected value of the filter function2415 * @return a new assertion object with the filtered iterable under test2416 * @throws IllegalArgumentException if the given function is {@code null}.2417 * @since 3.17.02418 */2419 @CheckReturnValue2420 public <T> SELF filteredOn(Function<? super ELEMENT, T> function, T expectedValue) {2421 checkArgument(function != null, "The filter function should not be null");2422 // call internalFilteredOn to avoid double proxying in soft assertions2423 return internalFilteredOn(element -> java.util.Objects.equals(function.apply(element), expectedValue));2424 }2425 /**2426 * Filters the iterable under test keeping only elements matching the given assertions specified with a {@link Consumer}.2427 * <p>2428 * Example : check young hobbits whose age &lt; 34:2429 *2430 * <pre><code class='java'> TolkienCharacter pippin = new TolkienCharacter("Pippin", 28, HOBBIT);2431 * TolkienCharacter frodo = new TolkienCharacter("Frodo", 33, HOBBIT);2432 * TolkienCharacter merry = new TolkienCharacter("Merry", 36, HOBBIT);2433 * TolkienCharacter sam = new TolkienCharacter("Sam", 38, HOBBIT);2434 *2435 * List&lt;TolkienCharacter&gt; hobbits = list(frodo, sam, merry, pippin);2436 *2437 * assertThat(hobbits).filteredOnAssertions(hobbit -&gt; assertThat(hobbit.age).isLessThan(34))2438 * .containsOnly(frodo, pippin);</code></pre>2439 *2440 * @param elementAssertions containing AssertJ assertions to filter on2441 * @return a new assertion object with the filtered iterable under test2442 * @throws IllegalArgumentException if the given predicate is {@code null}.2443 * @since 3.11.02444 */2445 public SELF filteredOnAssertions(Consumer<? super ELEMENT> elementAssertions) {2446 checkArgument(elementAssertions != null, "The element assertions should not be null");2447 List<? extends ELEMENT> filteredIterable = stream(actual.spliterator(), false).filter(byPassingAssertions(elementAssertions))2448 .collect(toList());2449 return newAbstractIterableAssert(filteredIterable).withAssertionState(myself);2450 }2451 // navigable assertions2452 /**2453 * Navigate and allow to perform assertions on the first element of the {@link Iterable} under test.2454 * <p>2455 * By default available assertions after {@code first()} are {@code Object} assertions, it is possible though to2456 * get more specific assertions if you create {@code IterableAssert} with either:2457 * <ul>2458 * <li>the element assert class, see: {@link Assertions#assertThat(Iterable, Class) assertThat(Iterable, element assert class)}</li>2459 * <li>an assert factory used that knows how to create elements assertion, see: {@link Assertions#assertThat(Iterable, AssertFactory) assertThat(Iterable, element assert factory)}</li>2460 * </ul>2461 * <p>2462 * Example: default {@code Object} assertions2463 * <pre><code class='java'> // default iterable assert =&gt; element assert is ObjectAssert2464 * Iterable&lt;TolkienCharacter&gt; hobbits = newArrayList(frodo, sam, pippin);2465 *2466 * // assertion succeeds, only Object assertions are available after first()2467 * assertThat(hobbits).first()2468 * .isEqualTo(frodo);2469 *2470 * // assertion fails2471 * assertThat(hobbits).first()2472 * .isEqualTo(pippin);</code></pre>2473 * <p>2474 * If you have created the Iterable assertion using an {@link AssertFactory} or the element assert class,2475 * you will be able to chain {@code first()} with more specific typed assertion.2476 * <p>2477 * Example: use of {@code String} assertions after {@code first()}2478 * <pre><code class='java'> Iterable&lt;String&gt; hobbits = newArrayList("Frodo", "Sam", "Pippin");2479 *2480 * // assertion succeeds2481 * // String assertions are available after first()2482 * assertThat(hobbits, StringAssert.class).first()2483 * .startsWith("Fro")2484 * .endsWith("do");2485 * // assertion fails2486 * assertThat(hobbits, StringAssert.class).first()2487 * .startsWith("Pip");</code></pre>2488 *2489 * @return the assertion on the first element2490 * @throws AssertionError if the actual {@link Iterable} is empty.2491 * @since 2.5.0 / 3.5.02492 * @see #first(InstanceOfAssertFactory)2493 */2494 @CheckReturnValue2495 public ELEMENT_ASSERT first() {2496 return internalFirst();2497 }2498 /**2499 * Navigate and allow to perform assertions on the first element of the {@link Iterable} under test.2500 * <p>2501 * The {@code assertFactory} parameter allows to specify an {@link InstanceOfAssertFactory}, which is used to get the2502 * assertions narrowed to the factory type.2503 * <p>2504 * Example: use of {@code String} assertions after {@code first(as(InstanceOfAssertFactories.STRING)}2505 * <pre><code class='java'> Iterable&lt;String&gt; hobbits = newArrayList("Frodo", "Sam", "Pippin");2506 *2507 * // assertion succeeds2508 * assertThat(hobbits).first(as(InstanceOfAssertFactories.STRING))2509 * .startsWith("Fro")2510 * .endsWith("do");2511 * // assertion fails2512 * assertThat(hobbits).first(as(InstanceOfAssertFactories.STRING))2513 * .startsWith("Pip");2514 * // assertion fails because of wrong factory type2515 * assertThat(hobbits).first(as(InstanceOfAssertFactories.INTEGER))2516 * .isZero();</code></pre>2517 *2518 * @param <ASSERT> the type of the resulting {@code Assert}2519 * @param assertFactory the factory which verifies the type and creates the new {@code Assert}2520 * @return a new narrowed {@link Assert} instance for assertions chaining on the first element2521 * @throws AssertionError if the actual {@link Iterable} is empty.2522 * @throws NullPointerException if the given factory is {@code null}2523 * @since 3.14.02524 */2525 @CheckReturnValue2526 public <ASSERT extends AbstractAssert<?, ?>> ASSERT first(InstanceOfAssertFactory<?, ASSERT> assertFactory) {2527 return internalFirst().asInstanceOf(assertFactory);2528 }2529 private ELEMENT_ASSERT internalFirst() {2530 isNotEmpty();2531 return toAssert(actual.iterator().next(), navigationDescription("check first element"));2532 }2533 /**2534 * Navigate and allow to perform assertions on the last element of the {@link Iterable} under test.2535 * <p>2536 * By default available assertions after {@code last()} are {@code Object} assertions, it is possible though to2537 * get more specific assertions if you create {@code IterableAssert} with either:2538 * <ul>2539 * <li>the element assert class, see: {@link Assertions#assertThat(Iterable, Class) assertThat(Iterable, element assert class)}</li>2540 * <li>an assert factory used that knows how to create elements assertion, see: {@link Assertions#assertThat(Iterable, AssertFactory) assertThat(Iterable, element assert factory)}</li>2541 * </ul>2542 * <p>2543 * Example: default {@code Object} assertions2544 * <pre><code class='java'> // default iterable assert =&gt; element assert is ObjectAssert2545 * Iterable&lt;TolkienCharacter&gt; hobbits = newArrayList(frodo, sam, pippin);2546 *2547 * // assertion succeeds, only Object assertions are available after last()2548 * assertThat(hobbits).last()2549 * .isEqualTo(pippin);2550 *2551 * // assertion fails2552 * assertThat(hobbits).last()2553 * .isEqualTo(frodo);</code></pre>2554 * <p>2555 * If you have created the Iterable assertion using an {@link AssertFactory} or the element assert class,2556 * you will be able to chain {@code last()} with more specific typed assertion.2557 * <p>2558 * Example: use of {@code String} assertions after {@code last()}2559 * <pre><code class='java'> Iterable&lt;String&gt; hobbits = newArrayList("Frodo", "Sam", "Pippin");2560 *2561 * // assertion succeeds2562 * // String assertions are available after last()2563 * assertThat(hobbits, StringAssert.class).last()2564 * .startsWith("Pi")2565 * .endsWith("in");2566 * // assertion fails2567 * assertThat(hobbits, StringAssert.class).last()2568 * .startsWith("Fro");</code></pre>2569 *2570 * @return the assertion on the last element2571 * @throws AssertionError if the actual {@link Iterable} is empty.2572 * @since 2.5.0 / 3.5.02573 * @see #last(InstanceOfAssertFactory)2574 */2575 @CheckReturnValue2576 public ELEMENT_ASSERT last() {2577 return internalLast();2578 }2579 /**2580 * Navigate and allow to perform assertions on the last element of the {@link Iterable} under test.2581 * <p>2582 * The {@code assertFactory} parameter allows to specify an {@link InstanceOfAssertFactory}, which is used to get the2583 * assertions narrowed to the factory type.2584 * <p>2585 * Example: use of {@code String} assertions after {@code last(as(InstanceOfAssertFactories.STRING)}2586 * <pre><code class='java'> Iterable&lt;String&gt; hobbits = newArrayList("Frodo", "Sam", "Pippin");2587 *2588 * // assertion succeeds2589 * assertThat(hobbits).last(as(InstanceOfAssertFactories.STRING))2590 * .startsWith("Pip")2591 * .endsWith("pin");2592 * // assertion fails2593 * assertThat(hobbits).last(as(InstanceOfAssertFactories.STRING))2594 * .startsWith("Fro");2595 * // assertion fails because of wrong factory type2596 * assertThat(hobbits).last(as(InstanceOfAssertFactories.INTEGER))2597 * .isZero();</code></pre>2598 *2599 * @param <ASSERT> the type of the resulting {@code Assert}2600 * @param assertFactory the factory which verifies the type and creates the new {@code Assert}2601 * @return a new narrowed {@link Assert} instance for assertions chaining on the last element2602 * @throws AssertionError if the actual {@link Iterable} is empty.2603 * @throws NullPointerException if the given factory is {@code null}2604 * @since 3.14.02605 */2606 @CheckReturnValue2607 public <ASSERT extends AbstractAssert<?, ?>> ASSERT last(InstanceOfAssertFactory<?, ASSERT> assertFactory) {2608 return internalLast().asInstanceOf(assertFactory);2609 }2610 private ELEMENT_ASSERT internalLast() {2611 isNotEmpty();2612 return toAssert(lastElement(), navigationDescription("check last element"));2613 }2614 private ELEMENT lastElement() {2615 if (actual instanceof List) {2616 @SuppressWarnings("unchecked")2617 List<? extends ELEMENT> list = (List<? extends ELEMENT>) actual;2618 return list.get(list.size() - 1);2619 }2620 Iterator<? extends ELEMENT> actualIterator = actual.iterator();2621 ELEMENT last = actualIterator.next();2622 while (actualIterator.hasNext()) {2623 last = actualIterator.next();2624 }2625 return last;2626 }2627 /**2628 * Navigate and allow to perform assertions on the chosen element of the {@link Iterable} under test.2629 * <p>2630 * By default available assertions after {@code element(index)} are {@code Object} assertions, it is possible though to2631 * get more specific assertions if you create {@code IterableAssert} with either:2632 * <ul>2633 * <li>the element assert class, see: {@link Assertions#assertThat(Iterable, Class) assertThat(Iterable, element assert class)}</li>2634 * <li>an assert factory used that knows how to create elements assertion, see: {@link Assertions#assertThat(Iterable, AssertFactory) assertThat(Iterable, element assert factory)}</li>2635 * </ul>2636 * <p>2637 * Example: default {@code Object} assertions2638 * <pre><code class='java'> // default iterable assert =&gt; element assert is ObjectAssert2639 * Iterable&lt;TolkienCharacter&gt; hobbits = newArrayList(frodo, sam, pippin);2640 *2641 * // assertion succeeds, only Object assertions are available after element(index)2642 * assertThat(hobbits).element(1)2643 * .isEqualTo(sam);2644 *2645 * // assertion fails2646 * assertThat(hobbits).element(1)2647 * .isEqualTo(pippin);</code></pre>2648 * <p>2649 * If you have created the Iterable assertion using an {@link AssertFactory} or the element assert class,2650 * you will be able to chain {@code element(index)} with more specific typed assertion.2651 * <p>2652 * Example: use of {@code String} assertions after {@code element(index)}2653 * <pre><code class='java'> Iterable&lt;String&gt; hobbits = newArrayList("Frodo", "Sam", "Pippin");2654 *2655 * // assertion succeeds2656 * // String assertions are available after element(index)2657 * assertThat(hobbits, StringAssert.class).element(1)2658 * .startsWith("Sa")2659 * .endsWith("am");2660 * // assertion fails2661 * assertThat(hobbits, StringAssert.class).element(1)2662 * .startsWith("Fro");</code></pre>2663 *2664 * @param index the element's index2665 * @return the assertion on the given element2666 * @throws AssertionError if the given index is out of bound.2667 * @since 2.5.0 / 3.5.02668 * @see #element(int, InstanceOfAssertFactory)2669 */2670 @CheckReturnValue2671 public ELEMENT_ASSERT element(int index) {2672 return internalElement(index);2673 }2674 /**2675 * Navigate and allow to perform assertions on the chosen element of the {@link Iterable} under test.2676 * <p>2677 * The {@code assertFactory} parameter allows to specify an {@link InstanceOfAssertFactory}, which is used to get the2678 * assertions narrowed to the factory type.2679 * <p>2680 * Example: use of {@code String} assertions after {@code element(index, as(InstanceOfAssertFactories.STRING)}2681 * <pre><code class='java'> Iterable&lt;String&gt; hobbits = newArrayList("Frodo", "Sam", "Pippin");2682 *2683 * // assertion succeeds2684 * assertThat(hobbits).element(1, as(InstanceOfAssertFactories.STRING))2685 * .startsWith("Sa")2686 * .endsWith("am");2687 * // assertion fails2688 * assertThat(hobbits).element(1, as(InstanceOfAssertFactories.STRING))2689 * .startsWith("Fro");2690 * // assertion fails because of wrong factory type2691 * assertThat(hobbits).element(1, as(InstanceOfAssertFactories.INTEGER))2692 * .isZero();</code></pre>2693 *2694 * @param <ASSERT> the type of the resulting {@code Assert}2695 * @param index the element's index2696 * @param assertFactory the factory which verifies the type and creates the new {@code Assert}2697 * @return a new narrowed {@link Assert} instance for assertions chaining on the element at the given index2698 * @throws AssertionError if the given index is out of bound.2699 * @throws NullPointerException if the given factory is {@code null}2700 * @since 3.14.02701 */2702 @CheckReturnValue2703 public <ASSERT extends AbstractAssert<?, ?>> ASSERT element(int index, InstanceOfAssertFactory<?, ASSERT> assertFactory) {2704 return internalElement(index).asInstanceOf(assertFactory);2705 }2706 private ELEMENT_ASSERT internalElement(int index) {2707 isNotEmpty();2708 assertThat(index).describedAs(navigationDescription("check index validity"))2709 .isBetween(0, IterableUtil.sizeOf(actual) - 1);2710 ELEMENT elementAtIndex;2711 if (actual instanceof List) {2712 @SuppressWarnings("unchecked")2713 List<? extends ELEMENT> list = (List<? extends ELEMENT>) actual;2714 elementAtIndex = list.get(index);2715 } else {2716 Iterator<? extends ELEMENT> actualIterator = actual.iterator();2717 for (int i = 0; i < index; i++) {2718 actualIterator.next();2719 }2720 elementAtIndex = actualIterator.next();2721 }2722 return toAssert(elementAtIndex, navigationDescription("element at index " + index));2723 }2724 /**2725 * Verifies that the {@link Iterable} under test contains a single element and allows to perform assertions that element.2726 * <p>2727 * This is a shorthand for <code>hasSize(1).first()</code>.2728 * <p>2729 * By default available assertions after {@code singleElement()} are {@code Object} assertions, it is possible though to2730 * get more specific assertions if you create {@code IterableAssert} with either:2731 * <ul>2732 * <li>the element assert class, see: {@link Assertions#assertThat(Iterable, Class) assertThat(Iterable, element assert class)}</li>2733 * <li>an assert factory used that knows how to create elements assertion, see: {@link Assertions#assertThat(Iterable, AssertFactory) assertThat(Iterable, element assert factory)}</li>2734 * <li>the general <code>assertThat(Iterable)</code> and narrow down the single element with an assert factory, see: {@link #singleElement(InstanceOfAssertFactory) singleElement(element assert factory)}</li>2735 * </ul>2736 * <p>2737 * Example: default {@code Object} assertions2738 * <pre><code class='java'> List&lt;String&gt; babySimpsons = list("Maggie");2739 *2740 * // assertion succeeds, only Object assertions are available after singleElement()2741 * assertThat(babySimpsons).singleElement()2742 * .isEqualTo("Maggie");2743 *2744 * // assertion fails2745 * assertThat(babySimpsons).singleElement()2746 * .isEqualTo("Homer");2747 *2748 * // assertion fails because list contains no elements2749 * assertThat(emptyList()).singleElement();2750 *2751 *2752 * // assertion fails because list contains more than one element2753 * List&lt;String&gt; simpsons = list("Homer", "Marge", "Lisa", "Bart", "Maggie");2754 * assertThat(simpsons).singleElement();</code></pre>2755 * <p>2756 * If you have created the Iterable assertion using an {@link AssertFactory} or the element assert class,2757 * you will be able to chain {@code singleElement()} with more specific typed assertion.2758 * <p>2759 * Example: use of {@code String} assertions after {@code singleElement()}2760 * <pre><code class='java'> List&lt;String&gt; babySimpsons = list("Maggie");2761 *2762 * // assertion succeeds2763 * // String assertions are available after singleElement()2764 * assertThat(babySimpsons, StringAssert.class).singleElement()2765 * .startsWith("Mag");2766 *2767 * // InstanceOfAssertFactories.STRING is an AssertFactory for String assertions2768 * assertThat(babySimpsons, InstanceOfAssertFactories.STRING).singleElement()2769 * .startsWith("Mag");2770 * // better readability with import static InstanceOfAssertFactories.STRING and Assertions.as2771 * assertThat(babySimpsons, as(STRING)).singleElement()2772 * .startsWith("Mag");2773 *2774 * // assertions fail2775 * assertThat(babySimpsons, StringAssert.class).singleElement()2776 * .startsWith("Lis");2777 * // failure as the single element is not an int/Integer2778 * assertThat(babySimpsons, IntegerAssert.class).singleElement()2779 * .startsWith("Lis");</code></pre>2780 *2781 * @return the assertion on the first element2782 * @throws AssertionError if the actual {@link Iterable} does not contain exactly one element.2783 * @since 3.17.02784 * @see #singleElement(InstanceOfAssertFactory)2785 */2786 @CheckReturnValue2787 public ELEMENT_ASSERT singleElement() {2788 return internalSingleElement();2789 }2790 /**2791 * Verifies that the {@link Iterable} under test contains a single element and allows to perform assertions on that element.<br>2792 * The assertions are strongly typed according to the given {@link AssertFactory} parameter.2793 * <p>2794 * This is a shorthand for <code>hasSize(1).first(assertFactory)</code>.2795 * <p>2796 * Example: use of {@code String} assertions after {@code singleElement(as(STRING)}2797 * <pre><code class='java'> import static org.assertj.core.api.InstanceOfAssertFactories.STRING;2798 * import static org.assertj.core.api.InstanceOfAssertFactories.INTEGER;2799 * import static org.assertj.core.api.Assertions.as; // syntactic sugar2800 *2801 * List&lt;String&gt; babySimpsons = list("Maggie");2802 *2803 * // assertion succeeds2804 * assertThat(babySimpsons).singleElement(as(STRING))2805 * .startsWith("Mag");2806 *2807 * // assertion fails2808 * assertThat(babySimpsons).singleElement(as(STRING))2809 * .startsWith("Lis");2810 *2811 * // assertion fails because of wrong factory type2812 * assertThat(babySimpsons).singleElement(as(INTEGER))2813 * .isZero();2814 *2815 * // assertion fails because list contains no elements2816 * assertThat(emptyList()).singleElement(as(STRING));2817 *2818 * // assertion fails because list contains more than one element2819 * List&lt;String&gt; simpsons = list("Homer", "Marge", "Lisa", "Bart", "Maggie");2820 * assertThat(simpsons).singleElement(as(STRING));</code></pre>2821 *2822 * @param <ASSERT> the type of the resulting {@code Assert}2823 * @param assertFactory the factory which verifies the type and creates the new {@code Assert}2824 * @return a new narrowed {@link Assert} instance for assertions chaining on the single element2825 * @throws AssertionError if the actual {@link Iterable} does not contain exactly one element.2826 * @throws NullPointerException if the given factory is {@code null}.2827 * @since 3.17.02828 */2829 @CheckReturnValue2830 public <ASSERT extends AbstractAssert<?, ?>> ASSERT singleElement(InstanceOfAssertFactory<?, ASSERT> assertFactory) {2831 return internalSingleElement().asInstanceOf(assertFactory);2832 }2833 private ELEMENT_ASSERT internalSingleElement() {2834 iterables.assertHasSize(info, actual, 1);2835 return internalFirst();2836 }2837 protected abstract ELEMENT_ASSERT toAssert(ELEMENT value, String description);2838 protected String navigationDescription(String propertyName) {2839 String text = descriptionText();2840 if (Strings.isNullOrEmpty(text)) {2841 text = removeAssert(this.getClass().getSimpleName());2842 }2843 return text + " " + propertyName;2844 }2845 private static String removeAssert(String text) {2846 return text.endsWith(ASSERT) ? text.substring(0, text.length() - ASSERT.length()) : text;2847 }2848 /**2849 * Filters the iterable under test keeping only elements matching the given {@link Predicate}.2850 * <p>2851 * Example : check old employees whose age &gt; 100:2852 *2853 * <pre><code class='java'> Employee yoda = new Employee(1L, new Name("Yoda"), 800);2854 * Employee obiwan = new Employee(2L, new Name("Obiwan"), 800);2855 * Employee luke = new Employee(3L, new Name("Luke", "Skywalker"), 26);2856 *2857 * List&lt;Employee&gt; employees = newArrayList(yoda, luke, obiwan);2858 *2859 * assertThat(employees).filteredOn(employee -&gt; employee.getAge() &gt; 100)2860 * .containsOnly(yoda, obiwan);</code></pre>2861 *2862 * @param predicate the filter predicate2863 * @return a new assertion object with the filtered iterable under test2864 * @throws IllegalArgumentException if the given predicate is {@code null}.2865 */2866 public SELF filteredOn(Predicate<? super ELEMENT> predicate) {2867 return internalFilteredOn(predicate);2868 }2869 /**2870 * {@inheritDoc}2871 */2872 @Override2873 public SELF allMatch(Predicate<? super ELEMENT> predicate) {2874 iterables.assertAllMatch(info, actual, predicate, PredicateDescription.GIVEN);2875 return myself;2876 }2877 /**2878 * {@inheritDoc}2879 */2880 @Override2881 public SELF allMatch(Predicate<? super ELEMENT> predicate, String predicateDescription) {2882 iterables.assertAllMatch(info, actual, predicate, new PredicateDescription(predicateDescription));2883 return myself;2884 }2885 /**2886 * {@inheritDoc}2887 */2888 @Override2889 public SELF allSatisfy(Consumer<? super ELEMENT> requirements) {2890 iterables.assertAllSatisfy(info, actual, requirements);2891 return myself;2892 }2893 @Override2894 public SELF anyMatch(Predicate<? super ELEMENT> predicate) {2895 iterables.assertAnyMatch(info, actual, predicate, PredicateDescription.GIVEN);2896 return myself;2897 }2898 /**2899 * Verifies that the zipped pairs of actual and other elements, i.e: (actual 1st element, other 1st element), (actual 2nd element, other 2nd element), ...2900 * all satisfy the given {@code zipRequirements}.2901 * <p>2902 * This assertion assumes that actual and other have the same size but they can contain different type of elements2903 * making it handy to compare objects converted to another type, for example Domain and View/DTO objects.2904 * <p>2905 * Example:2906 * <pre><code class='java'> List&lt;Adress&gt; addressModels = findGoodRestaurants();2907 * List&lt;AdressView&gt; addressViews = convertToView(addressModels);2908 *2909 * // compare addressViews and addressModels respective paired elements.2910 * assertThat(addressViews).zipSatisfy(addressModels, (AdressView view, Adress model) -&gt; {2911 * assertThat(view.getZipcode() + ' ' + view.getCity()).isEqualTo(model.getCityLine());2912 * assertThat(view.getStreet()).isEqualTo(model.getStreet().toUpperCase());2913 * });</code></pre>2914 *2915 * @param <OTHER_ELEMENT> the type of the other iterable elements.2916 * @param other the iterable to zip actual with.2917 * @param zipRequirements the given requirements that each pair must satisfy.2918 * @return {@code this} assertion object.2919 * @throws NullPointerException if the given zipRequirements {@link BiConsumer} is {@code null}.2920 * @throws NullPointerException if the other iterable to zip actual with is {@code null}.2921 * @throws AssertionError if the {@code Iterable} under test is {@code null}.2922 * @throws AssertionError if actual and other don't have the same size.2923 * @throws AssertionError if one or more pairs don't satisfy the given requirements.2924 * @since 3.9.02925 */2926 public <OTHER_ELEMENT> SELF zipSatisfy(Iterable<OTHER_ELEMENT> other,2927 BiConsumer<? super ELEMENT, OTHER_ELEMENT> zipRequirements) {2928 iterables.assertZipSatisfy(info, actual, other, zipRequirements);2929 return myself;2930 }2931 /**2932 * {@inheritDoc}2933 */2934 @Override2935 public SELF anySatisfy(Consumer<? super ELEMENT> requirements) {2936 iterables.assertAnySatisfy(info, actual, requirements);2937 return myself;2938 }2939 /**2940 * {@inheritDoc}2941 */2942 @Override2943 public SELF noneSatisfy(Consumer<? super ELEMENT> restrictions) {2944 iterables.assertNoneSatisfy(info, actual, restrictions);2945 return myself;2946 }2947 // override methods to avoid compilation error when chaining an AbstractAssert method with a AbstractIterableAssert2948 // one on raw types.2949 @Override2950 @CheckReturnValue2951 public SELF as(String description, Object... args) {2952 return super.as(description, args);2953 }2954 @Override2955 @CheckReturnValue2956 public SELF as(Description description) {2957 return super.as(description);2958 }2959 @Override2960 @CheckReturnValue2961 public SELF describedAs(Description description) {2962 return super.describedAs(description);2963 }2964 @Override2965 @CheckReturnValue2966 public SELF describedAs(String description, Object... args) {2967 return super.describedAs(description, args);2968 }2969 @Override2970 public SELF doesNotHave(Condition<? super ACTUAL> condition) {2971 return super.doesNotHave(condition);2972 }2973 @Override2974 public SELF doesNotHaveSameClassAs(Object other) {2975 return super.doesNotHaveSameClassAs(other);2976 }2977 @Override2978 public SELF has(Condition<? super ACTUAL> condition) {2979 return super.has(condition);2980 }2981 @Override2982 public SELF hasSameClassAs(Object other) {2983 return super.hasSameClassAs(other);2984 }2985 @Override2986 public SELF hasToString(String expectedToString) {2987 return super.hasToString(expectedToString);2988 }2989 @Override2990 public SELF is(Condition<? super ACTUAL> condition) {2991 return super.is(condition);2992 }2993 @Override2994 public SELF isEqualTo(Object expected) {2995 return super.isEqualTo(expected);2996 }2997 @Override2998 public SELF isExactlyInstanceOf(Class<?> type) {2999 return super.isExactlyInstanceOf(type);3000 }3001 @Override3002 public SELF isIn(Iterable<?> values) {3003 return super.isIn(values);3004 }3005 @Override3006 public SELF isIn(Object... values) {3007 return super.isIn(values);3008 }3009 @Override3010 public SELF isInstanceOf(Class<?> type) {3011 return super.isInstanceOf(type);3012 }3013 @Override3014 public SELF isInstanceOfAny(Class<?>... types) {3015 return super.isInstanceOfAny(types);3016 }3017 @Override3018 public SELF isNot(Condition<? super ACTUAL> condition) {3019 return super.isNot(condition);3020 }3021 @Override3022 public SELF isNotEqualTo(Object other) {3023 return super.isNotEqualTo(other);3024 }3025 @Override3026 public SELF isNotExactlyInstanceOf(Class<?> type) {3027 return super.isNotExactlyInstanceOf(type);3028 }3029 @Override3030 public SELF isNotIn(Iterable<?> values) {3031 return super.isNotIn(values);3032 }3033 @Override3034 public SELF isNotIn(Object... values) {3035 return super.isNotIn(values);3036 }3037 @Override3038 public SELF isNotInstanceOf(Class<?> type) {3039 return super.isNotInstanceOf(type);3040 }3041 @Override3042 public SELF isNotInstanceOfAny(Class<?>... types) {3043 return super.isNotInstanceOfAny(types);3044 }3045 @Override3046 public SELF isNotOfAnyClassIn(Class<?>... types) {3047 return super.isNotOfAnyClassIn(types);3048 }3049 @Override3050 public SELF isNotNull() {3051 return super.isNotNull();3052 }3053 @Override3054 public SELF isNotSameAs(Object other) {3055 return super.isNotSameAs(other);3056 }3057 @Override3058 public SELF isOfAnyClassIn(Class<?>... types) {3059 return super.isOfAnyClassIn(types);3060 }3061 @Override3062 public SELF isSameAs(Object expected) {3063 return super.isSameAs(expected);3064 }3065 @Override3066 public SELF noneMatch(Predicate<? super ELEMENT> predicate) {3067 iterables.assertNoneMatch(info, actual, predicate, PredicateDescription.GIVEN);3068 return myself;3069 }3070 @Override3071 @CheckReturnValue3072 public SELF overridingErrorMessage(String newErrorMessage, Object... args) {3073 return super.overridingErrorMessage(newErrorMessage, args);3074 }3075 @Override3076 @CheckReturnValue3077 public SELF usingDefaultComparator() {3078 return super.usingDefaultComparator();3079 }3080 @Override3081 @CheckReturnValue3082 public SELF usingComparator(Comparator<? super ACTUAL> customComparator) {3083 return usingComparator(customComparator, null);3084 }3085 @Override3086 @CheckReturnValue3087 public SELF usingComparator(Comparator<? super ACTUAL> customComparator, String customComparatorDescription) {3088 return super.usingComparator(customComparator, customComparatorDescription);3089 }3090 @Override3091 @CheckReturnValue3092 public SELF withFailMessage(String newErrorMessage, Object... args) {3093 return super.withFailMessage(newErrorMessage, args);3094 }3095 @Override3096 @CheckReturnValue3097 public SELF withThreadDumpOnError() {3098 return super.withThreadDumpOnError();3099 }3100 /**3101 * Returns an {@code Assert} object that allows performing assertions on the size of the {@link Iterable} under test.3102 * <p>3103 * Once this method is called, the object under test is no longer the {@link Iterable} but its size,3104 * to perform assertions on the {@link Iterable}, call {@link AbstractIterableSizeAssert#returnToIterable()}.3105 * <p>3106 * Example:3107 * <pre><code class='java'> Iterable&lt;Ring&gt; elvesRings = newArrayList(vilya, nenya, narya);3108 *3109 * // assertion will pass:3110 * assertThat(elvesRings).size().isGreaterThan(1)3111 * .isLessThanOrEqualTo(3)3112 * .returnToIterable().contains(narya)3113 * .doesNotContain(oneRing);3114 *3115 * // assertion will fail:3116 * assertThat(elvesRings).size().isGreaterThan(3);</code></pre>3117 *3118 * @return AbstractIterableSizeAssert built with the {@code Iterable}'s size.3119 * @throws NullPointerException if the given {@code Iterable} is {@code null}.3120 */3121 @SuppressWarnings({ "rawtypes", "unchecked" })3122 @CheckReturnValue3123 public AbstractIterableSizeAssert<SELF, ACTUAL, ELEMENT, ELEMENT_ASSERT> size() {3124 requireNonNull(actual, "Can not perform assertions on the size of a null iterable.");3125 return new IterableSizeAssert(this, IterableUtil.sizeOf(actual));3126 }3127 // lazy init TypeComparators3128 protected TypeComparators getComparatorsByType() {3129 if (comparatorsByType == null) comparatorsByType = defaultTypeComparators();3130 return comparatorsByType;3131 }3132 // lazy init TypeComparators3133 protected TypeComparators getComparatorsForElementPropertyOrFieldTypes() {3134 if (comparatorsForElementPropertyOrFieldTypes == null) comparatorsForElementPropertyOrFieldTypes = defaultTypeComparators();3135 return comparatorsForElementPropertyOrFieldTypes;3136 }3137 // use to build the assert instance with a filtered iterable3138 protected abstract SELF newAbstractIterableAssert(Iterable<? extends ELEMENT> iterable);3139 @SuppressWarnings({ "rawtypes", "unchecked" })3140 @Override3141 SELF withAssertionState(AbstractAssert assertInstance) {3142 if (assertInstance instanceof AbstractIterableAssert) {3143 AbstractIterableAssert iterableAssert = (AbstractIterableAssert) assertInstance;3144 return (SELF) super.withAssertionState(assertInstance).withIterables(iterableAssert.iterables)3145 .withTypeComparators(iterableAssert.comparatorsByType)3146 .withComparatorsForElementPropertyOrFieldNames(iterableAssert.comparatorsForElementPropertyOrFieldNames)3147 .withComparatorsForElementPropertyOrFieldTypes(iterableAssert.comparatorsForElementPropertyOrFieldTypes);...

Full Screen

Full Screen

getComparatorsForElementPropertyOrFieldTypes

Using AI Code Generation

copy

Full Screen

1AbstractIterableAssert<?, Iterable<?>, Object, ObjectAssert<Object>> absIterableAssert = null;2absIterableAssert.getComparatorsForElementPropertyOrFieldTypes("elementPropertyOrFieldType");3AbstractIterableAssert<?, Iterable<?>, Object, ObjectAssert<Object>> absIterableAssert = null;4absIterableAssert.getComparatorByType("type");5AbstractIterableAssert<?, Iterable<?>, Object, ObjectAssert<Object>> absIterableAssert = null;6absIterableAssert.getComparatorForElementPropertyOrFieldType("elementPropertyOrFieldType");7AbstractIterableAssert<?, Iterable<?>, Object, ObjectAssert<Object>> absIterableAssert = null;8absIterableAssert.getComparatorForElementPropertyOrFieldType("elementPropertyOrFieldType");9AbstractIterableAssert<?, Iterable<?>, Object, ObjectAssert<Object>> absIterableAssert = null;10absIterableAssert.getComparatorForElementPropertyOrFieldType("elementPropertyOrFieldType");11AbstractIterableAssert<?, Iterable<?>, Object, ObjectAssert<Object>> absIterableAssert = null;12absIterableAssert.getComparatorForElementPropertyOrFieldType("elementPropertyOrFieldType");13AbstractIterableAssert<?, Iterable<?>, Object, ObjectAssert<Object>> absIterableAssert = null;

Full Screen

Full Screen

getComparatorsForElementPropertyOrFieldTypes

Using AI Code Generation

copy

Full Screen

1import java.util.List;2public class AbstractIterableAssertGetComparatorsForElementPropertyOrFieldTypes {3 public static void main(String[] args) {4 IterableAssert<String> iterableAssert = Assertions.assertThat(List.of("one", "two", "three"));5 List<AbstractObjectAssert<?, ?>> comparators = iterableAssert.getComparatorsForElementPropertyOrFieldTypes(String.class, Integer.class);6 System.out.println("comparators = " + comparators);7 }8}

Full Screen

Full Screen

getComparatorsForElementPropertyOrFieldTypes

Using AI Code Generation

copy

Full Screen

1public class AssertJListTest {2 public void testListSorting() {3 List<Animal> animals = new ArrayList<Animal>();4 animals.add(new Animal("cat", 5));5 animals.add(new Animal("dog", 3));6 animals.add(new Animal("elephant", 10));7 animals.add(new Animal("ant", 1));8 assertThat(animals).isSortedAccordingTo(comparing(Animal::getAge));9 assertThat(animals).isSortedAccordingTo(comparing(Animal::getName, String::compareTo));10 assertThat(animals).isSortedAccordingTo(comparing(Animal::getName, String::compareTo).reversed());11 assertThat(animals).isSortedAccordingTo(comparing(Animal::getName, String::compareTo).reversed().thenComparing(Animal::getAge));12 assertThat(animals).is

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

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

Run Assertj automation tests on LambdaTest cloud grid

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

Most used method in AbstractIterableAssert

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful