Class IterableAssert<ELEMENT>
- java.lang.Object
-
- org.assertj.core.api.AbstractAssert<SELF,ACTUAL>
-
- org.assertj.core.api.AbstractIterableAssert<SELF,ACTUAL,ELEMENT,ELEMENT_ASSERT>
-
- org.assertj.core.api.FactoryBasedNavigableIterableAssert<IterableAssert<ELEMENT>,Iterable<? extends ELEMENT>,ELEMENT,ObjectAssert<ELEMENT>>
-
- org.assertj.core.api.IterableAssert<ELEMENT>
-
- Type Parameters:
ELEMENT- the type of elements of the "actual" value.
- All Implemented Interfaces:
Assert<IterableAssert<ELEMENT>,Iterable<? extends ELEMENT>>,Descriptable<IterableAssert<ELEMENT>>,EnumerableAssert<IterableAssert<ELEMENT>,ELEMENT>,ExtensionPoints<IterableAssert<ELEMENT>,Iterable<? extends ELEMENT>>,ObjectEnumerableAssert<IterableAssert<ELEMENT>,ELEMENT>
public class IterableAssert<ELEMENT> extends FactoryBasedNavigableIterableAssert<IterableAssert<ELEMENT>,Iterable<? extends ELEMENT>,ELEMENT,ObjectAssert<ELEMENT>>
Assertion methods forIterable.To create an instance of this class, invoke
.Assertions.assertThat(Iterable)- Author:
- Yvonne Wang, Alex Ruiz, Matthieu Baechler, Joel Costigliola, Mikhail Mazursky, Julien Meddah
-
-
Field Summary
-
Fields inherited from class org.assertj.core.api.AbstractIterableAssert
iterables
-
Fields inherited from class org.assertj.core.api.AbstractAssert
actual, assertionErrorCreator, conditions, info, myself, objects, throwUnsupportedExceptionOnEquals
-
-
Constructor Summary
Constructors Constructor Description IterableAssert(Iterable<? extends ELEMENT> actual)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description IterableAssert<ELEMENT>contains(ELEMENT... values)Verifies that the actual group contains the given values, in any order.IterableAssert<ELEMENT>containsAnyOf(ELEMENT... values)Verifies that the actualIterablecontains at least one of the given values.IterableAssert<ELEMENT>containsExactly(ELEMENT... values)Verifies that the actual group contains exactly the given values and nothing else, in order.
This assertion should only be used with groups that have a consistent iteration order (i.e.IterableAssert<ELEMENT>containsExactlyInAnyOrder(ELEMENT... values)Verifies that the actual group contains exactly the given values and nothing else, in any order.IterableAssert<ELEMENT>containsOnly(ELEMENT... values)Verifies that the actual group contains only the given values and nothing else, in any order and ignoring duplicates (i.e.IterableAssert<ELEMENT>containsOnlyOnce(ELEMENT... values)Verifies that the actual group contains the given values only once.IterableAssert<ELEMENT>containsSequence(ELEMENT... sequence)Verifies that the actual group contains the given sequence in the correct order and without extra values between the sequence values.IterableAssert<ELEMENT>containsSubsequence(ELEMENT... sequence)Verifies that the actual group contains the given subsequence in the correct order (possibly with other values between them).IterableAssert<ELEMENT>doesNotContain(ELEMENT... values)Verifies that the actual group does not contain the given values.IterableAssert<ELEMENT>doesNotContainSequence(ELEMENT... sequence)Verifies that the actual group does not contain the given sequence, a sequence is defined by an ordered group of values without extra values between them.IterableAssert<ELEMENT>doesNotContainSubsequence(ELEMENT... sequence)Verifies that the actual group does not contain the given subsequence, a subsequence is defined by an ordered group of values with possibly extra values between them.IterableAssert<ELEMENT>endsWith(ELEMENT first, ELEMENT... rest)Verifies that the actual group ends with the given sequence of objects, without any other objects between them.AbstractListAssert<?,List<? extends Tuple>,Tuple,ObjectAssert<Tuple>>extracting(Function<? super ELEMENT,?>... extractors)AbstractListAssert<?,List<?>,Object,ObjectAssert<Object>>flatExtracting(Function<? super ELEMENT,?>... extractors)Extract multiple values from eachIterable's element according to the givenFunctions and concatenate/flatten the extracted values in a list that is used as the new object under test.<EXCEPTION extends Exception>
AbstractListAssert<?,List<?>,Object,ObjectAssert<Object>>flatExtracting(ThrowingExtractor<? super ELEMENT,?,EXCEPTION>... extractors)Extract multiple values from eachIterable's element according to the givenThrowingExtractors and concatenate/flatten the extracted values in a list that is used as the new object under test.IterableAssert<ELEMENT>isSubsetOf(ELEMENT... values)Verifies that all the elements of actual are present in the given values.protected IterableAssert<ELEMENT>newAbstractIterableAssert(Iterable<? extends ELEMENT> iterable)(package private) static <T> Iterable<T>toIterable(Iterator<T> iterator)-
Methods inherited from class org.assertj.core.api.FactoryBasedNavigableIterableAssert
toAssert
-
Methods inherited from class org.assertj.core.api.AbstractIterableAssert
allMatch, allMatch, allSatisfy, anyMatch, anySatisfy, are, areAtLeast, areAtLeastOne, areAtMost, areExactly, areNot, as, as, containsAll, containsAnyElementsOf, containsExactlyElementsOf, containsExactlyInAnyOrderElementsOf, containsNull, containsOnlyElementsOf, containsOnlyNulls, containsSequence, containsSubsequence, describedAs, describedAs, doesNotContainAnyElementsOf, doesNotContainNull, doesNotContainSequence, doesNotContainSubsequence, doesNotHave, doesNotHaveAnyElementsOfTypes, doesNotHaveDuplicates, doesNotHaveSameClassAs, doNotHave, element, endsWith, extracting, extracting, extracting, extracting, extracting, extractingResultOf, extractingResultOf, filteredOn, filteredOn, filteredOn, filteredOn, filteredOnAssertions, filteredOnNull, first, flatExtracting, flatExtracting, flatExtracting, flatExtracting, getComparatorsByType, getComparatorsForElementPropertyOrFieldTypes, has, hasAtLeastOneElementOfType, hasOnlyElementsOfType, hasOnlyElementsOfTypes, hasOnlyOneElementSatisfying, hasSameClassAs, hasSameElementsAs, hasSameSizeAs, hasSameSizeAs, hasSize, hasSizeBetween, hasSizeGreaterThan, hasSizeGreaterThanOrEqualTo, hasSizeLessThan, hasSizeLessThanOrEqualTo, hasToString, have, haveAtLeast, haveAtLeastOne, haveAtMost, haveExactly, inBinary, inHexadecimal, is, isEmpty, isEqualTo, isExactlyInstanceOf, isIn, isIn, isInstanceOf, isInstanceOfAny, isNot, isNotEmpty, isNotEqualTo, isNotExactlyInstanceOf, isNotIn, isNotIn, isNotInstanceOf, isNotInstanceOfAny, isNotNull, isNotOfAnyClassIn, isNotSameAs, isNullOrEmpty, isOfAnyClassIn, isSameAs, isSubsetOf, last, navigationDescription, noneMatch, noneSatisfy, overridingErrorMessage, size, startsWith, usingComparator, usingComparator, usingComparatorForElementFieldsWithNames, usingComparatorForElementFieldsWithType, usingComparatorForType, usingComparisonStrategy, usingDefaultComparator, usingDefaultElementComparator, usingElementComparator, usingElementComparatorIgnoringFields, usingElementComparatorOnFields, usingFieldByFieldElementComparator, usingRecursiveFieldByFieldElementComparator, withAssertionState, withComparatorsForElementPropertyOrFieldNames, withComparatorsForElementPropertyOrFieldTypes, withFailMessage, withIterables, withThreadDumpOnError, withTypeComparators, zipSatisfy
-
Methods inherited from class org.assertj.core.api.AbstractAssert
asInstanceOf, asList, asString, descriptionText, equals, failWithMessage, getWritableAssertionInfo, hashCode, hasSameHashCodeAs, isInstanceOfSatisfying, isNull, matches, matches, newListAssertInstance, satisfies, satisfies, satisfiesAnyOf, satisfiesAnyOf, setCustomRepresentation, throwAssertionError, withRepresentation
-
-
-
-
Method Detail
-
newAbstractIterableAssert
protected IterableAssert<ELEMENT> newAbstractIterableAssert(Iterable<? extends ELEMENT> iterable)
- Overrides:
newAbstractIterableAssertin classFactoryBasedNavigableIterableAssert<IterableAssert<ELEMENT>,Iterable<? extends ELEMENT>,ELEMENT,ObjectAssert<ELEMENT>>
-
contains
@SafeVarargs public final IterableAssert<ELEMENT> contains(ELEMENT... values)
Description copied from class:AbstractIterableAssertVerifies that the actual group contains the given values, in any order.Example:
Iterable<String> abc = newArrayList("a", "b", "c"); // assertions will pass assertThat(abc).contains("b", "a"); assertThat(abc).contains("b", "a", "b"); // assertion will fail assertThat(abc).contains("d");If you want to specify the elements to check with an
Iterable, usecontainsAll(Iterable)instead.- Specified by:
containsin interfaceObjectEnumerableAssert<IterableAssert<ELEMENT>,ELEMENT>- Overrides:
containsin classAbstractIterableAssert<IterableAssert<ELEMENT>,Iterable<? extends ELEMENT>,ELEMENT,ObjectAssert<ELEMENT>>- Parameters:
values- the given values.- Returns:
thisassertion object.
-
containsOnly
@SafeVarargs public final IterableAssert<ELEMENT> containsOnly(ELEMENT... values)
Description copied from class:AbstractIterableAssertVerifies that the actual group contains only the given values and nothing else, in any order and ignoring duplicates (i.e. once a value is found, its duplicates are also considered found).If you need to check exactly the elements and their duplicates use:
containsExactly(Object...)if the order does mattercontainsExactlyInAnyOrder(Object...)if the order does not matter
Example:
Iterable<String> abc = newArrayList("a", "b", "c"); // assertions will pass as order does not matter assertThat(abc).containsOnly("c", "b", "a"); // duplicates are ignored assertThat(abc).containsOnly("a", "a", "b", "c", "c"); // ... on both actual and expected values assertThat(asList("a", "a", "b")).containsOnly("a", "b") .containsOnly("a", "a", "b", "b"); // assertion will fail because "c" is missing in the given values assertThat(abc).containsOnly("a", "b"); // assertion will fail because "d" is missing in abc (use isSubsetOf if you want this assertion to pass) assertThat(abc).containsOnly("a", "b", "c", "d");If you need to check that actual is a subset of the given values, use
ObjectEnumerableAssert.isSubsetOf(Object...).If you want to specify the elements to check with an
Iterable, usecontainsOnlyElementsOf(Iterable)instead.- Specified by:
containsOnlyin interfaceObjectEnumerableAssert<IterableAssert<ELEMENT>,ELEMENT>- Overrides:
containsOnlyin classAbstractIterableAssert<IterableAssert<ELEMENT>,Iterable<? extends ELEMENT>,ELEMENT,ObjectAssert<ELEMENT>>- Parameters:
values- the given values.- Returns:
thisassertion object.
-
containsOnlyOnce
@SafeVarargs public final IterableAssert<ELEMENT> containsOnlyOnce(ELEMENT... values)
Description copied from class:AbstractIterableAssertVerifies 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:
containsOnlyOncein interfaceObjectEnumerableAssert<IterableAssert<ELEMENT>,ELEMENT>- Overrides:
containsOnlyOncein classAbstractIterableAssert<IterableAssert<ELEMENT>,Iterable<? extends ELEMENT>,ELEMENT,ObjectAssert<ELEMENT>>- Parameters:
values- the given values.- Returns:
thisassertion object.
-
containsExactly
@SafeVarargs public final IterableAssert<ELEMENT> containsExactly(ELEMENT... values)
Description copied from class:AbstractIterableAssertVerifies that the actual group contains exactly the given values and nothing else, in order.
This assertion should only be used with groups that have a consistent iteration order (i.e. don't use it withHashSet, preferObjectEnumerableAssert.containsOnly(Object...)in that case).Example:
// an Iterable is used in the example but it would also work with an array Iterable<Ring> elvesRings = newArrayList(vilya, nenya, narya); // assertion will pass assertThat(elvesRings).containsExactly(vilya, nenya, narya); // assertion will fail as actual and expected order differ assertThat(elvesRings).containsExactly(nenya, vilya, narya);If you want to specify the elements to check with an
Iterable, usecontainsExactlyElementsOf(Iterable)instead.- Specified by:
containsExactlyin interfaceObjectEnumerableAssert<IterableAssert<ELEMENT>,ELEMENT>- Overrides:
containsExactlyin classAbstractIterableAssert<IterableAssert<ELEMENT>,Iterable<? extends ELEMENT>,ELEMENT,ObjectAssert<ELEMENT>>- Parameters:
values- the given values.- Returns:
thisassertion object.
-
containsExactlyInAnyOrder
@SafeVarargs public final IterableAssert<ELEMENT> containsExactlyInAnyOrder(ELEMENT... values)
Description copied from class:AbstractIterableAssertVerifies that the actual group contains exactly the given values and nothing else, in any order.
Example:
// an Iterable is used in the example but it would also work with an array Iterable<Ring> elvesRings = newArrayList(vilya, nenya, narya, vilya); // assertion will pass assertThat(elvesRings).containsExactlyInAnyOrder(vilya, vilya, nenya, narya); // assertion will fail as vilya is contained twice in elvesRings. assertThat(elvesRings).containsExactlyInAnyOrder(nenya, vilya, narya);If you want to specify the elements to check with an
Iterable, usecontainsExactlyInAnyOrderElementsOf(Iterable)instead.- Specified by:
containsExactlyInAnyOrderin interfaceObjectEnumerableAssert<IterableAssert<ELEMENT>,ELEMENT>- Overrides:
containsExactlyInAnyOrderin classAbstractIterableAssert<IterableAssert<ELEMENT>,Iterable<? extends ELEMENT>,ELEMENT,ObjectAssert<ELEMENT>>- Parameters:
values- the given values.- Returns:
thisassertion object.
-
containsAnyOf
@SafeVarargs public final IterableAssert<ELEMENT> containsAnyOf(ELEMENT... values)
Description copied from class:AbstractIterableAssertVerifies that the actualIterablecontains at least one of the given values.Example :
Iterable<String> abc = Arrays.asList("a", "b", "c"); // assertions will pass assertThat(abc).containsAnyOf("b") .containsAnyOf("b", "c") .containsAnyOf("a", "b", "c") .containsAnyOf("a", "b", "c", "d") .containsAnyOf("e", "f", "g", "b"); // assertions will fail assertThat(abc).containsAnyOf("d"); assertThat(abc).containsAnyOf("d", "e", "f", "g");- Specified by:
containsAnyOfin interfaceObjectEnumerableAssert<IterableAssert<ELEMENT>,ELEMENT>- Overrides:
containsAnyOfin classAbstractIterableAssert<IterableAssert<ELEMENT>,Iterable<? extends ELEMENT>,ELEMENT,ObjectAssert<ELEMENT>>- Parameters:
values- the values whose at least one which is expected to be in theIterableunder test.- Returns:
thisassertion object.
-
isSubsetOf
@SafeVarargs public final IterableAssert<ELEMENT> isSubsetOf(ELEMENT... values)
Description copied from class:AbstractIterableAssertVerifies that all the elements of actual are present in the given values.Example:
// an Iterable is used in the example but it would also work with an array Iterable<Ring> elvesRings = newArrayList(vilya, nenya, narya); // assertions will pass: assertThat(elvesRings).isSubsetOf(vilya, nenya, narya) .isSubsetOf(vilya, nenya, narya, dwarfRing); // assertions will fail: assertThat(elvesRings).isSubsetOf(vilya, nenya); assertThat(elvesRings).isSubsetOf(vilya, nenya, dwarfRing);If you want to specify the set of elements an
Iterable, useisSubsetOf(Iterable)instead.- Specified by:
isSubsetOfin interfaceObjectEnumerableAssert<IterableAssert<ELEMENT>,ELEMENT>- Overrides:
isSubsetOfin classAbstractIterableAssert<IterableAssert<ELEMENT>,Iterable<? extends ELEMENT>,ELEMENT,ObjectAssert<ELEMENT>>- Parameters:
values- the values that should be used for checking the elements of actual.- Returns:
- this assertion object.
-
containsSequence
@SafeVarargs public final IterableAssert<ELEMENT> containsSequence(ELEMENT... sequence)
Description copied from class:AbstractIterableAssertVerifies that the actual group contains the given sequence in the correct order and without extra values between the sequence values.Use
ObjectEnumerableAssert.containsSubsequence(Object...)to allow values between the expected sequence values.Example:
// an Iterable is used in the example but it would also work with an array Iterable<Ring> elvesRings = newArrayList(vilya, nenya, narya); // assertions will pass assertThat(elvesRings).containsSequence(vilya, nenya) .containsSequence(nenya, narya); // assertions will fail, the elements order is correct but there is a value between them (nenya) assertThat(elvesRings).containsSequence(vilya, narya); assertThat(elvesRings).containsSequence(nenya, vilya);If you want to specify the sequence to check with an
Iterable, usecontainsSequence(Iterable)instead.- Specified by:
containsSequencein interfaceObjectEnumerableAssert<IterableAssert<ELEMENT>,ELEMENT>- Overrides:
containsSequencein classAbstractIterableAssert<IterableAssert<ELEMENT>,Iterable<? extends ELEMENT>,ELEMENT,ObjectAssert<ELEMENT>>- Parameters:
sequence- the sequence of objects to look for.- Returns:
- this assertion object.
-
doesNotContainSequence
@SafeVarargs public final IterableAssert<ELEMENT> doesNotContainSequence(ELEMENT... sequence)
Description copied from class:AbstractIterableAssertVerifies that the actual group does not contain the given sequence, a sequence is defined by an ordered group of values without extra values between them.Use
ObjectEnumerableAssert.doesNotContainSubsequence(Object...)to also ensure the sequence does not exist with values between the expected sequence values.Example:
// an Iterable is used in the example but it would also work with an array Iterable<Ring> elvesRings = newArrayList(vilya, nenya, narya); // assertions will pass, the elements order is correct but there is a value between them (nenya) assertThat(elvesRings).doesNotContainSequence(vilya, narya) .doesNotContainSequence(nenya, vilya); // assertions will fail assertThat(elvesRings).doesNotContainSequence(vilya, nenya); assertThat(elvesRings).doesNotContainSequence(nenya, narya);If you want to specify the sequence not to find with an
Iterable, usedoesNotContainSequence(Iterable)instead.- Specified by:
doesNotContainSequencein interfaceObjectEnumerableAssert<IterableAssert<ELEMENT>,ELEMENT>- Overrides:
doesNotContainSequencein classAbstractIterableAssert<IterableAssert<ELEMENT>,Iterable<? extends ELEMENT>,ELEMENT,ObjectAssert<ELEMENT>>- Parameters:
sequence- the sequence of objects to look for.- Returns:
- this assertion object.
-
containsSubsequence
@SafeVarargs public final IterableAssert<ELEMENT> containsSubsequence(ELEMENT... sequence)
Description copied from class:AbstractIterableAssertVerifies that the actual group contains the given subsequence in the correct order (possibly with other values between them).Example:
// an Iterable is used in the example but it would also work with an array Iterable<Ring> elvesRings = newArrayList(vilya, nenya, narya); // assertions will pass assertThat(elvesRings).containsSubsequence(vilya, nenya) .containsSubsequence(vilya, narya); // assertion will fail assertThat(elvesRings).containsSubsequence(nenya, vilya);If you want to specify the elements of the subsequence to check with an
Iterable, usecontainsSubsequence(Iterable)instead.- Specified by:
containsSubsequencein interfaceObjectEnumerableAssert<IterableAssert<ELEMENT>,ELEMENT>- Overrides:
containsSubsequencein classAbstractIterableAssert<IterableAssert<ELEMENT>,Iterable<? extends ELEMENT>,ELEMENT,ObjectAssert<ELEMENT>>- Parameters:
sequence- the sequence of objects to look for.- Returns:
- this assertion object.
-
doesNotContainSubsequence
@SafeVarargs public final IterableAssert<ELEMENT> doesNotContainSubsequence(ELEMENT... sequence)
Description copied from class:AbstractIterableAssertVerifies that the actual group does not contain the given subsequence, a subsequence is defined by an ordered group of values with possibly extra values between them.Example:
// an Iterable is used in the example but it would also work with an array Iterable<Ring> elvesRings = newArrayList(vilya, nenya, narya); // assertions will pass assertThat(elvesRings).doesNotContainSubsequence(nenya, vilya) .doesNotContainSubsequence(narya, vilya); // assertion will fail assertThat(elvesRings).doesNotContainSubsequence(vilya, nenya); assertThat(elvesRings).doesNotContainSubsequence(vilya, narya);If you want to specify the subsequence not to find with an
Iterable, usedoesNotContainSubsequence(Iterable)instead.- Specified by:
doesNotContainSubsequencein interfaceObjectEnumerableAssert<IterableAssert<ELEMENT>,ELEMENT>- Overrides:
doesNotContainSubsequencein classAbstractIterableAssert<IterableAssert<ELEMENT>,Iterable<? extends ELEMENT>,ELEMENT,ObjectAssert<ELEMENT>>- Parameters:
sequence- the sequence of objects to look for.- Returns:
- this assertion object.
-
doesNotContain
@SafeVarargs public final IterableAssert<ELEMENT> doesNotContain(ELEMENT... values)
Description copied from interface:ObjectEnumerableAssertVerifies that the actual group does not contain the given values.Example:
// an Iterable is used in the example but it would also work with an array Iterable<String> abc = newArrayList("a", "b", "c"); // assertions will pass assertThat(abc).doesNotContain("d") .doesNotContain("d", "e"); // assertions will fail assertThat(abc).doesNotContain("a"); assertThat(abc).doesNotContain("a", "b"); assertThat(abc).doesNotContain("c", "d");If you want to specify the elements not to find with an
Iterable, usedoesNotContainAnyElementsOf(Iterable)instead.- Specified by:
doesNotContainin interfaceObjectEnumerableAssert<IterableAssert<ELEMENT>,ELEMENT>- Overrides:
doesNotContainin classAbstractIterableAssert<IterableAssert<ELEMENT>,Iterable<? extends ELEMENT>,ELEMENT,ObjectAssert<ELEMENT>>- Parameters:
values- the given values.- Returns:
thisassertion object.
-
endsWith
@SafeVarargs public final IterableAssert<ELEMENT> endsWith(ELEMENT first, ELEMENT... rest)
Description copied from class:AbstractIterableAssertVerifies that the actual group ends with the given sequence of objects, without any other objects between them. Similar to, but it also verifies that the last element in the sequence is also last element of the actual group.ObjectEnumerableAssert.containsSequence(Object...)Example:
// an Iterable is used in the example but it would also work with an array Iterable<String> abc = newArrayList("a", "b", "c"); // assertions will pass assertThat(abc).endsWith("c") .endsWith("b", "c"); // assertions will fail assertThat(abc).endsWith("a"); assertThat(abc).endsWith("a", "b");- Specified by:
endsWithin interfaceObjectEnumerableAssert<IterableAssert<ELEMENT>,ELEMENT>- Overrides:
endsWithin classAbstractIterableAssert<IterableAssert<ELEMENT>,Iterable<? extends ELEMENT>,ELEMENT,ObjectAssert<ELEMENT>>- Parameters:
first- the first element of the sequence of objects to look for.rest- the rest of the sequence of objects to look for.- Returns:
- this assertion object.
-
flatExtracting
@SafeVarargs public final <EXCEPTION extends Exception> AbstractListAssert<?,List<?>,Object,ObjectAssert<Object>> flatExtracting(ThrowingExtractor<? super ELEMENT,?,EXCEPTION>... extractors)
Description copied from class:AbstractIterableAssertExtract multiple values from eachIterable's element according to the givenThrowingExtractors 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:
The resulting extracted values list is ordered by// 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");Iterable's element first and then extracted values, this is why is in the example that age values come before names.- Overrides:
flatExtractingin classAbstractIterableAssert<IterableAssert<ELEMENT>,Iterable<? extends ELEMENT>,ELEMENT,ObjectAssert<ELEMENT>>- Type Parameters:
EXCEPTION- the exception type ofThrowingExtractor- Parameters:
extractors- all the extractors to apply on each actualIterable'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:AbstractIterableAssertExtract multiple values from eachIterable's element according to the givenFunctions 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:
The resulting extracted values list is ordered by// 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");Iterable's element first and then extracted values, this is why is in the example that age values come before names.- Overrides:
flatExtractingin classAbstractIterableAssert<IterableAssert<ELEMENT>,Iterable<? extends ELEMENT>,ELEMENT,ObjectAssert<ELEMENT>>- Parameters:
extractors- all the extractors to apply on each actualIterable's elements- Returns:
- a new assertion object whose object under test is a flattened list of all extracted values.
-
extracting
@SafeVarargs public final AbstractListAssert<?,List<? extends Tuple>,Tuple,ObjectAssert<Tuple>> extracting(Function<? super ELEMENT,?>... extractors)
Description copied from class:AbstractIterableAssertUse the givenFunctions to extract the values from theIterable's elements into a newIterablecomposed ofTuples (a simple data structure containing the extracted values), this newIterablebecoming 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 :
You can use lambda expression or a method reference to extract the expected values.// 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));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:
extractingin classAbstractIterableAssert<IterableAssert<ELEMENT>,Iterable<? 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.
-
-