S
- the "self" type of this assertion class. Please read "Emulating 'self types' using Java Generics to simplify fluent API implementation"
for more details.T
- the type of elements of the "actual" value.public interface ObjectEnumerableAssert<S extends ObjectEnumerableAssert<S,T>,T> extends EnumerableAssert<S,T>
Modifier and Type | Method and Description |
---|---|
S |
allMatch(java.util.function.Predicate<? super T> predicate)
Verifies that all the elements of actual match the given
Predicate . |
S |
are(Condition<? super T> condition)
Verifies that each element value satisfies the given condition
|
S |
areAtLeast(int n,
Condition<? super T> condition)
Verifies that there is at least n elements in the actual group satisfying the given condition.
|
S |
areAtLeastOne(Condition<? super T> condition)
Verifies that there is at least one element in the actual group satisfying the given condition.
|
S |
areAtMost(int n,
Condition<? super T> condition)
Verifies that there is at most n elements in the actual group satisfying the given condition.
|
S |
areExactly(int n,
Condition<? super T> condition)
Verifies that there is exactly n elements in the actual group satisfying the given condition.
|
S |
areNot(Condition<? super T> condition)
Verifies that each element value does not satisfy the given condition
|
S |
contains(T... values)
Verifies that the actual group contains the given values, in any order.
|
S |
containsAll(Iterable<? extends T> iterable)
Verifies that the actual group contains all the elements of given
Iterable , in any order. |
S |
containsExactly(T... values)
Verifies that the actual group contains only the given values and nothing else, in order.
This assertion should only be used with group that have a consistent iteration order (i.e. |
S |
containsExactlyElementsOf(Iterable<? extends T> iterable)
Same as
containsExactly(Object...) but handle the Iterable to array conversion : verifies that
actual contains all the elements of the given iterable and nothing else in the same order. |
S |
containsNull()
Verifies that the actual group contains at least a null element.
|
S |
containsOnly(T... values)
Verifies that the actual group contains only the given values and nothing else, in any order.
|
S |
containsOnlyElementsOf(Iterable<? extends T> iterable)
Same semantic as
containsOnly(Object[]) : verifies that actual contains all the elements of the given
iterable and nothing else, in any order. |
S |
containsOnlyOnce(T... values)
Verifies that the actual group contains the given values only once.
|
S |
containsSequence(T... sequence)
Verifies that the actual group contains the given sequence in the correct order and without extra value between the sequence values.
|
S |
containsSubsequence(T... sequence)
Verifies that the actual group contains the given subsequence in the correct order (possibly with other values between them).
|
S |
doesNotContain(T... values)
Verifies that the actual group does not contain the given values.
|
S |
doesNotContainAnyElementsOf(Iterable<? extends T> iterable)
Verifies that actual does not contain any elements of the given
Iterable (i.e. |
S |
doesNotContainNull()
Verifies that the actual group does not contain null elements.
|
S |
doesNotHaveDuplicates()
Verifies that the actual group does not contain duplicates.
|
S |
doNotHave(Condition<? super T> condition)
Verifies that all elements don't satisfy the given condition.
|
S |
endsWith(T... sequence)
Verifies that the actual group ends with the given sequence of objects, without any other objects between them.
|
S |
hasAtLeastOneElementOfType(Class<?> expectedType)
Verifies that at least one element in the actual
Object group belong to the specified type (matching
includes subclasses of the given type). |
S |
hasOnlyElementsOfType(Class<?> expectedType)
Verifies that all the elements in the actual
Object group belong to the specified type (matching includes
subclasses of the given type). |
S |
hasSameElementsAs(Iterable<? extends T> iterable)
An alias of
containsOnlyElementsOf(Iterable) : verifies that actual contains all the elements of the
given iterable and nothing else, in any order. |
S |
have(Condition<? super T> condition)
Verifies that all elements satisfy the given condition.
|
S |
haveAtLeast(int n,
Condition<? super T> condition)
Verifies that there is at least n elements in the actual group satisfying the given condition.
|
S |
haveAtLeastOne(Condition<? super T> condition)
Verifies that there is at least one element in the actual group satisfying the given condition.
|
S |
haveAtMost(int n,
Condition<? super T> condition)
Verifies that there is at most n elements in the actual group satisfying the given condition.
|
S |
haveExactly(int n,
Condition<? super T> condition)
Verifies that there is exactly n elements in the actual group satisfying the given condition.
|
S |
isSubsetOf(Iterable<? extends T> values)
Verifies that all the elements of actual are present in the given
Iterable . |
S |
isSubsetOf(T... values)
Verifies that all the elements of actual are present in the given values.
|
S |
startsWith(T... sequence)
Verifies that the actual group starts with the given sequence of objects, without any other objects between them.
|
hasSameSizeAs, hasSameSizeAs, hasSize, isEmpty, isNotEmpty, isNullOrEmpty, usingDefaultElementComparator, usingElementComparator
S contains(T... values)
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");
values
- the given values.this
assertion object.NullPointerException
- if the given argument is null
.IllegalArgumentException
- if the given argument is an empty array.AssertionError
- if the actual group is null
.AssertionError
- if the actual group does not contain the given values.S containsOnly(T... values)
Example :
Iterable<String> abc = newArrayList("a", "b", "c");
// assertion will pass
assertThat(abc).containsOnly("c", "b", "a");
// assertion will fail because "c" is missing
assertThat(abc).containsOnly("a", "b");
values
- the given values.this
assertion object.NullPointerException
- if the given argument is null
.IllegalArgumentException
- if the given argument is an empty array.AssertionError
- if the actual group is null
.AssertionError
- if the actual group does not contain the given values, i.e. the actual group contains some
or none of the given values, or the actual group contains more values than the given ones.S containsOnlyOnce(T... values)
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("Aria", "Stark", "daughter", "of", "Ned", "Stark")).containsOnlyOnce("Stark");
assertThat(newArrayList("Aria", "Stark", "daughter", "of", "Ned", "Stark")).containsOnlyOnce("Stark", "Lannister", "Aria");
values
- the given values.this
assertion object.NullPointerException
- if the given argument is null
.IllegalArgumentException
- if the given argument is an empty array.AssertionError
- if the actual group is null
.AssertionError
- if the actual group does not contain the given values, i.e. the actual group contains some
or none of the given values, or the actual group contains more than once these values.S containsExactly(T... values)
HashSet
, prefer 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);
values
- the given values.this
assertion object.NullPointerException
- if the given argument is null
.AssertionError
- if the actual group is null
.AssertionError
- if the actual group does not contain the given values with same order, i.e. the actual group
contains some or none of the given values, or the actual group contains more values than the given ones
or values are the same but the order is not.S containsSequence(T... sequence)
Use 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);
assertThat(elvesRings).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);
sequence
- the sequence of objects to look for.AssertionError
- if the actual group is null
.AssertionError
- if the given array is null
.AssertionError
- if the actual group does not contain the given sequence.S containsSubsequence(T... sequence)
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);
assertThat(elvesRings).containsSubsequence(vilya, narya);
// assertion will fail
assertThat(elvesRings).containsSubsequence(nenya, vilya);
sequence
- the sequence of objects to look for.AssertionError
- if the actual group is null
.AssertionError
- if the given array is null
.AssertionError
- if the actual group does not contain the given subsequence.S doesNotContain(T... values)
Example :
// an Iterable is used in the example but it would also work with an array
Iterable<String> abc = newArrayList("a", "b", "c");
// assertion will pass
assertThat(abc).doesNotContain("d", "e");
values
- the given values.this
assertion object.NullPointerException
- if the given argument is null
.IllegalArgumentException
- if the given argument is an empty array.AssertionError
- if the actual group is null
.AssertionError
- if the actual group contains any of the given values.S doesNotHaveDuplicates()
Example :
// an Iterable is used in the example but it would also work with an array
Iterable<String> abc = newArrayList("a", "b", "c");
Iterable<String> lotsOfAs = newArrayList("a", "a", "a");
// assertion will pass
assertThat(abc).doesNotHaveDuplicates();
// assertion will fail
assertThat(lotsOfAs).doesNotHaveDuplicates();
this
assertion object.AssertionError
- if the actual group is null
.AssertionError
- if the actual group contains duplicates.S startsWith(T... sequence)
containsSequence(Object...)
, but it also verifies that the first element in the
sequence is also first element of the actual group.
Example :
// an Iterable is used in the example but it would also work with an array
Iterable<String> abc = newArrayList("a", "b", "c");
// assertion will pass
assertThat(abc).startsWith("a", "b");
// assertion will fail
assertThat(abc).startsWith("c");
sequence
- the sequence of objects to look for.NullPointerException
- if the given argument is null
.IllegalArgumentException
- if the given argument is an empty array.AssertionError
- if the actual group is null
.AssertionError
- if the actual group does not start with the given sequence of objects.S endsWith(T... sequence)
containsSequence(Object...)
, but it also verifies that the last element in the
sequence is also last element of the actual group.
Example :
// an Iterable is used in the example but it would also work with an array
Iterable<String> abc = newArrayList("a", "b", "c");
// assertion will pass
assertThat(abc).endsWith("b", "c");
// assertion will fail
assertThat(abc).endsWith("a");
sequence
- the sequence of objects to look for.NullPointerException
- if the given argument is null
.IllegalArgumentException
- if the given argument is an empty array.AssertionError
- if the actual group is null
.AssertionError
- if the actual group does not end with the given sequence of objects.S containsNull()
Example :
Iterable<String> abc = newArrayList("a", "b", "c");
Iterable<String> abNull = newArrayList("a", "b", null);
// assertion will pass
assertThat(abNull).containsNull();
// assertion will fail
assertThat(abc).containsNull();
this
assertion object.AssertionError
- if the actual group is null
.AssertionError
- if the actual group does not contain a null element.S doesNotContainNull()
Example :
Iterable<String> abc = newArrayList("a", "b", "c");
Iterable<String> abNull = newArrayList("a", "b", null);
// assertion will pass
assertThat(abc).doesNotContainNull();
// assertion will fail
assertThat(abNull).doesNotContainNull();
this
assertion object.AssertionError
- if the actual group is null
.AssertionError
- if the actual group contains a null element.S are(Condition<? super T> condition)
Example :
Iterable<String> abc = newArrayList("a", "b", "c");
Iterable<String> abcc = newArrayList("a", "b", "cc");
Condition<String> singleCharacterString
= new Condition<>(s -> s.length() == 1, "single character String");
// assertion will pass
assertThat(abc).are(singleCharacterString);
// assertion will fail
assertThat(abcc).are(singleCharacterString);
condition
- the given condition.this
object.NullPointerException
- if the given condition is null
.AssertionError
- if an element cannot be cast to T.AssertionError
- if one or more elements don't satisfy the given condition.S areNot(Condition<? super T> condition)
Example :
Iterable<String> abc = newArrayList("a", "b", "c");
Iterable<String> abcc = newArrayList("a", "b", "cc");
Condition<String> moreThanOneCharacter =
= new Condition<>(s -> s.length() > 1, "more than one character");
// assertion will pass
assertThat(abc).areNot(moreThanOneCharacter);
// assertion will fail
assertThat(abcc).areNot(moreThanOneCharacter);
condition
- the given condition.this
object.NullPointerException
- if the given condition is null
.AssertionError
- if an element cannot be cast to T.AssertionError
- if one or more elements satisfy the given condition.S have(Condition<? super T> condition)
Example :
Iterable<String> abc = newArrayList("a", "b", "c");
Iterable<String> abcc = newArrayList("a", "b", "cc");
Condition<String> onlyOneCharacter =
= new Condition<>(s -> s.length() == 1, "only one character");
// assertion will pass
assertThat(abc).have(onlyOneCharacter);
// assertion will fail
assertThat(abcc).have(onlyOneCharacter);
condition
- the given condition.this
object.NullPointerException
- if the given condition is null
.AssertionError
- if an element cannot be cast to T.AssertionError
- if one or more elements do not satisfy the given condition.S doNotHave(Condition<? super T> condition)
Example :
Iterable<String> abc = newArrayList("a", "b", "c");
Iterable<String> abcc = newArrayList("a", "b", "cc");
Condition<String> moreThanOneCharacter =
= new Condition<>(s -> s.length() > 1, "more than one character");
// assertion will pass
assertThat(abc).doNotHave(moreThanOneCharacter);
// assertion will fail
assertThat(abcc).doNotHave(moreThanOneCharacter);
condition
- the given condition.this
object.NullPointerException
- if the given condition is null
.AssertionError
- if an element cannot be cast to T.AssertionError
- if one or more elements satisfy the given condition.S areAtLeast(int n, Condition<? super T> condition)
Example :
Iterable<Integer> oneTwoThree = newArrayList(1, 2, 3);
Condition<Integer> oddNumber = new Condition<>(value % 2 == 1, "odd number");
// assertion will pass
oneTwoThree.areAtLeast(2, oddNumber);
// assertion will fail
oneTwoThree.areAtLeast(3, oddNumber);
n
- the minimum number of times the condition should be verified.condition
- the given condition.this
object.NullPointerException
- if the given condition is null
.AssertionError
- if an element can not be cast to T.AssertionError
- if the number of elements satisfying the given condition is < n.S areAtLeastOne(Condition<? super T> condition)
areAtLeast(1, condition)
.
Example:
// jedi is a Condition<String>
assertThat(newLinkedHashSet("Luke", "Solo", "Leia")).areAtLeastOne(jedi);
haveAtLeast(int, Condition)
S areAtMost(int n, Condition<? super T> condition)
Example :
Iterable<Integer> oneTwoThree = newArrayList(1, 2, 3);
Condition<Integer> oddNumber = new Condition<>(value % 2 == 1, "odd number");
// assertions will pass
oneTwoThree.areAtMost(2, oddNumber);
oneTwoThree.areAtMost(3, oddNumber);
// assertion will fail
oneTwoThree.areAtMost(1, oddNumber);
n
- the number of times the condition should be at most verified.condition
- the given condition.this
object.NullPointerException
- if the given condition is null
.AssertionError
- if an element cannot be cast to T.AssertionError
- if the number of elements satisfying the given condition is > n.S areExactly(int n, Condition<? super T> condition)
Example :
Iterable<Integer> oneTwoThree = newArrayList(1, 2, 3);
Condition<Integer> oddNumber = new Condition<>(value % 2 == 1, "odd number");
// assertion will pass
oneTwoThree.areExactly(2, oddNumber);
// assertions will fail
oneTwoThree.areExactly(1, oddNumber);
oneTwoThree.areExactly(3, oddNumber);
n
- the exact number of times the condition should be verified.condition
- the given condition.this
object.NullPointerException
- if the given condition is null
.AssertionError
- if an element cannot be cast to T.AssertionError
- if the number of elements satisfying the given condition is ≠ n.S haveAtLeastOne(Condition<? super T> condition)
haveAtLeast(1, condition)
.
Example:
Iterable<BasketBallPlayer> bullsPlayers = newArrayList(butler, rose);
// potentialMvp is a Condition<BasketBallPlayer>
assertThat(bullsPlayers).haveAtLeastOne(potentialMvp);
haveAtLeast(int, Condition)
S haveAtLeast(int n, Condition<? super T> condition)
Example :
Iterable<Integer> oneTwoThree = newArrayList(1, 2, 3);
Condition<Integer> oddNumber = new Condition<>(value % 2 == 1, "odd number");
// assertion will pass
oneTwoThree.haveAtLeast(2, oddNumber);
// assertion will fail
oneTwoThree.haveAtLeast(3, oddNumber);
This method is an alias for areAtLeast(int, Condition)
.S haveAtMost(int n, Condition<? super T> condition)
Example :
Iterable<Integer> oneTwoThree = newArrayList(1, 2, 3);
Condition<Integer> oddNumber = new Condition<>(value % 2 == 1, "odd number");
// assertions will pass
oneTwoThree.haveAtMost(2, oddNumber);
oneTwoThree.haveAtMost(3, oddNumber);
// assertion will fail
oneTwoThree.haveAtMost(1, oddNumber);
This method is an alias areAtMost(int, Condition)
.S haveExactly(int n, Condition<? super T> condition)
Example :
Iterable<Integer> oneTwoThree = newArrayList(1, 2, 3);
Condition<Integer> oddNumber = new Condition<>(value % 2 == 1, "odd number");
// assertion will pass
oneTwoThree.haveExactly(2, oddNumber);
// assertions will fail
oneTwoThree.haveExactly(1, oddNumber);
oneTwoThree.haveExactly(3, oddNumber);
This method is an alias areExactly(int, Condition)
.S containsAll(Iterable<? extends T> iterable)
Iterable
, in any order.
Example :
// an Iterable is used in the example but it would also work with an array
Iterable<String> abc = newArrayList("a", "b", "c");
Iterable<String> cb = newArrayList("c", "b");
// assertion will pass
assertThat(abc).containsAll(cb);
iterable
- the given Iterable
we will get elements from.this
assertion object.NullPointerException
- if the given argument is null
.AssertionError
- if the actual group is null
.AssertionError
- if the actual group does not contain all the elements of given Iterable
.S hasAtLeastOneElementOfType(Class<?> expectedType)
Object
group belong to the specified type (matching
includes subclasses of the given type).
Example:
Number[] numbers = { 2, 6L, 8.0 };
// successful assertion:
assertThat(numbers).hasAtLeastOneElementOfType(Long.class);
// assertion failure:
assertThat(numbers).hasAtLeastOneElementOfType(Float.class);
expectedType
- the expected type.NullPointerException
- if the given type is null
.AssertionError
- if the actual Object
group does not have any elements of the given type.S hasOnlyElementsOfType(Class<?> expectedType)
Object
group belong to the specified type (matching includes
subclasses of the given type).
Example:
Number[] numbers = { 2, 6, 8 };
// successful assertion:
assertThat(numbers).hasOnlyElementsOfType(Integer.class);
// assertion failure:
assertThat(numbers).hasOnlyElementsOfType(Long.class);
expectedType
- the expected type.NullPointerException
- if the given type is null
.AssertionError
- if one element is not of the expected type.S containsExactlyElementsOf(Iterable<? extends T> iterable)
containsExactly(Object...)
but handle the Iterable
to array conversion : verifies that
actual contains all the elements of the given iterable and nothing else in the same order.
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).containsExactlyElementsOf(newLinkedList(vilya, nenya, narya));
// assertion will fail as actual and expected order differ
assertThat(elvesRings).containsExactlyElementsOf(newLinkedList(nenya, vilya, narya));
iterable
- the given Iterable
we will get elements from.S containsOnlyElementsOf(Iterable<? extends T> iterable)
containsOnly(Object[])
: verifies that actual contains all the elements of the given
iterable and nothing else, in any order.
Example :
// an Iterable is used in the example but it would also work with an array
Iterable<Ring> rings = newArrayList(nenya, vilya);
// assertion will pass
assertThat(rings).containsOnlyElementsOf(newLinkedList(nenya, vilya));
assertThat(rings).containsOnlyElementsOf(newLinkedList(nenya, nenya, vilya, vilya));
// assertion will fail as actual does not contain narya
assertThat(rings).containsOnlyElementsOf(newLinkedList(nenya, vilya, narya));
// assertion will fail as actual contains nenya
assertThat(rings).containsOnlyElementsOf(newLinkedList(vilya));
iterable
- the given Iterable
we will get elements from.S hasSameElementsAs(Iterable<? extends T> iterable)
containsOnlyElementsOf(Iterable)
: verifies that actual contains all the elements of the
given iterable 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);
// assertions will pass:
assertThat(elvesRings).hasSameElementsAs(newArrayList(nenya, narya, vilya));
assertThat(elvesRings).hasSameElementsAs(newArrayList(nenya, narya, vilya, nenya));
// assertions will fail:
assertThat(elvesRings).hasSameElementsAs(newArrayList(nenya, narya));
assertThat(elvesRings).hasSameElementsAs(newArrayList(nenya, narya, vilya, oneRing));
iterable
- the Iterable whose elements we expect to be presentAssertionError
- if the actual group is null
NullPointerException
- if the given Iterable
is null
AssertionError
- if the actual Iterable
does not have the same elements, in any order, as the given
Iterable
S doesNotContainAnyElementsOf(Iterable<? extends T> iterable)
Iterable
(i.e. none).
Example:
Iterable<String> abc = newArrayList("a", "b", "c");
// These assertions succeed:
assertThat(actual).doesNotContainAnyElementsOf(newArrayList("d", "e"));
// These fail:
assertThat(actual).doesNotContainAnyElementsOf(newArrayList("d", "e", "a"));
iterable
- the Iterable
whose elements must not be in the actual group.this
assertion object.NullPointerException
- if the given argument is null
.IllegalArgumentException
- if the given argument is an empty iterable.AssertionError
- if the actual group is null
.AssertionError
- if the actual group contains some elements of the given Iterable
.S isSubsetOf(Iterable<? extends T> values)
Iterable
.
Example:
// an Iterable is used in the example but it would also work with an array
List<Ring> ringsOfPower = newArrayList(oneRing, vilya, nenya, narya, dwarfRing, manRing);
Iterable<Ring> elvesRings = newArrayList(vilya, nenya, narya);
// assertion will pass:
assertThat(elvesRings).isSubsetOf(ringsOfPower);
// assertion will fail:
assertThat(elvesRings).isSubsetOf(newArrayList(nenya, narya));
values
- the Iterable
that should contain all actual elements.AssertionError
- if the actual Iterable
is null
.NullPointerException
- if the given Iterable
is null
.AssertionError
- if the actual Iterable
is not subset of set Iterable
.S isSubsetOf(T... values)
// 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);
assertThat(elvesRings).isSubsetOf(vilya, nenya, narya, dwarfRing);
// assertions will fail:
assertThat(elvesRings).isSubsetOf(vilya, nenya);
assertThat(elvesRings).isSubsetOf(vilya, nenya, dwarfRing);
values
- the values that should be used for checking the elements of actual.AssertionError
- if the actual Iterable
is null
.AssertionError
- if the actual Iterable
is not subset of the given values.S allMatch(java.util.function.Predicate<? super T> predicate)
Predicate
.
Example :
Iterable<String> abc = newArrayList("a", "b", "c");
Iterable<String> abcc = newArrayList("a", "b", "cc");
// assertion will pass
assertThat(abc).allMatch(s -> s.length() == 1);
// assertion will fail
assertThat(abcc).allMatch(s -> s.length() == 1);
Note that you can achieve the same result with are(Condition)
or have(Condition)
.predicate
- the given Predicate
.this
object.NullPointerException
- if the given predicate is null
.AssertionError
- if an element cannot be cast to T.AssertionError
- if one or more elements don't satisfy the given predicate.Copyright © 2014-2016 AssertJ. All Rights Reserved.