Class ObjectArrayAssert<ELEMENT>

    • Constructor Detail

      • ObjectArrayAssert

        public ObjectArrayAssert​(ELEMENT[] actual)
      • ObjectArrayAssert

        public ObjectArrayAssert​(java.util.concurrent.atomic.AtomicReferenceArray<ELEMENT> actual)
    • Method Detail

      • extracting

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

        It allows you to test values from the array'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 arrays's elements, for instance if you pass functions extracting "id", "name" and "email" values then each Tuple's data will be composed of an id, a name and an email extracted from the element of the initial array (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 an array of TolkienCharacter, a TolkienCharacter has a name (String) and a Race (a class)
         // they can be public field or properties, both works when extracting their values.
         TolkienCharacter[] fellowshipOfTheRing = new TolkienCharacter[] {
           new TolkienCharacter("Frodo", 33, HOBBIT),
           new TolkienCharacter("Sam", 38, HOBBIT),
           new TolkienCharacter("Gandalf", 2020, MAIA),
           new TolkienCharacter("Legolas", 1000, ELF),
           new TolkienCharacter("Pippin", 28, HOBBIT),
           new TolkienCharacter("Gimli", 139, DWARF),
           new TolkienCharacter("Aragorn", 87, MAN,
           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 array 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 AbstractObjectArrayAssert<ObjectArrayAssert<ELEMENT>,​ELEMENT>
        Parameters:
        extractors - the extractor functions to extract a value from an element of the array under test.
        Returns:
        a new assertion object whose object under test is the list of Tuples containing the extracted values.
      • satisfiesExactly

        @SafeVarargs
        public final ObjectArrayAssert<ELEMENT> satisfiesExactly​(java.util.function.Consumer<? super ELEMENT>... requirements)
        Description copied from interface: ObjectEnumerableAssert
        Verifies that each element satisfies the requirements corresponding to its index, so the first element must satisfy the first requirements, the second element the second requirements etc...

        Each requirements are expressed as a Consumer, there must be as many requirements as there are iterable elements.

        Example:

         Iterable<TolkienCharater> characters = list(frodo, aragorn, legolas);
        
         // assertions succeed
         assertThat(characters).satisfiesExactly(character -> assertThat(character.getRace()).isEqualTo("Hobbit"),
                                                 character -> assertThat(character.isMortal()).isTrue(),
                                                 character -> assertThat(character.getName()).isEqualTo("Legolas"));
        
         // you can specify more that one assertion per requirements
         assertThat(characters).satisfiesExactly(character -> {
                                                    assertThat(character.getRace()).isEqualTo("Hobbit");
                                                    assertThat(character.getName()).isEqualTo("Frodo");
                                                 },
                                                 character -> {
                                                    assertThat(character.isMortal()).isTrue();
                                                    assertThat(character.getName()).isEqualTo("Aragorn");
                                                 },
                                                 character -> {
                                                    assertThat(character.getRace()).isEqualTo("Elf");
                                                    assertThat(character.getName()).isEqualTo("Legolas");
                                                 });
        
         // assertion fails as aragorn does not meet the second requirements
         assertThat(characters).satisfiesExactly(character -> assertThat(character.getRace()).isEqualTo("Hobbit"),
                                                 character -> assertThat(character.isMortal()).isFalse(),
                                                 character -> assertThat(character.getName()).isEqualTo("Legolas"));
        Specified by:
        satisfiesExactly in interface ObjectEnumerableAssert<AbstractObjectArrayAssert<ObjectArrayAssert<ELEMENT>,​ELEMENT>,​ELEMENT>
        Overrides:
        satisfiesExactly in class AbstractObjectArrayAssert<ObjectArrayAssert<ELEMENT>,​ELEMENT>
        Parameters:
        requirements - the requirements to meet.
        Returns:
        this to chain assertions.
      • satisfiesExactlyInAnyOrder

        @SafeVarargs
        public final ObjectArrayAssert<ELEMENT> satisfiesExactlyInAnyOrder​(java.util.function.Consumer<? super ELEMENT>... requirements)
        Description copied from interface: ObjectEnumerableAssert
        Verifies that at least one combination of iterable elements exists that satisfies the consumers in order (there must be as many consumers as iterable elements and once a consumer is matched it cannot be reused to match other elements).

        This is a variation of ObjectEnumerableAssert.satisfiesExactly(Consumer...) where order does not matter.

        Examples:

         List<String> starWarsCharacterNames = list("Luke", "Leia", "Yoda");
        
         // these assertions succeed:
         assertThat(starWarsCharacterNames).satisfiesExactlyInAnyOrder(name -> assertThat(name).contains("Y"), // matches "Yoda"
                                                                       name -> assertThat(name).contains("L"), // matches "Luke" and "Leia"
                                                                       name -> {
                                                                         assertThat(name).hasSize(4);
                                                                         assertThat(name).doesNotContain("a"); // matches "Luke" but not "Leia"
                                                                       })
                                           .satisfiesExactlyInAnyOrder(name -> assertThat(name).contains("Yo"),
                                                                       name -> assertThat(name).contains("Lu"),
                                                                       name -> assertThat(name).contains("Le"))
                                           .satisfiesExactlyInAnyOrder(name -> assertThat(name).contains("Le"),
                                                                       name -> assertThat(name).contains("Yo"),
                                                                       name -> assertThat(name).contains("Lu"));
        
         // this assertion fails as 3 consumer/requirements are expected
         assertThat(starWarsCharacterNames).satisfiesExactlyInAnyOrder(name -> assertThat(name).contains("Y"),
                                                                       name -> assertThat(name).contains("L"));
        
         // this assertion fails as no element contains "Han" (first consumer/requirements can't be met)
         assertThat(starWarsCharacterNames).satisfiesExactlyInAnyOrder(name -> assertThat(name).contains("Han"),
                                                                       name -> assertThat(name).contains("L"),
                                                                       name -> assertThat(name).contains("Y"));
        
         // this assertion fails as "Yoda" element can't satisfy any consumers/requirements (even though all consumers/requirements are met)
         assertThat(starWarsCharacterNames).satisfiesExactlyInAnyOrder(name -> assertThat(name).contains("L"),
                                                                       name -> assertThat(name).contains("L"),
                                                                       name -> assertThat(name).contains("L"));
        
         // this assertion fails as no combination of elements can satisfy the consumers in order
         // the problem is if the last consumer is matched by Leia then no other consumer can match Luke (and vice versa)
         assertThat(starWarsCharacterNames).satisfiesExactlyInAnyOrder(name -> assertThat(name).contains("Y"),
                                                                       name -> assertThat(name).contains("o"),
                                                                       name -> assertThat(name).contains("L"));
        Specified by:
        satisfiesExactlyInAnyOrder in interface ObjectEnumerableAssert<AbstractObjectArrayAssert<ObjectArrayAssert<ELEMENT>,​ELEMENT>,​ELEMENT>
        Overrides:
        satisfiesExactlyInAnyOrder in class AbstractObjectArrayAssert<ObjectArrayAssert<ELEMENT>,​ELEMENT>
        Parameters:
        requirements - the consumers that are expected to be satisfied by the elements of the given Iterable.
        Returns:
        this assertion object.