Interface ObjectEnumerableAssert<SELF extends ObjectEnumerableAssert<SELF,ELEMENT>,ELEMENT>
-
- Type Parameters:
SELF
- the "self" type of this assertion class. Please read "Emulating 'self types' using Java Generics to simplify fluent API implementation" for more details.ELEMENT
- the type of elements of the "actual" value.
- All Superinterfaces:
EnumerableAssert<SELF,ELEMENT>
- All Known Subinterfaces:
IndexedObjectEnumerableAssert<SELF,ELEMENT>
- All Known Implementing Classes:
AbstractIterableAssert
,AbstractListAssert
,AbstractObjectArrayAssert
,AtomicReferenceArrayAssert
,ClassBasedNavigableIterableAssert
,ClassBasedNavigableListAssert
,FactoryBasedNavigableIterableAssert
,FactoryBasedNavigableListAssert
,IterableAssert
,ListAssert
,ObjectArrayAssert
,ProxyableIterableAssert
,ProxyableListAssert
,ProxyableObjectArrayAssert
public interface ObjectEnumerableAssert<SELF extends ObjectEnumerableAssert<SELF,ELEMENT>,ELEMENT> extends EnumerableAssert<SELF,ELEMENT>
Assertions methods applicable to groups of objects (e.g. arrays or collections.)- Author:
- Yvonne Wang, Alex Ruiz, Nicolas François, Mikhail Mazursky, Joel Costigliola, Nicolas François, Florent Biville
-
-
Method Summary
All Methods Instance Methods Abstract Methods Deprecated Methods Modifier and Type Method Description SELF
allMatch(Predicate<? super ELEMENT> predicate)
Verifies that all the elements of actual match the givenPredicate
.SELF
allMatch(Predicate<? super ELEMENT> predicate, String predicateDescription)
Verifies that all the elements of actual match the givenPredicate
.SELF
allSatisfy(Consumer<? super ELEMENT> requirements)
Verifies that all the elements satisfy given requirements expressed as aConsumer
.SELF
anyMatch(Predicate<? super ELEMENT> predicate)
Verifies whether any elements match the providedPredicate
.SELF
anySatisfy(Consumer<? super ELEMENT> requirements)
Verifies that at least one element satisfies the given requirements expressed as aConsumer
.SELF
are(Condition<? super ELEMENT> condition)
Verifies that each element value satisfies the given condition.SELF
areAtLeast(int n, Condition<? super ELEMENT> condition)
Verifies that there are at least n elements in the actual group satisfying the given condition.SELF
areAtLeastOne(Condition<? super ELEMENT> condition)
Verifies that there is at least one element in the actual group satisfying the given condition.SELF
areAtMost(int n, Condition<? super ELEMENT> condition)
Verifies that there are at most n elements in the actual group satisfying the given condition.SELF
areExactly(int n, Condition<? super ELEMENT> condition)
Verifies that there are exactly n elements in the actual group satisfying the given condition.SELF
areNot(Condition<? super ELEMENT> condition)
Verifies that each element value does not satisfy the given condition.SELF
contains(ELEMENT... values)
Verifies that the actual group contains the given values, in any order.SELF
containsAll(Iterable<? extends ELEMENT> iterable)
Verifies that the actual group contains all the elements of givenIterable
, in any order.SELF
containsAnyElementsOf(Iterable<? extends ELEMENT> iterable)
SELF
containsAnyOf(ELEMENT... values)
Verifies that the actualIterable
contains at least one of the given values.SELF
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.SELF
containsExactlyElementsOf(Iterable<? extends ELEMENT> iterable)
Same ascontainsExactly(Object...)
but handle theIterable
to array conversion : verifies that actual contains exactly the elements of the given iterable and nothing else in the same order.SELF
containsExactlyInAnyOrder(ELEMENT... values)
Verifies that the actual group contains exactly the given values and nothing else, in any order.SELF
containsExactlyInAnyOrderElementsOf(Iterable<? extends ELEMENT> values)
Verifies that the actual group contains exactly the given values and nothing else, in any order.SELF
containsNull()
Verifies that the actual group contains at least a null element.SELF
containsOnly(ELEMENT... values)
Verifies that the actual group contains only the given values and nothing else, in any order and ignoring duplicates (i.e.SELF
containsOnlyElementsOf(Iterable<? extends ELEMENT> iterable)
Deprecated.SELF
containsOnlyNulls()
Verifies that the actual group contains only null elements and nothing else.SELF
containsOnlyOnce(ELEMENT... values)
Verifies that the actual group contains the given values only once.SELF
containsOnlyOnceElementsOf(Iterable<? extends ELEMENT> iterable)
Same semantic ascontainsOnlyOnce(Object...)
: verifies that the actual group contains the elements of the given iterable only once.SELF
containsSequence(ELEMENT... sequence)
Verifies that the actual group contains the given sequence in the correct order and without extra values between the sequence values.SELF
containsSequence(Iterable<? extends ELEMENT> sequence)
Verifies that the actual group contains the given sequence in the correct order and without extra values between the sequence values.SELF
containsSubsequence(ELEMENT... sequence)
Verifies that the actual group contains the given subsequence in the correct order (possibly with other values between them).SELF
containsSubsequence(Iterable<? extends ELEMENT> sequence)
Verifies that the actual group contains the given subsequence in the correct order (possibly with other values between them).SELF
doesNotContain(ELEMENT... values)
Verifies that the actual group does not contain the given values.SELF
doesNotContainAnyElementsOf(Iterable<? extends ELEMENT> iterable)
Verifies that actual does not contain any elements of the givenIterable
(i.e.SELF
doesNotContainNull()
Verifies that the actual group does not contain null elements.SELF
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.SELF
doesNotContainSequence(Iterable<? extends 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.SELF
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.SELF
doesNotContainSubsequence(Iterable<? extends 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.SELF
doesNotHaveAnyElementsOfTypes(Class<?>... unexpectedTypes)
Verifies that all the elements in the actualObject
group do not belong to the specified types (including subclasses).SELF
doesNotHaveDuplicates()
Verifies that the actual group does not contain duplicates.SELF
doNotHave(Condition<? super ELEMENT> condition)
Verifies that all elements do not satisfy the given condition.SELF
endsWith(ELEMENT[] sequence)
Verifies that the actual group ends with the given sequence of objects, without any other objects between them.SELF
endsWith(ELEMENT first, ELEMENT... sequence)
Verifies that the actual group ends with the given sequence of objects, without any other objects between them.SELF
hasAtLeastOneElementOfType(Class<?> expectedType)
Verifies that at least one element in the actualObject
group has the specified type (matching includes subclasses of the given type).SELF
hasOnlyElementsOfType(Class<?> expectedType)
Verifies that all the elements in the actualObject
group belong to the specified type (matching includes subclasses of the given type).SELF
hasOnlyElementsOfTypes(Class<?>... types)
Verifies that all elements of the actual group are instances of the given types.SELF
hasOnlyOneElementSatisfying(Consumer<? super ELEMENT> elementAssertions)
Verifies that the unique element of theIterable
satisfies the given assertions expressed as aConsumer
, if it does not, only the first error is reported, useSoftAssertions
to get all the errors.SELF
hasSameElementsAs(Iterable<? extends ELEMENT> iterable)
Verifies that actual contains all the elements of the given iterable and nothing else, in any order and ignoring duplicates (i.e.SELF
have(Condition<? super ELEMENT> condition)
Verifies that all elements satisfy the given condition.SELF
haveAtLeast(int n, Condition<? super ELEMENT> condition)
Verifies that there are at least n elements in the actual group satisfying the given condition.SELF
haveAtLeastOne(Condition<? super ELEMENT> condition)
Verifies that there is at least one element in the actual group satisfying the given condition.SELF
haveAtMost(int n, Condition<? super ELEMENT> condition)
Verifies that there are at most n elements in the actual group satisfying the given condition.SELF
haveExactly(int n, Condition<? super ELEMENT> condition)
Verifies that there are exactly n elements in the actual group satisfying the given condition.SELF
isSubsetOf(ELEMENT... values)
Verifies that all the elements of actual are present in the given values.SELF
isSubsetOf(Iterable<? extends ELEMENT> values)
Verifies that all the elements of actual are present in the givenIterable
.SELF
noneMatch(Predicate<? super ELEMENT> predicate)
Verifies that no elements match the givenPredicate
.SELF
noneSatisfy(Consumer<? super ELEMENT> restrictions)
Verifies that no elements satisfy the given restrictions expressed as aConsumer
.SELF
startsWith(ELEMENT... sequence)
Verifies that the actual group starts with the given sequence of objects, without any other objects between them.-
Methods inherited from interface org.assertj.core.api.EnumerableAssert
hasSameSizeAs, hasSameSizeAs, hasSize, hasSizeBetween, hasSizeGreaterThan, hasSizeGreaterThanOrEqualTo, hasSizeLessThan, hasSizeLessThanOrEqualTo, isEmpty, isNotEmpty, isNullOrEmpty, usingDefaultElementComparator, usingElementComparator
-
-
-
-
Method Detail
-
contains
SELF contains(ELEMENT... values)
Verifies 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.- Parameters:
values
- the given values.- Returns:
this
assertion object.- Throws:
NullPointerException
- if the given argument isnull
.IllegalArgumentException
- if the given argument is an empty array.AssertionError
- if the actual group isnull
.AssertionError
- if the actual group does not contain the given values.
-
containsOnly
SELF containsOnly(ELEMENT... values)
Verifies 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
isSubsetOf(Object...)
.If you want to specify the elements to check with an
Iterable
, usecontainsOnlyElementsOf(Iterable)
instead.- Parameters:
values
- the given values.- Returns:
this
assertion object.- Throws:
NullPointerException
- if the given argument isnull
.IllegalArgumentException
- if the given argument is an empty array.AssertionError
- if the actual group isnull
.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.
-
containsOnlyNulls
SELF containsOnlyNulls()
Verifies that the actual group contains only null elements and nothing else.Example:
// assertion will pass Iterable<String> items = Arrays.asList(null, null, null); assertThat(items).containsOnlyNulls(); // assertion will fail because items2 contains a not null element Iterable<String> items2 = Arrays.asList(null, null, "notNull"); assertThat(items2).containsOnlyNulls(); // assertion will fail since an empty iterable does not contain any elements and therefore no null ones. Iterable<String> empty = new ArrayList<>(); assertThat(empty).containsOnlyNulls();
- Returns:
this
assertion object.- Throws:
AssertionError
- if the actual group isnull
.AssertionError
- if the actual group is empty or contains non null elements.- Since:
- 2.9.0 / 3.9.0
-
containsOnlyOnce
SELF containsOnlyOnce(ELEMENT... values)
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");
- Parameters:
values
- the given values.- Returns:
this
assertion object.- Throws:
NullPointerException
- if the given argument isnull
.IllegalArgumentException
- if the given argument is an empty array.AssertionError
- if the actual group isnull
.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.
-
containsExactly
SELF 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. don't use it withHashSet
, prefercontainsOnly(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.- Parameters:
values
- the given values.- Returns:
this
assertion object.- Throws:
NullPointerException
- if the given argument isnull
.AssertionError
- if the actual group isnull
.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.
-
containsExactlyInAnyOrder
SELF containsExactlyInAnyOrder(ELEMENT... values)
Verifies 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.- Parameters:
values
- the given values.- Returns:
this
assertion object.- Throws:
NullPointerException
- if the given argument isnull
.AssertionError
- if the actual group isnull
.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.
-
containsExactlyInAnyOrderElementsOf
SELF containsExactlyInAnyOrderElementsOf(Iterable<? extends ELEMENT> values)
Verifies 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); Iterable<Ring> elvesRingsSomeMissing = newArrayList(vilya, nenya, narya); Iterable<Ring> elvesRingsDifferentOrder = newArrayList(nenya, narya, vilya, vilya); // assertion will pass assertThat(elvesRings).containsExactlyInAnyOrder(elvesRingsDifferentOrder); // assertion will fail as vilya is contained twice in elvesRings. assertThat(elvesRings).containsExactlyInAnyOrder(elvesRingsSomeMissing);
If you want to directly specify the elements to check, use
containsExactlyInAnyOrder(Object...)
instead.- Parameters:
values
- the given values.- Returns:
this
assertion object.- Throws:
NullPointerException
- if the given argument isnull
.AssertionError
- if the actual group isnull
.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.- Since:
- 2.9.0 / 3.9.0
-
containsSequence
SELF containsSequence(ELEMENT... sequence)
Verifies that the actual group contains the given sequence in the correct order and without extra values between the sequence values.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) .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.- Parameters:
sequence
- the sequence of objects to look for.- Returns:
- this assertion object.
- Throws:
AssertionError
- if the actual group isnull
.AssertionError
- if the given array isnull
.AssertionError
- if the actual group does not contain the given sequence.
-
containsSequence
SELF containsSequence(Iterable<? extends ELEMENT> sequence)
Verifies that the actual group contains the given sequence in the correct order and without extra values between the sequence values.Use
containsSubsequence(Iterable)
to allow values between the expected sequence values.Example:
Iterable<Ring> elvesRings = newArrayList(vilya, nenya, narya); // assertions will pass assertThat(elvesRings).containsSequence(newArrayList(vilya, nenya)) .containsSequence(newArrayList(nenya, narya)); // assertions will fail, the elements order is correct but there is a value between them (nenya) assertThat(elvesRings).containsSequence(newArrayList(vilya, narya)); assertThat(elvesRings).containsSequence(newArrayList(nenya, vilya));
If you want to directly specify the elements of the sequence to check, use
containsSequence(Object...)
instead.- Parameters:
sequence
- the sequence of objects to look for.- Returns:
- this assertion object.
- Throws:
AssertionError
- if the actual group isnull
.AssertionError
- if the given array isnull
.AssertionError
- if the actual group does not contain the given sequence.
-
doesNotContainSequence
SELF 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.Use
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.- Parameters:
sequence
- the sequence of objects to look for.- Returns:
- this assertion object.
- Throws:
AssertionError
- if the actual group isnull
.AssertionError
- if the given array isnull
.AssertionError
- if the actual group contains the given sequence.
-
doesNotContainSequence
SELF doesNotContainSequence(Iterable<? extends 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.Use
doesNotContainSubsequence(Iterable)
to also ensure the sequence does not exist with values between the 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(newArrayList(vilya, narya)) .doesNotContainSequence(newArrayList(nenya, vilya)); // assertions will fail assertThat(elvesRings).doesNotContainSequence(newArrayList(vilya, nenya)); assertThat(elvesRings).doesNotContainSequence(newArrayList(nenya, narya));
If you want to directly specify the elements of the sequence not to find, use
doesNotContainSequence(Object...)
instead.- Parameters:
sequence
- the sequence of objects to look for.- Returns:
- this assertion object.
- Throws:
AssertionError
- if the actual group isnull
.AssertionError
- if the given array isnull
.AssertionError
- if the actual group contains the given sequence.
-
containsSubsequence
SELF containsSubsequence(ELEMENT... sequence)
Verifies 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.- Parameters:
sequence
- the sequence of objects to look for.- Returns:
- this assertion object.
- Throws:
AssertionError
- if the actual group isnull
.AssertionError
- if the given array isnull
.AssertionError
- if the actual group does not contain the given subsequence.
-
containsSubsequence
SELF containsSubsequence(Iterable<? extends ELEMENT> sequence)
Verifies that the actual group contains the given subsequence in the correct order (possibly with other values between them).Example:
Iterable<Ring> elvesRings = newArrayList(vilya, nenya, narya); // assertions will pass assertThat(elvesRings).containsSubsequence(newArrayList(vilya, nenya)) .containsSubsequence(newArrayList(vilya, narya)); // assertion will fail assertThat(elvesRings).containsSubsequence(newArrayList(nenya, vilya));
If you want to directly specify the subsequence to check, use
containsSubsequence(Object...)
instead.- Parameters:
sequence
- the sequence of objects to look for.- Returns:
- this assertion object.
- Throws:
AssertionError
- if the actual group isnull
.AssertionError
- if the given array isnull
.AssertionError
- if the actual group does not contain the given subsequence.
-
doesNotContainSubsequence
SELF 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.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.- Parameters:
sequence
- the sequence of objects to look for.- Returns:
- this assertion object.
- Throws:
AssertionError
- if the actual group isnull
.AssertionError
- if the given array isnull
.AssertionError
- if the actual group contains the given subsequence.
-
doesNotContainSubsequence
SELF doesNotContainSubsequence(Iterable<? extends 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.Example:
Iterable<Ring> elvesRings = newArrayList(vilya, nenya, narya); // assertions will pass assertThat(elvesRings).doesNotContainSubsequence(newArrayList(nenya, vilya)); .doesNotContainSubsequence(newArrayList(narya, vilya)); // assertion will fail assertThat(elvesRings).doesNotContainSubsequence(newArrayList(vilya, nenya)); assertThat(elvesRings).doesNotContainSubsequence(newArrayList(vilya, narya));
If you want to directly specify the elements of the subsequence not to find, use
doesNotContainSubsequence(Object...)
instead.- Parameters:
sequence
- the sequence of objects to look for.- Returns:
- this assertion object.
- Throws:
AssertionError
- if the actual group isnull
.AssertionError
- if the given array isnull
.AssertionError
- if the actual group contains the given subsequence.
-
doesNotContain
SELF doesNotContain(ELEMENT... values)
Verifies 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.- Parameters:
values
- the given values.- Returns:
this
assertion object.- Throws:
NullPointerException
- if the given argument isnull
.IllegalArgumentException
- if the given argument is an empty array.AssertionError
- if the actual group isnull
.AssertionError
- if the actual group contains any of the given values.
-
doesNotHaveDuplicates
SELF doesNotHaveDuplicates()
Verifies that the actual group does not contain duplicates.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();
- Returns:
this
assertion object.- Throws:
AssertionError
- if the actual group isnull
.AssertionError
- if the actual group contains duplicates.
-
startsWith
SELF startsWith(ELEMENT... sequence)
Verifies that the actual group starts with the given sequence of objects, without any other objects between them. Similar to
, but it also verifies that the first element in the sequence is also first element of the actual group.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).startsWith("a") .startsWith("a", "b"); // assertion will fail assertThat(abc).startsWith("c");
- Parameters:
sequence
- the sequence of objects to look for.- Returns:
- this assertion object.
- Throws:
NullPointerException
- if the given argument isnull
.IllegalArgumentException
- if the given argument is an empty array.AssertionError
- if the actual group isnull
.AssertionError
- if the actual group does not start with the given sequence of objects.
-
endsWith
SELF endsWith(ELEMENT first, ELEMENT... sequence)
Verifies 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.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");
- Parameters:
first
- the first element of the sequence of objects to look for.sequence
- the rest of the sequence of objects to look for.- Returns:
- this assertion object.
- Throws:
NullPointerException
- if the given argument isnull
.IllegalArgumentException
- if the given argument is an empty array.AssertionError
- if the actual group isnull
.AssertionError
- if the actual group does not end with the given sequence of objects.
-
endsWith
SELF endsWith(ELEMENT[] sequence)
Verifies 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.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(new String[0]) .endsWith(new String[] {"c"}) .endsWith(new String[] {"b", "c"}); // assertions will fail assertThat(abc).endsWith(new String[] {"a"}); assertThat(abc).endsWith(new String[] {"a", "b"});
- Parameters:
sequence
- the sequence of objects to look for.- Returns:
- this assertion object.
- Throws:
NullPointerException
- if the given argument isnull
.AssertionError
- if the actual group isnull
.AssertionError
- if the actual group does not end with the given sequence of objects.
-
containsNull
SELF containsNull()
Verifies that the actual group contains at least a null element.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();
- Returns:
this
assertion object.- Throws:
AssertionError
- if the actual group isnull
.AssertionError
- if the actual group does not contain a null element.
-
doesNotContainNull
SELF doesNotContainNull()
Verifies that the actual group does not contain null elements.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();
- Returns:
this
assertion object.- Throws:
AssertionError
- if the actual group isnull
.AssertionError
- if the actual group contains a null element.
-
are
SELF are(Condition<? super ELEMENT> condition)
Verifies that each element value satisfies the given 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);
- Parameters:
condition
- the given condition.- Returns:
this
object.- Throws:
NullPointerException
- if the given condition isnull
.AssertionError
- if an element cannot be cast to T.AssertionError
- if one or more elements do not satisfy the given condition.
-
areNot
SELF areNot(Condition<? super ELEMENT> condition)
Verifies that each element value does not satisfy the given 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);
- Parameters:
condition
- the given condition.- Returns:
this
object.- Throws:
NullPointerException
- if the given condition isnull
.AssertionError
- if an element cannot be cast to T.AssertionError
- if one or more elements satisfy the given condition.
-
have
SELF have(Condition<? super ELEMENT> condition)
Verifies that all elements satisfy the given 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);
- Parameters:
condition
- the given condition.- Returns:
this
object.- Throws:
NullPointerException
- if the given condition isnull
.AssertionError
- if an element cannot be cast to T.AssertionError
- if one or more elements do not satisfy the given condition.
-
doNotHave
SELF doNotHave(Condition<? super ELEMENT> condition)
Verifies that all elements do not satisfy the given 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);
- Parameters:
condition
- the given condition.- Returns:
this
object.- Throws:
NullPointerException
- if the given condition isnull
.AssertionError
- if an element cannot be cast to T.AssertionError
- if one or more elements satisfy the given condition.
-
areAtLeast
SELF areAtLeast(int n, Condition<? super ELEMENT> condition)
Verifies that there are at least n elements in the actual group satisfying the given 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);
- Parameters:
n
- the minimum number of times the condition should be verified.condition
- the given condition.- Returns:
this
object.- Throws:
NullPointerException
- if the given condition isnull
.AssertionError
- if an element can not be cast to T.AssertionError
- if the number of elements satisfying the given condition is < n.
-
areAtLeastOne
SELF areAtLeastOne(Condition<? super ELEMENT> condition)
Verifies that there is at least one element in the actual group satisfying the given condition.This method is an alias for
areAtLeast(1, condition)
.Example:
// jedi is a Condition<String> assertThat(newLinkedHashSet("Luke", "Solo", "Leia")).areAtLeastOne(jedi);
- Parameters:
condition
- the given condition.- Returns:
this
assertion object.- See Also:
haveAtLeast(int, Condition)
-
areAtMost
SELF areAtMost(int n, Condition<? super ELEMENT> condition)
Verifies that there are at most n elements in the actual group satisfying the given 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) .areAtMost(3, oddNumber); // assertion will fail oneTwoThree.areAtMost(1, oddNumber);
- Parameters:
n
- the number of times the condition should be at most verified.condition
- the given condition.- Returns:
this
object.- Throws:
NullPointerException
- if the given condition isnull
.AssertionError
- if an element cannot be cast to T.AssertionError
- if the number of elements satisfying the given condition is > n.
-
areExactly
SELF areExactly(int n, Condition<? super ELEMENT> condition)
Verifies that there are exactly n elements in the actual group satisfying the given 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);
- Parameters:
n
- the exact number of times the condition should be verified.condition
- the given condition.- Returns:
this
object.- Throws:
NullPointerException
- if the given condition isnull
.AssertionError
- if an element cannot be cast to T.AssertionError
- if the number of elements satisfying the given condition is ≠ n.
-
haveAtLeastOne
SELF haveAtLeastOne(Condition<? super ELEMENT> condition)
Verifies that there is at least one element in the actual group satisfying the given condition.This method is an alias for
haveAtLeast(1, condition)
.Example:
Iterable<BasketBallPlayer> bullsPlayers = newArrayList(butler, rose); // potentialMvp is a Condition<BasketBallPlayer> assertThat(bullsPlayers).haveAtLeastOne(potentialMvp);
- Parameters:
condition
- the given condition.- Returns:
this
assertion object.- See Also:
haveAtLeast(int, Condition)
-
haveAtLeast
SELF haveAtLeast(int n, Condition<? super ELEMENT> condition)
Verifies that there are at least n elements in the actual group satisfying the given condition.Example:
This method is an alias forIterable<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);
areAtLeast(int, Condition)
.- Parameters:
n
- the minimum number of times the condition must hold.condition
- the given condition.- Returns:
this
assertion object.
-
haveAtMost
SELF haveAtMost(int n, Condition<? super ELEMENT> condition)
Verifies that there are at most n elements in the actual group satisfying the given condition.Example:
This method is an aliasIterable<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);
areAtMost(int, Condition)
.- Parameters:
n
- the maximum number of times the condition must hold.condition
- the given condition.- Returns:
this
assertion object.
-
haveExactly
SELF haveExactly(int n, Condition<? super ELEMENT> condition)
Verifies that there are exactly n elements in the actual group satisfying the given condition.Example:
This method is an aliasIterable<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);
areExactly(int, Condition)
.- Parameters:
n
- the exact number of times the condition must hold.condition
- the given condition.- Returns:
this
assertion object.
-
containsAll
SELF containsAll(Iterable<? extends ELEMENT> iterable)
Verifies that the actual group contains all the elements of givenIterable
, in any order.Example:
Iterable<String> abc = Arrays.asList("a", "b", "c"); // assertions will pass assertThat(abc).containsAll(Arrays.asList("b", "c")) .containsAll(Arrays.asList("a", "b", "c")); // assertions will fail assertThat(abc).containsAll(Arrays.asList("d")); assertThat(abc).containsAll(Arrays.asList("a", "b", "c", "d"));
If you want to directly specify the elements to check, use
contains(Object...)
instead.- Parameters:
iterable
- the givenIterable
we will get elements from.- Returns:
this
assertion object.- Throws:
NullPointerException
- if the given argument isnull
.AssertionError
- if the actual group isnull
.AssertionError
- if the actual group does not contain all the elements of givenIterable
.
-
hasOnlyOneElementSatisfying
SELF hasOnlyOneElementSatisfying(Consumer<? super ELEMENT> elementAssertions)
Verifies that the unique element of theIterable
satisfies the given assertions expressed as aConsumer
, if it does not, only the first error is reported, useSoftAssertions
to get all the errors.Example:
List<Jedi> jedis = asList(new Jedi("Yoda", "red")); // assertions will pass assertThat(jedis).hasOnlyOneElementSatisfying(yoda -> assertThat(yoda.getName()).startsWith("Y")); assertThat(jedis).hasOnlyOneElementSatisfying(yoda -> { assertThat(yoda.getName()).isEqualTo("Yoda"); assertThat(yoda.getLightSaberColor()).isEqualTo("red"); }); // assertions will fail assertThat(jedis).hasOnlyOneElementSatisfying(yoda -> assertThat(yoda.getName()).startsWith("Vad")); // fail as one the assertions is not satisfied assertThat(jedis).hasOnlyOneElementSatisfying(yoda -> { assertThat(yoda.getName()).isEqualTo("Yoda"); assertThat(yoda.getLightSaberColor()).isEqualTo("purple"); }); // fail but only report the first error assertThat(jedis).hasOnlyOneElementSatisfying(yoda -> { assertThat(yoda.getName()).isEqualTo("Luke"); assertThat(yoda.getLightSaberColor()).isEqualTo("green"); }); // fail and reports the errors thanks to Soft assertions assertThat(jedis).hasOnlyOneElementSatisfying(yoda -> { SoftAssertions softly = new SoftAssertions(); softly.assertThat(yoda.getName()).isEqualTo("Luke"); softly.assertThat(yoda.getLightSaberColor()).isEqualTo("green"); softly.assertAll(); }); // even if the assertion is correct, there are too many jedis ! jedis.add(new Jedi("Luke", "green")); assertThat(jedis).hasOnlyOneElementSatisfying(yoda -> assertThat(yoda.getName()).startsWith("Yo"));
- Parameters:
elementAssertions
- the assertions to perform on the unique element.- Returns:
this
assertion object.- Throws:
AssertionError
- if theIterable
does not have a unique element.AssertionError
- if theIterable
's unique element does not satisfies the given assertions.- Since:
- 3.5.0
-
hasOnlyElementsOfTypes
SELF hasOnlyElementsOfTypes(Class<?>... types)
Verifies that all elements of the actual group are instances of the given types.Example:
Iterable<? extends Object> objects = Arrays.asList("foo", new StringBuilder()); // assertions will pass assertThat(objects).hasOnlyElementsOfTypes(CharSequence.class) .hasOnlyElementsOfTypes(String.class, StringBuilder.class); // assertions will fail assertThat(objects).hasOnlyElementsOfTypes(Number.class); assertThat(objects).hasOnlyElementsOfTypes(String.class, Number.class); assertThat(objects).hasOnlyElementsOfTypes(String.class);
- Parameters:
types
- the expected classes and interfaces- Returns:
this
assertion object.- Throws:
NullPointerException
- if the given argument isnull
.AssertionError
- if the actual group isnull
.AssertionError
- if not all elements of the actual group are instances of one of the given types- Since:
- 2.7.0 / 3.7.0
-
hasAtLeastOneElementOfType
SELF hasAtLeastOneElementOfType(Class<?> expectedType)
Verifies that at least one element in the actualObject
group has 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);
- Parameters:
expectedType
- the expected type.- Returns:
- this assertion object.
- Throws:
NullPointerException
- if the given type isnull
.AssertionError
- if the actualObject
group does not have any elements of the given type.
-
hasOnlyElementsOfType
SELF hasOnlyElementsOfType(Class<?> expectedType)
Verifies that all the elements in the actualObject
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);
- Parameters:
expectedType
- the expected type.- Returns:
- this assertion object.
- Throws:
NullPointerException
- if the given type isnull
.AssertionError
- if one element is not of the expected type.
-
doesNotHaveAnyElementsOfTypes
SELF doesNotHaveAnyElementsOfTypes(Class<?>... unexpectedTypes)
Verifies that all the elements in the actualObject
group do not belong to the specified types (including subclasses).Example:
Number[] numbers = { 2, 6, 8.0 }; // successful assertion: assertThat(numbers).doesNotHaveAnyElementsOfTypes(Long.class, Float.class); // assertion failure: assertThat(numbers).doesNotHaveAnyElementsOfTypes(Long.class, Integer.class);
- Parameters:
unexpectedTypes
- the not expected types.- Returns:
- this assertion object.
- Throws:
NullPointerException
- if the given type isnull
.AssertionError
- if one element's type matches the given types.- Since:
- 2.9.0 / 3.9.0
-
containsExactlyElementsOf
SELF containsExactlyElementsOf(Iterable<? extends ELEMENT> iterable)
Same ascontainsExactly(Object...)
but handle theIterable
to array conversion : verifies that actual contains exactly the elements of the given iterable and nothing else in the same order.Example:
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));
If you want to directly specify the elements to check, use
containsExactly(Object...)
instead.- Parameters:
iterable
- the givenIterable
we will get elements from.- Returns:
this
assertion object.
-
containsOnlyElementsOf
@Deprecated SELF containsOnlyElementsOf(Iterable<? extends ELEMENT> iterable)
Deprecated.Same semantic ascontainsOnly(Object[])
: verifies that actual contains all the elements of the given iterable and nothing else, in any order and ignoring duplicates (i.e. once a value is found, its duplicates are also considered found).This assertion has been deprecated because its name is confusing, users were expecting it to behave like
isSubsetOf(Iterable)
.For example this assertion fails when users expected it to pass:
Iterable<Ring> rings = list(nenya, vilya); // assertion fails because narya is not in rings, confusing! assertThat(rings).containsOnlyElementsOf(list(nenya, vilya, narya));
Use
isSubsetOf(Iterable)
to check that actual is a subset of given iterable, or if you need to same assertion semantics usehasSameElementsAs(Iterable)
.Examples:
Iterable<Ring> rings = newArrayList(nenya, vilya); // assertion will pass assertThat(rings).containsOnlyElementsOf(list(nenya, vilya)) .containsOnlyElementsOf(list(nenya, nenya, vilya, vilya)); assertThat(list(nenya, nenya, vilya, vilya)).containsOnlyElementsOf(rings); // assertion will fail as actual does not contain narya assertThat(rings).containsOnlyElementsOf(list(nenya, vilya, narya)); // assertion will fail as actual contains nenya assertThat(rings).containsOnlyElementsOf(list(vilya));
If you want to directly specify the elements to check, use
containsOnly(Object...)
instead.- Parameters:
iterable
- the givenIterable
we will get elements from.- Returns:
this
assertion object.
-
containsOnlyOnceElementsOf
SELF containsOnlyOnceElementsOf(Iterable<? extends ELEMENT> iterable)
Same semantic ascontainsOnlyOnce(Object...)
: verifies that the actual group contains the elements of the given iterable only once.Examples :
// assertions will pass assertThat(list("winter", "is", "coming")).containsOnlyOnceElementsOf(list("winter")) .containsOnlyOnceElementsOf(list("coming", "winter")); // assertions will fail assertThat(list("winter", "is", "coming")).containsOnlyOnceElementsOf(list("Lannister")); assertThat(list("Arya", "Stark", "daughter", "of", "Ned", "Stark")).containsOnlyOnceElementsOf(list("Stark")); assertThat(list("Arya", "Stark", "daughter", "of", "Ned", "Stark")).containsOnlyOnceElementsOf(list("Stark", "Lannister", "Arya"));
If you want to directly specify the elements to check with, use
containsOnlyOnce(Object...)
instead.- Parameters:
iterable
- the givenIterable
we will get elements from.- Returns:
this
assertion object.- Since:
- 3.16.0
-
hasSameElementsAs
SELF hasSameElementsAs(Iterable<? extends ELEMENT> iterable)
Verifies that actual contains all the elements of the given iterable and nothing else, in any order and ignoring duplicates (i.e. once a value is found, its duplicates are also considered found).Example:
Iterable<Ring> elvesRings = newArrayList(vilya, nenya, narya); // assertions will pass: assertThat(elvesRings).hasSameElementsAs(newArrayList(nenya, narya, vilya)) .hasSameElementsAs(newArrayList(nenya, narya, vilya, nenya)); // assertions will fail: assertThat(elvesRings).hasSameElementsAs(newArrayList(nenya, narya)); assertThat(elvesRings).hasSameElementsAs(newArrayList(nenya, narya, vilya, oneRing));
- Parameters:
iterable
- the Iterable whose elements we expect to be present- Returns:
- this assertion object
- Throws:
AssertionError
- if the actual group isnull
NullPointerException
- if the givenIterable
isnull
AssertionError
- if the actualIterable
does not have the same elements, in any order, as the givenIterable
-
doesNotContainAnyElementsOf
SELF doesNotContainAnyElementsOf(Iterable<? extends ELEMENT> iterable)
Verifies that actual does not contain any elements of the givenIterable
(i.e. none).Example:
Iterable<String> abc = newArrayList("a", "b", "c"); // assertion succeeds: assertThat(actual).doesNotContainAnyElementsOf(newArrayList("d", "e")); // assertion fails: assertThat(actual).doesNotContainAnyElementsOf(newArrayList("d", "e", "a"));
If you want to directly specify the elements not to find, use
doesNotContain(Object...)
instead.- Parameters:
iterable
- theIterable
whose elements must not be in the actual group.- Returns:
this
assertion object.- Throws:
NullPointerException
- if the given argument isnull
.IllegalArgumentException
- if the given argument is an empty iterable.AssertionError
- if the actual group isnull
.AssertionError
- if the actual group contains some elements of the givenIterable
.
-
isSubsetOf
SELF isSubsetOf(Iterable<? extends ELEMENT> values)
Verifies that all the elements of actual are present in the givenIterable
.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));
If you want to directly specify the set of elements, use
isSubsetOf(Object...)
instead.- Parameters:
values
- theIterable
that should contain all actual elements.- Returns:
- this assertion object.
- Throws:
AssertionError
- if the actualIterable
isnull
.NullPointerException
- if the givenIterable
isnull
.AssertionError
- if the actualIterable
is not subset of setIterable
.
-
isSubsetOf
SELF isSubsetOf(ELEMENT... values)
Verifies 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.- Parameters:
values
- the values that should be used for checking the elements of actual.- Returns:
- this assertion object.
- Throws:
AssertionError
- if the actualIterable
isnull
.AssertionError
- if the actualIterable
is not subset of the given values.
-
allMatch
SELF allMatch(Predicate<? super ELEMENT> predicate)
Verifies that all the elements of actual match the givenPredicate
.Example :
Note that you can achieve the same result withIterable<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);
are(Condition)
orhave(Condition)
.- Parameters:
predicate
- the givenPredicate
.- Returns:
this
object.- Throws:
NullPointerException
- if the given predicate isnull
.AssertionError
- if an element cannot be cast to T.AssertionError
- if one or more elements don't satisfy the given predicate.
-
allMatch
SELF allMatch(Predicate<? super ELEMENT> predicate, String predicateDescription)
Verifies that all the elements of actual match the givenPredicate
. The predicate description is used to get an informative error message.Example :
The message of the failed assertion would be:Iterable<String> abc = newArrayList("a", "b", "c"); Iterable<String> abcc = newArrayList("a", "b", "cc"); // assertion will pass assertThat(abc).allMatch(s -> s.length() == 1, "length of 1"); // assertion will fail assertThat(abcc).allMatch(s -> s.length() == 1, "length of 1");
Expecting all elements of: <["a", "b", "cc"]> to match 'length of 1' predicate but this element did not: <"cc">
- Parameters:
predicate
- the givenPredicate
.predicateDescription
- a description of thePredicate
used in the error message- Returns:
this
object.- Throws:
NullPointerException
- if the given predicate isnull
.AssertionError
- if an element cannot be cast to T.AssertionError
- if one or more elements don't satisfy the given predicate.- Since:
- 3.6.0
-
allSatisfy
SELF allSatisfy(Consumer<? super ELEMENT> requirements)
Verifies that all the elements satisfy given requirements expressed as aConsumer
.This is useful to perform a group of assertions on elements.
Example:
assertThat(myIcelanderFriends).allSatisfy(person -> { assertThat(person.getCountry()).isEqualTo("Iceland"); assertThat(person.getPhoneCountryCode()).isEqualTo("+354"); });
If the actual iterable/array is empty, this assertion succeeds as there is no elements to check.
- Parameters:
requirements
- the givenConsumer
.- Returns:
this
object.- Throws:
NullPointerException
- if the givenConsumer
isnull
.AssertionError
- if one or more elements don't satisfy given requirements.- Since:
- 3.6.0
-
anyMatch
SELF anyMatch(Predicate<? super ELEMENT> predicate)
Verifies whether any elements match the providedPredicate
.Example :
Note that you can achieve the same result withIterable<String> abcc = newArrayList("a", "b", "cc"); // assertion will pass assertThat(abcc).anyMatch(s -> s.length() == 2); // assertion will fail assertThat(abcc).anyMatch(s -> s.length() > 2);
areAtLeastOne(Condition)
orhaveAtLeastOne(Condition)
.- Parameters:
predicate
- the givenPredicate
.- Returns:
this
object.- Throws:
NullPointerException
- if the given predicate isnull
.AssertionError
- if no elements satisfy the given predicate.- Since:
- 3.9.0
-
anySatisfy
SELF anySatisfy(Consumer<? super ELEMENT> requirements)
Verifies that at least one element satisfies the given requirements expressed as aConsumer
.This is useful to check that a group of assertions is verified by (at least) one element.
If the group of elements to assert is empty, the assertion will fail.
Example:
// assume that one icelander in myIcelanderFriends has a name finishing by 'son' assertThat(myIcelanderFriends).anySatisfy(person -> { assertThat(person.getCountry()).isEqualTo("Iceland"); assertThat(person.getSurname()).endsWith("son"); }); // assertion fails for empty group, whatever the requirements are. assertThat(emptyGroup).anySatisfy($ -> assertThat(true).isTrue());
- Parameters:
requirements
- the givenConsumer
.- Returns:
this
object.- Throws:
NullPointerException
- if the givenConsumer
isnull
.AssertionError
- none elements satisfy the given requirements.- Since:
- 3.7.0
-
noneSatisfy
SELF noneSatisfy(Consumer<? super ELEMENT> restrictions)
Verifies that no elements satisfy the given restrictions expressed as aConsumer
.Example:
Note that this assertion succeeds if the group (collection, array, ...) is empty whatever the restrictions are.// assume that all icelander in myIcelanderFriends are not from Brazil assertThat(myIcelanderFriends).noneSatisfy(person -> { assertThat(person.getCountry()).isEqualTo("Brazil"); });
- Parameters:
restrictions
- the given restrictions asConsumer
that no elements should met.- Returns:
this
object.- Throws:
NullPointerException
- if the givenConsumer
isnull
.AssertionError
- if one or more elements satisfy the given requirements.- Since:
- 3.10.0
-
containsAnyOf
SELF containsAnyOf(ELEMENT... values)
Verifies that the actualIterable
contains 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");
If you want to specify the elements to check with an
Iterable
, usecontainsAnyElementsOf(Iterable)
instead.- Parameters:
values
- the values whose at least one which is expected to be in theIterable
under test.- Returns:
this
assertion object.- Throws:
NullPointerException
- if the array of values isnull
.IllegalArgumentException
- if the array of values is empty and theIterable
under test is not empty.AssertionError
- if theIterable
under test isnull
.AssertionError
- if theIterable
under test does not contain any of the givenvalues
.- Since:
- 2.9.0 / 3.9.0
-
containsAnyElementsOf
SELF containsAnyElementsOf(Iterable<? extends ELEMENT> iterable)
Verifies that theIterable
under test contains at least one of the givenIterable
elements.Example:
Iterable<String> abc = Arrays.asList("a", "b", "c"); // assertions will pass assertThat(abc).containsAnyElementsOf(Arrays.asList("b")) .containsAnyElementsOf(Arrays.asList("b", "c")) .containsAnyElementsOf(Arrays.asList("a", "b", "c")) .containsAnyElementsOf(Arrays.asList("a", "b", "c", "d")) .containsAnyElementsOf(Arrays.asList("e", "f", "g", "b")); // assertions will fail assertThat(abc).containsAnyElementsOf(Arrays.asList("d")); assertThat(abc).containsAnyElementsOf(Arrays.asList("d", "e", "f", "g"));
If you want to directly specify the elements to check, use
containsAnyOf(Object...)
instead.- Parameters:
iterable
- the iterable whose at least one element is expected to be in theIterable
under test.- Returns:
this
assertion object.- Throws:
NullPointerException
- if the iterable of expected values isnull
.IllegalArgumentException
- if the iterable of expected values is empty and theIterable
under test is not empty.AssertionError
- if theIterable
under test isnull
.AssertionError
- if theIterable
under test does not contain any of elements from the givenIterable
.- Since:
- 2.9.0 / 3.9.0
-
noneMatch
SELF noneMatch(Predicate<? super ELEMENT> predicate)
Verifies that no elements match the givenPredicate
.Example :
Note that you can achieve the same result withIterable<String> abcc = newArrayList("a", "b", "cc"); // assertion will pass assertThat(abcc).noneMatch(s -> s.isEmpty()); // assertion will fail assertThat(abcc).noneMatch(s -> s.length() == 2);
areNot(Condition)
ordoNotHave(Condition)
.- Parameters:
predicate
- the givenPredicate
.- Returns:
this
object.- Throws:
NullPointerException
- if the given predicate isnull
.AssertionError
- if an element cannot be cast to T.AssertionError
- if any element satisfy the given predicate.- Since:
- 3.9.0
-
-