Class ListAssert<ELEMENT>

    • Constructor Detail

      • ListAssert

        public ListAssert​(List<? extends ELEMENT> actual)
      • ListAssert

        public ListAssert​(Stream<? extends ELEMENT> actual)
      • ListAssert

        public ListAssert​(IntStream actual)
      • ListAssert

        public ListAssert​(LongStream actual)
    • Method Detail

      • isInstanceOf

        public ListAssert<ELEMENT> isInstanceOf​(Class<?> type)
        Description copied from class: AbstractAssert
        Verifies that the actual value is an instance of the given type.

        Example:

         // assertions will pass
         assertThat("abc").isInstanceOf(String.class);
         assertThat(new HashMap<String, Integer>()).isInstanceOf(HashMap.class);
         assertThat(new HashMap<String, Integer>()).isInstanceOf(Map.class);
        
         // assertions will fail
         assertThat(1).isInstanceOf(String.class);
         assertThat(new ArrayList<String>()).isInstanceOf(LinkedList.class);
        Specified by:
        isInstanceOf in interface Assert<ListAssert<ELEMENT>,​List<? extends ELEMENT>>
        Overrides:
        isInstanceOf in class AbstractListAssert<ListAssert<ELEMENT>,​List<? extends ELEMENT>,​ELEMENT,​ObjectAssert<ELEMENT>>
        Parameters:
        type - the type to check the actual value against.
        Returns:
        this assertion object.
      • isInstanceOfAny

        public ListAssert<ELEMENT> isInstanceOfAny​(Class<?>... types)
        Description copied from class: AbstractAssert
        Verifies that the actual value is an instance of any of the given types.

        Example:

         // assertions will pass
         assertThat("abc").isInstanceOfAny(String.class, Integer.class);
         assertThat(new ArrayList<String>()).isInstanceOfAny(LinkedList.class, ArrayList.class);
         assertThat(new HashMap<String, Integer>()).isInstanceOfAny(TreeMap.class, Map.class);
        
         // assertions will fail
         assertThat(1).isInstanceOfAny(Double.class, Float.class);
         assertThat(new ArrayList<String>()).isInstanceOfAny(LinkedList.class, Vector.class);
        Specified by:
        isInstanceOfAny in interface Assert<ListAssert<ELEMENT>,​List<? extends ELEMENT>>
        Overrides:
        isInstanceOfAny in class AbstractListAssert<ListAssert<ELEMENT>,​List<? extends ELEMENT>,​ELEMENT,​ObjectAssert<ELEMENT>>
        Parameters:
        types - the types to check the actual value against.
        Returns:
        this assertion object.
      • isOfAnyClassIn

        public ListAssert<ELEMENT> isOfAnyClassIn​(Class<?>... types)
        Description copied from class: AbstractAssert
        Verifies that the actual value type is in given types.

        Example:

         // assertions will pass
         assertThat(new HashMap<String, Integer>()).isOfAnyClassIn(HashMap.class, TreeMap.class);
         assertThat(new ArrayList<String>()).isOfAnyClassIn(ArrayList.class, LinkedList.class);
        
         // assertions will fail
         assertThat(new HashMap<String, Integer>()).isOfAnyClassIn(TreeMap.class, Map.class);
         assertThat(new ArrayList<String>()).isOfAnyClassIn(LinkedList.class, List.class);
        Specified by:
        isOfAnyClassIn in interface Assert<ListAssert<ELEMENT>,​List<? extends ELEMENT>>
        Overrides:
        isOfAnyClassIn in class AbstractListAssert<ListAssert<ELEMENT>,​List<? extends ELEMENT>,​ELEMENT,​ObjectAssert<ELEMENT>>
        Parameters:
        types - the types to check the actual value against.
        Returns:
        this assertion object.
      • isExactlyInstanceOf

        public ListAssert<ELEMENT> isExactlyInstanceOf​(Class<?> type)
        Description copied from class: AbstractAssert
        Verifies that the actual value is exactly an instance of the given type.

        Example:

         // assertions will pass
         assertThat("abc").isExactlyInstanceOf(String.class);
         assertThat(new ArrayList<String>()).isExactlyInstanceOf(ArrayList.class);
         assertThat(new HashMap<String, Integer>()).isExactlyInstanceOf(HashMap.class);
        
         // assertions will fail
         assertThat(1).isExactlyInstanceOf(String.class);
         assertThat(new ArrayList<String>()).isExactlyInstanceOf(List.class);
         assertThat(new HashMap<String, Integer>()).isExactlyInstanceOf(Map.class);
        Specified by:
        isExactlyInstanceOf in interface Assert<ListAssert<ELEMENT>,​List<? extends ELEMENT>>
        Overrides:
        isExactlyInstanceOf in class AbstractListAssert<ListAssert<ELEMENT>,​List<? extends ELEMENT>,​ELEMENT,​ObjectAssert<ELEMENT>>
        Parameters:
        type - the type to check the actual value against.
        Returns:
        this assertion object.
      • isNotInstanceOf

        public ListAssert<ELEMENT> isNotInstanceOf​(Class<?> type)
        Description copied from class: AbstractAssert
        Verifies that the actual value is not an instance of the given type.

        Example:

         // assertions will pass
         assertThat(1).isNotInstanceOf(Double.class);
         assertThat(new ArrayList<String>()).isNotInstanceOf(LinkedList.class);
        
         // assertions will fail
         assertThat("abc").isNotInstanceOf(String.class);
         assertThat(new HashMap<String, Integer>()).isNotInstanceOf(HashMap.class);
         assertThat(new HashMap<String, Integer>()).isNotInstanceOf(Map.class);
        Specified by:
        isNotInstanceOf in interface Assert<ListAssert<ELEMENT>,​List<? extends ELEMENT>>
        Overrides:
        isNotInstanceOf in class AbstractListAssert<ListAssert<ELEMENT>,​List<? extends ELEMENT>,​ELEMENT,​ObjectAssert<ELEMENT>>
        Parameters:
        type - the type to check the actual value against.
        Returns:
        this assertion object.
      • isNotInstanceOfAny

        public ListAssert<ELEMENT> isNotInstanceOfAny​(Class<?>... types)
        Description copied from class: AbstractAssert
        Verifies that the actual value is not an instance of any of the given types.

        Example:

         // assertions will pass
         assertThat(1).isNotInstanceOfAny(Double.class, Float.class);
         assertThat(new ArrayList<String>()).isNotInstanceOfAny(LinkedList.class, Vector.class);
        
         // assertions will fail
         assertThat(1).isNotInstanceOfAny(Double.class, Integer.class);
         assertThat(new ArrayList<String>()).isNotInstanceOfAny(LinkedList.class, ArrayList.class);
         assertThat(new HashMap<String, Integer>()).isNotInstanceOfAny(TreeMap.class, Map.class);
        Specified by:
        isNotInstanceOfAny in interface Assert<ListAssert<ELEMENT>,​List<? extends ELEMENT>>
        Overrides:
        isNotInstanceOfAny in class AbstractListAssert<ListAssert<ELEMENT>,​List<? extends ELEMENT>,​ELEMENT,​ObjectAssert<ELEMENT>>
        Parameters:
        types - the types to check the actual value against.
        Returns:
        this assertion object.
      • isNotOfAnyClassIn

        public ListAssert<ELEMENT> isNotOfAnyClassIn​(Class<?>... types)
        Description copied from class: AbstractAssert
        Verifies that the actual value type is not in given types.

        Example:

         // assertions will pass
         assertThat(new HashMap<String, Integer>()).isNotOfAnyClassIn(Map.class, TreeMap.class);
         assertThat(new ArrayList<String>()).isNotOfAnyClassIn(LinkedList.class, List.class);
        
         // assertions will fail
         assertThat(new HashMap<String, Integer>()).isNotOfAnyClassIn(HashMap.class, TreeMap.class);
         assertThat(new ArrayList<String>()).isNotOfAnyClassIn(ArrayList.class, LinkedList.class);
        Specified by:
        isNotOfAnyClassIn in interface Assert<ListAssert<ELEMENT>,​List<? extends ELEMENT>>
        Overrides:
        isNotOfAnyClassIn in class AbstractListAssert<ListAssert<ELEMENT>,​List<? extends ELEMENT>,​ELEMENT,​ObjectAssert<ELEMENT>>
        Parameters:
        types - the types to check the actual value against.
        Returns:
        this assertion object.
      • isNotExactlyInstanceOf

        public ListAssert<ELEMENT> isNotExactlyInstanceOf​(Class<?> type)
        Description copied from class: AbstractAssert
        Verifies that the actual value is not exactly an instance of given type.

        Example:

         // assertions will pass
         assertThat(1).isNotExactlyInstanceOf(String.class);
         assertThat(new ArrayList<String>()).isNotExactlyInstanceOf(List.class);
         assertThat(new HashMap<String, Integer>()).isNotExactlyInstanceOf(Map.class);
        
         // assertions will fail
         assertThat("abc").isNotExactlyInstanceOf(String.class);
         assertThat(new ArrayList<String>()).isNotExactlyInstanceOf(ArrayList.class);
         assertThat(new HashMap<String, Integer>()).isNotExactlyInstanceOf(HashMap.class);
        Specified by:
        isNotExactlyInstanceOf in interface Assert<ListAssert<ELEMENT>,​List<? extends ELEMENT>>
        Overrides:
        isNotExactlyInstanceOf in class AbstractListAssert<ListAssert<ELEMENT>,​List<? extends ELEMENT>,​ELEMENT,​ObjectAssert<ELEMENT>>
        Parameters:
        type - the type to check the actual value against.
        Returns:
        this assertion object.
      • isSameAs

        public ListAssert<ELEMENT> isSameAs​(Object expected)
        Description copied from class: AbstractAssert
        Verifies that the actual value is the same as the given one, ie using == comparison.

        Example:

         // Name is a class with first and last fields, two Names are equals if both first and last are equals.
         Name tyrion = new Name("Tyrion", "Lannister");
         Name alias  = tyrion;
         Name clone  = new Name("Tyrion", "Lannister");
        
         // assertions succeed:
         assertThat(tyrion).isSameAs(alias)
                           .isEqualTo(clone);
        
         // assertion fails:
         assertThat(tyrion).isSameAs(clone);
        Specified by:
        isSameAs in interface Assert<ListAssert<ELEMENT>,​List<? extends ELEMENT>>
        Overrides:
        isSameAs in class AbstractListAssert<ListAssert<ELEMENT>,​List<? extends ELEMENT>,​ELEMENT,​ObjectAssert<ELEMENT>>
        Parameters:
        expected - the given value to compare the actual value to.
        Returns:
        this assertion object.
      • isNotSameAs

        public ListAssert<ELEMENT> isNotSameAs​(Object expected)
        Description copied from class: AbstractAssert
        Verifies that the actual value is not the same as the given one, ie using == comparison.

        Example:

         // Name is a class with first and last fields, two Names are equals if both first and last are equals.
         Name tyrion = new Name("Tyrion", "Lannister");
         Name alias  = tyrion;
         Name clone  = new Name("Tyrion", "Lannister");
        
         // assertions succeed:
         assertThat(clone).isNotSameAs(tyrion)
                          .isEqualTo(tyrion);
        
         // assertion fails:
         assertThat(alias).isNotSameAs(tyrion);
        Specified by:
        isNotSameAs in interface Assert<ListAssert<ELEMENT>,​List<? extends ELEMENT>>
        Overrides:
        isNotSameAs in class AbstractListAssert<ListAssert<ELEMENT>,​List<? extends ELEMENT>,​ELEMENT,​ObjectAssert<ELEMENT>>
        Parameters:
        expected - the given value to compare the actual value to.
        Returns:
        this assertion object.
      • containsOnlyOnce

        @SafeVarargs
        public final ListAssert<ELEMENT> containsOnlyOnce​(ELEMENT... values)
        Description copied from class: AbstractIterableAssert
        Verifies that the actual group contains the given values only once.

        Examples :

         // lists are used in the examples but it would also work with arrays
        
         // assertions will pass
         assertThat(newArrayList("winter", "is", "coming")).containsOnlyOnce("winter");
         assertThat(newArrayList("winter", "is", "coming")).containsOnlyOnce("coming", "winter");
        
         // assertions will fail
         assertThat(newArrayList("winter", "is", "coming")).containsOnlyOnce("Lannister");
         assertThat(newArrayList("Arya", "Stark", "daughter", "of", "Ned", "Stark")).containsOnlyOnce("Stark");
         assertThat(newArrayList("Arya", "Stark", "daughter", "of", "Ned", "Stark")).containsOnlyOnce("Stark", "Lannister", "Arya");
        Specified by:
        containsOnlyOnce in interface ObjectEnumerableAssert<ListAssert<ELEMENT>,​ELEMENT>
        Overrides:
        containsOnlyOnce in class AbstractIterableAssert<ListAssert<ELEMENT>,​List<? extends ELEMENT>,​ELEMENT,​ObjectAssert<ELEMENT>>
        Parameters:
        values - the given values.
        Returns:
        this assertion object.
      • extracting

        @SafeVarargs
        public final AbstractListAssert<?,​List<? extends Tuple>,​Tuple,​ObjectAssert<Tuple>> extracting​(Function<? super ELEMENT,​?>... extractors)
        Description copied from class: AbstractIterableAssert
        Use the given Functions to extract the values from the Iterable's elements into a new Iterable composed of Tuples (a simple data structure containing the extracted values), this new Iterable becoming the object under test.

        It allows you to test values from the Iterable's elements instead of testing the elements themselves, which sometimes can be much less work!

        The Tuple data corresponds to the extracted values from the Iterable's elements, for instance if you pass functions extracting "id", "name" and "email" values then each Tuple data will be composed of an id, a name and an email extracted from the element of the initial Iterable (the Tuple's data order is the same as the given functions order).

        Let's take a look at an example to make things clearer :

         // Build a list of TolkienCharacter, a TolkienCharacter has a name, and age and a Race (a specific class)
         // they can be public field or properties, both can be extracted.
         List<TolkienCharacter> fellowshipOfTheRing = new ArrayList<TolkienCharacter>();
        
         fellowshipOfTheRing.add(new TolkienCharacter("Frodo", 33, HOBBIT));
         fellowshipOfTheRing.add(new TolkienCharacter("Sam", 38, HOBBIT));
         fellowshipOfTheRing.add(new TolkienCharacter("Gandalf", 2020, MAIA));
         fellowshipOfTheRing.add(new TolkienCharacter("Legolas", 1000, ELF));
         fellowshipOfTheRing.add(new TolkienCharacter("Pippin", 28, HOBBIT));
         fellowshipOfTheRing.add(new TolkienCharacter("Gimli", 139, DWARF));
         fellowshipOfTheRing.add(new TolkienCharacter("Aragorn", 87, MAN);
         fellowshipOfTheRing.add(new TolkienCharacter("Boromir", 37, MAN));
        
         // let's verify 'name', 'age' and Race of some TolkienCharacter in fellowshipOfTheRing :
         assertThat(fellowshipOfTheRing).extracting(TolkienCharacter::getName,
                                                    character -> character.getAge(),
                                                    TolkienCharacter::getRace)
                                        .containsOnly(tuple("Frodo", 33, HOBBIT),
                                                      tuple("Sam", 38, HOBBIT),
                                                      tuple("Gandalf", 2020, MAIA),
                                                      tuple("Legolas", 1000, ELF),
                                                      tuple("Pippin", 28, HOBBIT),
                                                      tuple("Gimli", 139, DWARF),
                                                      tuple("Aragorn", 87, MAN),
                                                      tuple("Boromir", 37, MAN));
        You can use lambda expression or a method reference to extract the expected values.

        Use Tuple.tuple(Object...) to initialize the expected values.

        Note that the order of the extracted tuples list is consistent with the iteration order of the Iterable under test, for example if it's a HashSet, you won't be able to make any assumptions on the extracted tuples order.

        Overrides:
        extracting in class AbstractIterableAssert<ListAssert<ELEMENT>,​List<? extends ELEMENT>,​ELEMENT,​ObjectAssert<ELEMENT>>
        Parameters:
        extractors - the extractor functions to extract a value from an element of the Iterable under test.
        Returns:
        a new assertion object whose object under test is the list of Tuples containing the extracted values.
      • flatExtracting

        @SafeVarargs
        public final <EXCEPTION extends ExceptionAbstractListAssert<?,​List<?>,​Object,​ObjectAssert<Object>> flatExtracting​(ThrowingExtractor<? super ELEMENT,​?,​EXCEPTION>... extractors)
        Description copied from class: AbstractIterableAssert
        Extract multiple values from each Iterable's element according to the given ThrowingExtractors and concatenate/flatten the extracted values in a list that is used as the new object under test.

        If extracted values were not flattened, instead of a simple list like (given 2 extractors) :

        element1.value1, element1.value2, element2.value1, element2.value2, ...  
        we would get a list of list like :
        list(element1.value1, element1.value2), list(element2.value1, element2.value2), ...  

        Code example:

         // fellowshipOfTheRing is a List<TolkienCharacter>
        
         // values are extracted in order and flattened : age1, name1, age2, name2, age3 ...
         assertThat(fellowshipOfTheRing).flatExtracting(input -> {
           if (input.getAge() < 20) {
             throw new Exception("age < 20");
           }
           return input.getName();
         }, input2 -> {
           if (input2.getAge() < 20) {
             throw new Exception("age < 20");
           }
           return input2.getAge();
         }).contains(33 ,"Frodo",
             1000, "Legolas",
             87, "Aragorn");
        The resulting extracted values list is ordered by Iterable's element first and then extracted values, this is why is in the example that age values come before names.
        Overrides:
        flatExtracting in class AbstractIterableAssert<ListAssert<ELEMENT>,​List<? extends ELEMENT>,​ELEMENT,​ObjectAssert<ELEMENT>>
        Type Parameters:
        EXCEPTION - the exception type of ThrowingExtractor
        Parameters:
        extractors - all the extractors to apply on each actual Iterable's elements
        Returns:
        a new assertion object whose object under test is a flattened list of all extracted values.
      • flatExtracting

        @SafeVarargs
        public final AbstractListAssert<?,​List<?>,​Object,​ObjectAssert<Object>> flatExtracting​(Function<? super ELEMENT,​?>... extractors)
        Description copied from class: AbstractIterableAssert
        Extract multiple values from each Iterable's element according to the given Functions and concatenate/flatten the extracted values in a list that is used as the new object under test.

        If extracted values were not flattened, instead of a simple list like (given 2 extractors) :

        element1.value1, element1.value2, element2.value1, element2.value2, ...  
        we would get a list of list like :
        list(element1.value1, element1.value2), list(element2.value1, element2.value2), ...  

        Code example:

         // fellowshipOfTheRing is a List<TolkienCharacter>
        
         // values are extracted in order and flattened : age1, name1, age2, name2, age3 ...
         assertThat(fellowshipOfTheRing).flatExtracting(TolkienCharacter::getAge,
                                                        TolkienCharacter::getName)
                                        .contains(33 ,"Frodo",
                                                  1000, "Legolas",
                                                  87, "Aragorn");
        The resulting extracted values list is ordered by Iterable's element first and then extracted values, this is why is in the example that age values come before names.
        Overrides:
        flatExtracting in class AbstractIterableAssert<ListAssert<ELEMENT>,​List<? extends ELEMENT>,​ELEMENT,​ObjectAssert<ELEMENT>>
        Parameters:
        extractors - all the extractors to apply on each actual Iterable's elements
        Returns:
        a new assertion object whose object under test is a flattened list of all extracted values.