T
- the type of elements of the "actual" value.public abstract class AbstractObjectArrayAssert<S extends AbstractObjectArrayAssert<S,T>,T> extends AbstractAssert<S,T[]> implements IndexedObjectEnumerableAssert<AbstractObjectArrayAssert<S,T>,T>, ArraySortedAssert<AbstractObjectArrayAssert<S,T>,T>
To create an instance of this class, invoke
.
Assertions#assertThat(T[])
actual, info, myself
Modifier | Constructor and Description |
---|---|
protected |
AbstractObjectArrayAssert(T[] actual,
Class<?> selfType) |
Modifier and Type | Method and Description |
---|---|
S |
are(Condition<? super T> condition)
Verifies that each element value satisfies the given condition
|
S |
areAtLeast(int times,
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 array satisfying the given condition.
|
S |
areAtMost(int times,
Condition<? super T> condition)
Verifies that there is at most n elements in the actual group satisfying the given condition.
|
S |
areExactly(int times,
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 |
contains(T value,
Index index)
Verifies that the actual array contains the given object at the given index.
|
S |
containsAll(Iterable<? extends T> iterable)
Verifies that the actual array contains all the elements of given
Iterable , in any order. |
S |
containsExactly(T... values)
Verifies that the actual array contains only the given values and nothing else, in order.
|
S |
containsExactlyElementsOf(Iterable<? extends T> iterable)
Same as
containsExactly(Object...) but handle the Iterable to array conversion : verifies that
actual contains all elements of the given Iterable and nothing else in the same order. |
S |
containsExactlyInAnyOrder(T... values)
Verifies that the actual group contains exactly the given values and nothing else, in any order.
|
S |
containsNull()
Verifies that the actual array 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 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 array contains the given sequence in the correct order and without extra value between the sequence values.
|
S |
containsSubsequence(T... subsequence)
Verifies that the actual array contains the given subsequence in the correct order (possibly with other values between them).
|
S |
doesNotContain(T... values)
Verifies that the actual array does not contain the given values.
|
S |
doesNotContain(T value,
Index index)
Verifies that the actual array does not contain the given object at the given index.
|
S |
doesNotContainAnyElementsOf(Iterable<? extends T> iterable)
Verifies that the actual array does not contain any elements of the given
Iterable (i.e. |
S |
doesNotContainNull()
Verifies that the actual array does not contain null elements.
|
S |
doesNotHaveDuplicates()
Verifies that the actual array 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 array ends with the given sequence of objects, without any other objects between them.
|
<U> ObjectArrayAssert<U> |
extracting(Extractor<? super T,U> extractor)
Extract the values from the array's elements by applying an extracting function on them.
|
ObjectArrayAssert<Tuple> |
extracting(String... propertiesOrFields)
Extract the values of given fields/properties from the array's elements under test into a new array composed of
Tuple (a simple data structure), this new array becoming the array under test.
|
ObjectArrayAssert<Object> |
extracting(String fieldOrProperty)
Extract the values of given field or property from the array's elements under test into a new array, this new array
becoming the array under test.
|
<P> ObjectArrayAssert<P> |
extracting(String fieldOrProperty,
Class<P> extractingType)
Extract the values of given field or property from the array's elements under test into a new array, this new array
becoming the array under test with type.
|
ObjectArrayAssert<Object> |
extractingResultOf(String method)
Extract the result of given method invocation from the array's elements under test into a new array, this new array
becoming the array under test.
|
<P> ObjectArrayAssert<P> |
extractingResultOf(String method,
Class<P> extractingType)
Extract the result of given method invocation from the array's elements under test into a new array, this new array
becoming the array under test.
|
S |
filteredOn(Condition<? super T> condition)
Filter the array under test keeping only elements matching the given
Condition . |
S |
filteredOn(String propertyOrFieldName,
FilterOperator<?> filterOperator)
Filter the array under test keeping only elements having a property or field matching the filter expressed with
the
FilterOperator , the property/field is specified by propertyOrFieldName parameter. |
S |
filteredOn(String propertyOrFieldName,
Object expectedValue)
Filter the array under test keeping only elements having a property or field equal to
expectedValue , the
property/field is specified by propertyOrFieldName parameter. |
S |
filteredOnNull(String propertyOrFieldName)
Filter the array under test keeping only elements whose property or field specified by
propertyOrFieldName
is null. |
<U,C extends Collection<U>> |
flatExtracting(Extractor<? super T,C> extractor)
Extract the Iterable values from the array's elements by applying an Iterable extracting function on them
and concatenating the result lists into an array which becomes the new object under test.
|
ObjectArrayAssert<Object> |
flatExtracting(String propertyName)
Extract from array's elements the Iterable/Array values corresponding to the given property/field name and
concatenate them into a single array becoming the new object under test.
|
S |
hasAtLeastOneElementOfType(Class<?> type)
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<?> type)
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 elements of the
given Iterable and nothing else, in any order. |
S |
hasSameSizeAs(Iterable<?> other)
Verifies that the actual group has the same size as the given
Iterable . |
S |
hasSameSizeAs(Object other)
Verifies that the actual array has the same size as the given array.
|
S |
hasSize(int expected)
Verifies that the number of values in the actual group is equal to the given one.
|
S |
have(Condition<? super T> condition)
Verifies that all elements satisfy the given condition.
|
S |
haveAtLeast(int times,
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 times,
Condition<? super T> condition)
Verifies that there is at most n elements in the actual group satisfying the given condition.
|
S |
haveExactly(int times,
Condition<? super T> condition)
Verifies that there is exactly n elements in the actual group satisfying the given condition.
|
S |
inBinary()
Use binary object representation instead of standard representation in error messages.
|
S |
inHexadecimal()
Enable hexadecimal object representation of Iterable elements instead of standard java representation in error
messages.
|
void |
isEmpty()
Verifies that the actual group of values is empty.
|
S |
isNotEmpty()
Verifies that the actual group of values is not empty.
|
void |
isNullOrEmpty()
Verifies that the actual group of values is
null or empty. |
S |
isSorted()
Verifies that the actual array is sorted into ascending order according to the natural ordering of its elements.
|
S |
isSortedAccordingTo(Comparator<? super T> comparator)
Verifies that the actual array is sorted according to the given comparator. Empty arrays are considered sorted whatever
the comparator is. One element arrays are considered sorted if element is compatible with comparator, otherwise an
AssertionError is thrown.
|
S |
isSubsetOf(Iterable<? extends T> values)
Verifies that all elements of actual are present in the given
Iterable . |
S |
isSubsetOf(T... values)
Verifies that all elements of actual are present in the given values.
|
S |
startsWith(T... sequence)
Verifies that the actual array starts with the given sequence of objects, without any other objects between them.
|
S |
usingDefaultElementComparator()
Revert to standard comparison for incoming assertion group element checks.
|
S |
usingElementComparator(Comparator<? super T> elementComparator)
Use given custom comparator instead of relying on actual type A
equals method to compare group
elements for
incoming assertion checks. |
S |
usingElementComparatorIgnoringFields(String... fields)
Use field/property by field/property on all fields/properties except the given ones (including inherited
fields/properties)instead of relying on actual type A
equals method to compare group elements for
incoming assertion checks. |
S |
usingElementComparatorOnFields(String... fields)
Use field/property by field/property comparison on the given fields/properties only (including inherited
fields/properties)instead of relying on actual type A
equals method to compare group elements for
incoming assertion checks. |
S |
usingFieldByFieldElementComparator()
Use field/property by field/property comparison (including inherited fields/properties) instead of relying on
actual type A
equals method to compare group elements for incoming assertion checks. |
as, as, asList, asString, describedAs, describedAs, descriptionText, doesNotHave, doesNotHaveSameClassAs, equals, failWithMessage, getWritableAssertionInfo, has, hashCode, hasSameClassAs, hasToString, is, isEqualTo, isExactlyInstanceOf, isIn, isIn, isInstanceOf, isInstanceOfAny, isNot, isNotEqualTo, isNotExactlyInstanceOf, isNotIn, isNotIn, isNotInstanceOf, isNotInstanceOfAny, isNotNull, isNotOfAnyClassIn, isNotSameAs, isNull, isOfAnyClassIn, isSameAs, overridingErrorMessage, usingComparator, usingDefaultComparator, withFailMessage, withThreadDumpOnError
public void isNullOrEmpty()
null
or empty.
Example:
// assertions will pass
List<String> strings = new ArrayList<>();
assertThat(strings).isNullOrEmpty();
assertThat(new int[] { }).isNullOrEmpty();
// assertions will fail
assertThat(new String[] { "a", "b"}).isNullOrEmpty();
assertThat(Arrays.asList(1, 2, 3)).isNullOrEmpty();
isNullOrEmpty
in interface EnumerableAssert<AbstractObjectArrayAssert<S extends AbstractObjectArrayAssert<S,T>,T>,T>
AssertionError
- if the actual group of values is not null
or not empty.public void isEmpty()
Example:
// assertions will pass
assertThat(new ArrayList()).isEmpty();
assertThat(new int[] { }).isEmpty();
// assertions will fail
assertThat(new String[] { "a", "b" }).isEmpty();
assertThat(Arrays.asList(1, 2, 3)).isEmpty();
isEmpty
in interface EnumerableAssert<AbstractObjectArrayAssert<S extends AbstractObjectArrayAssert<S,T>,T>,T>
AssertionError
- if the actual group of values is not empty.public S isNotEmpty()
Example:
// assertions will pass
assertThat(new String[] { "a", "b" }).isNotEmpty();
assertThat(Arrays.asList(1, 2, 3)).isNotEmpty();
// assertions will fail
assertThat(new ArrayList()).isNotEmpty();
assertThat(new int[] { }).isNotEmpty();
isNotEmpty
in interface EnumerableAssert<AbstractObjectArrayAssert<S extends AbstractObjectArrayAssert<S,T>,T>,T>
this
assertion object.AssertionError
- if the actual group of values is empty.public S hasSize(int expected)
Example:
// assertions will pass
assertThat(new String[] { "a", "b" }).hasSize(2);
assertThat(Arrays.asList(1, 2, 3)).hasSize(3);
// assertions will fail
assertThat(new ArrayList()).hasSize(1);
assertThat(new int[] { 1, 2, 3 }).hasSize(2);
hasSize
in interface EnumerableAssert<AbstractObjectArrayAssert<S extends AbstractObjectArrayAssert<S,T>,T>,T>
expected
- the expected number of values in the actual group.this
assertion object.AssertionError
- if the number of values of the actual group is not equal to the given one.public S hasSameSizeAs(Object other)
Object[]
and primitive arrays (e.g. int[]
).
Example:
int[] oneTwoThree = {1, 2, 3};
int[] fourFiveSix = {4, 5, 6};
// assertions will pass
assertThat(oneTwoThree).hasSameSizeAs(fourFiveSix);
hasSameSizeAs
in interface EnumerableAssert<AbstractObjectArrayAssert<S extends AbstractObjectArrayAssert<S,T>,T>,T>
array
- the array to compare size with actual group.this
assertion object.AssertionError
- if the actual group is null
.AssertionError
- if the array parameter is null
or is not a true array.AssertionError
- if actual group and given array don't have the same size.public S hasSameSizeAs(Iterable<?> other)
Iterable
.
Example:
int[] oneTwoThree = {1, 2, 3};
Iterable<Ring> elvesRings = newArrayList(vilya, nenya, narya);
// assertions will pass
assertThat(oneTwoThree).hasSameSizeAs(elvesRings);
hasSameSizeAs
in interface EnumerableAssert<AbstractObjectArrayAssert<S extends AbstractObjectArrayAssert<S,T>,T>,T>
other
- the Iterable
to compare size with actual group.this
assertion object.AssertionError
- if the actual group is null
.AssertionError
- if the other Iterable
is null
.AssertionError
- if actual group and given Iterable
don't have the same size.public S contains(T... values)
Example :
String[] abc = {"a", "b", "c"};
// assertions will pass
assertThat(abc).contains("b", "a");
assertThat(abc).contains("b", "a", "b");
// assertion will fail
assertThat(abc).contains("d");
contains
in interface ObjectEnumerableAssert<AbstractObjectArrayAssert<S extends AbstractObjectArrayAssert<S,T>,T>,T>
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.public S containsOnly(T... values)
Example :
String[] abc = {"a", "b", "c"};
// assertion will pass
assertThat(abc).containsOnly("c", "b", "a");
// assertion will fail because "c" is missing
assertThat(abc).containsOnly("a", "b");
containsOnly
in interface ObjectEnumerableAssert<AbstractObjectArrayAssert<S extends AbstractObjectArrayAssert<S,T>,T>,T>
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.public S containsOnlyElementsOf(Iterable<? extends T> iterable)
containsOnly(Object[])
: verifies that actual contains all elements of the given
Iterable
and nothing else, in any order.
Example :
Ring[] rings = {nenya, vilya};
// assertion will pass
assertThat(rings).containsOnlyElementsOf(newArrayList(nenya, vilya));
assertThat(rings).containsOnlyElementsOf(newArrayList(nenya, nenya, vilya, vilya));
// assertion will fail as actual does not contain narya
assertThat(rings).containsOnlyElementsOf(newArrayList(nenya, vilya, narya));
// assertion will fail as actual contains nenya
assertThat(rings).containsOnlyElementsOf(newArrayList(vilya));
containsOnlyElementsOf
in interface ObjectEnumerableAssert<AbstractObjectArrayAssert<S extends AbstractObjectArrayAssert<S,T>,T>,T>
iterable
- the given Iterable
we will get elements from.public S hasSameElementsAs(Iterable<? extends T> iterable)
containsOnlyElementsOf(Iterable)
: verifies that actual contains all elements of the
given Iterable
and nothing else, in any order.
Example:
Ring[] elvesRings = {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));
hasSameElementsAs
in interface ObjectEnumerableAssert<AbstractObjectArrayAssert<S extends AbstractObjectArrayAssert<S,T>,T>,T>
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
public S containsOnlyOnce(T... values)
Examples :
// array if a factory method to create arrays.
// assertions will pass
assertThat(array("winter", "is", "coming")).containsOnlyOnce("winter");
assertThat(array("winter", "is", "coming")).containsOnlyOnce("coming", "winter");
// assertions will fail
assertThat(array("winter", "is", "coming")).containsOnlyOnce("Lannister");
assertThat(array("Aria", "Stark", "daughter", "of", "Ned", "Stark")).containsOnlyOnce("Stark");
assertThat(array("Aria", "Stark", "daughter", "of", "Ned", "Stark")).containsOnlyOnce("Stark", "Lannister", "Aria");
containsOnlyOnce
in interface ObjectEnumerableAssert<AbstractObjectArrayAssert<S extends AbstractObjectArrayAssert<S,T>,T>,T>
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.public S containsExactly(T... values)
Example :
Ring[] elvesRings = {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);
containsExactly
in interface ObjectEnumerableAssert<AbstractObjectArrayAssert<S extends AbstractObjectArrayAssert<S,T>,T>,T>
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.public S containsExactlyInAnyOrder(T... values)
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);
containsExactlyInAnyOrder
in interface ObjectEnumerableAssert<AbstractObjectArrayAssert<S extends AbstractObjectArrayAssert<S,T>,T>,T>
values
- the given values.this
assertion object.public S containsExactlyElementsOf(Iterable<? extends T> iterable)
containsExactly(Object...)
but handle the Iterable
to array conversion : verifies that
actual contains all elements of the given Iterable
and nothing else in the same order.
Example :
Ring[] elvesRings = {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));
containsExactlyElementsOf
in interface ObjectEnumerableAssert<AbstractObjectArrayAssert<S extends AbstractObjectArrayAssert<S,T>,T>,T>
iterable
- the given Iterable
we will get elements from.public S containsSequence(T... sequence)
Use containsSubsequence(Object...)
to allow values between the expected sequence values.
Example:
Ring[] elvesRings = {vilya, nenya, narya};
// assertion 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);
containsSequence
in interface ObjectEnumerableAssert<AbstractObjectArrayAssert<S extends AbstractObjectArrayAssert<S,T>,T>,T>
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.public S containsSubsequence(T... subsequence)
Example:
Ring[] elvesRings = {vilya, nenya, narya};
// assertions will pass
assertThat(elvesRings).containsSubsequence(vilya, nenya);
assertThat(elvesRings).containsSubsequence(vilya, narya);
// assertion will fail
assertThat(elvesRings).containsSubsequence(nenya, vilya);
containsSubsequence
in interface ObjectEnumerableAssert<AbstractObjectArrayAssert<S extends AbstractObjectArrayAssert<S,T>,T>,T>
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.public S contains(T value, Index index)
Ring[] elvesRings = {vilya, nenya, narya};
// assertions will pass
assertThat(elvesRings).contains(vilya, atIndex(0));
assertThat(elvesRings).contains(nenya, atIndex(1));
assertThat(elvesRings).contains(narya, atIndex(2));
// assertions will fail
assertThat(elvesRings).contains(vilya, atIndex(1));
assertThat(elvesRings).contains(nenya, atIndex(2));
assertThat(elvesRings).contains(narya, atIndex(0));
contains
in interface IndexedObjectEnumerableAssert<AbstractObjectArrayAssert<S extends AbstractObjectArrayAssert<S,T>,T>,T>
value
- the object to look for.index
- the index where the object should be stored in the actual group.AssertionError
- if the actual group is null
or empty.NullPointerException
- if the given Index
is null
.IndexOutOfBoundsException
- if the value of the given Index
is equal to or greater than the size of the actual
group.AssertionError
- if the actual group does not contain the given object at the given index.public S doesNotContain(T value, Index index)
Ring[] elvesRings = {vilya, nenya, narya};
// assertions will pass
assertThat(elvesRings).contains(vilya, atIndex(1));
assertThat(elvesRings).contains(nenya, atIndex(2));
assertThat(elvesRings).contains(narya, atIndex(0));
// assertions will fail
assertThat(elvesRings).contains(vilya, atIndex(0));
assertThat(elvesRings).contains(nenya, atIndex(1));
assertThat(elvesRings).contains(narya, atIndex(2));
doesNotContain
in interface IndexedObjectEnumerableAssert<AbstractObjectArrayAssert<S extends AbstractObjectArrayAssert<S,T>,T>,T>
value
- the object to look for.index
- the index where the object should not be stored in the actual group.AssertionError
- if the actual group is null
.NullPointerException
- if the given Index
is null
.AssertionError
- if the actual group contains the given object at the given index.public S doesNotContain(T... values)
Example :
String[] abc = {"a", "b", "c"};
// assertion will pass
assertThat(abc).doesNotContain("d", "e");
// assertions will fail
assertThat(abc).doesNotContain("a");
assertThat(abc).doesNotContain("a", "b", "c");
assertThat(abc).doesNotContain("a", "x");
doesNotContain
in interface ObjectEnumerableAssert<AbstractObjectArrayAssert<S extends AbstractObjectArrayAssert<S,T>,T>,T>
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.public S doesNotContainAnyElementsOf(Iterable<? extends T> iterable)
Iterable
(i.e. none).
Example:
String[] abc = {"a", "b", "c"};
// assertion will pass
assertThat(actual).doesNotContainAnyElementsOf(newArrayList("d", "e"));
// assertions will fail
assertThat(actual).doesNotContainAnyElementsOf(newArrayList("a", "b"));
assertThat(actual).doesNotContainAnyElementsOf(newArrayList("d", "e", "a"));
doesNotContainAnyElementsOf
in interface ObjectEnumerableAssert<AbstractObjectArrayAssert<S extends AbstractObjectArrayAssert<S,T>,T>,T>
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
.public S doesNotHaveDuplicates()
Example :
String[] abc = {"a", "b", "c"};
String[] lotsOfAs = {"a", "a", "a"};
// assertion will pass
assertThat(abc).doesNotHaveDuplicates();
// assertion will fail
assertThat(lotsOfAs).doesNotHaveDuplicates();
doesNotHaveDuplicates
in interface ObjectEnumerableAssert<AbstractObjectArrayAssert<S extends AbstractObjectArrayAssert<S,T>,T>,T>
this
assertion object.AssertionError
- if the actual group is null
.AssertionError
- if the actual group contains duplicates.public S startsWith(T... sequence)
containsSequence(Object...)
, but it also verifies that the first element in the
sequence is also the first element of the actual array.
Example :
String[] abc = {"a", "b", "c"};
// assertion will pass
assertThat(abc).startsWith("a", "b");
// assertion will fail
assertThat(abc).startsWith("c");
startsWith
in interface ObjectEnumerableAssert<AbstractObjectArrayAssert<S extends AbstractObjectArrayAssert<S,T>,T>,T>
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.public S endsWith(T... sequence)
containsSequence(Object...)
, but it also verifies that the last element in the
sequence is also last element of the actual array.
Example :
String[] abc = {"a", "b", "c"};
// assertion will pass
assertThat(abc).endsWith("b", "c");
// assertion will fail
assertThat(abc).endsWith("a");
endsWith
in interface ObjectEnumerableAssert<AbstractObjectArrayAssert<S extends AbstractObjectArrayAssert<S,T>,T>,T>
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.public S isSubsetOf(Iterable<? extends T> values)
Iterable
.
Example:
Ring[] elvesRings = {vilya, nenya, narya};
List<Ring> ringsOfPower = newArrayList(oneRing, vilya, nenya, narya, dwarfRing, manRing);
// assertion will pass:
assertThat(elvesRings).isSubsetOf(ringsOfPower);
// assertion will fail:
assertThat(elvesRings).isSubsetOf(newArrayList(nenya, narya));
isSubsetOf
in interface ObjectEnumerableAssert<AbstractObjectArrayAssert<S extends AbstractObjectArrayAssert<S,T>,T>,T>
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
.public S isSubsetOf(T... values)
Ring[] elvesRings = {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);
isSubsetOf
in interface ObjectEnumerableAssert<AbstractObjectArrayAssert<S extends AbstractObjectArrayAssert<S,T>,T>,T>
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.public S containsNull()
Example :
String[] abc = {"a", "b", "c"};
String[] abNull = {"a", "b", null};
// assertion will pass
assertThat(abNull).containsNull();
// assertion will fail
assertThat(abc).containsNull();
containsNull
in interface ObjectEnumerableAssert<AbstractObjectArrayAssert<S extends AbstractObjectArrayAssert<S,T>,T>,T>
this
assertion object.AssertionError
- if the actual group is null
.AssertionError
- if the actual group does not contain a null element.public S doesNotContainNull()
Example :
String[] abc = {"a", "b", "c"};
String[] abNull = {"a", "b", null};
// assertion will pass
assertThat(abc).doesNotContainNull();
// assertion will fail
assertThat(abNull).doesNotContainNull();
doesNotContainNull
in interface ObjectEnumerableAssert<AbstractObjectArrayAssert<S extends AbstractObjectArrayAssert<S,T>,T>,T>
this
assertion object.AssertionError
- if the actual group is null
.AssertionError
- if the actual group contains a null element.public 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<String>() {
public boolean matches(String value) {
return value.length() == 1;
}
});
// assertion will pass
assertThat(abc).are(singleCharacterString);
// assertion will fail
assertThat(abcc).are(singleCharacterString);
are
in interface ObjectEnumerableAssert<AbstractObjectArrayAssert<S extends AbstractObjectArrayAssert<S,T>,T>,T>
condition
- the given condition.this
object.public 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<String>() {
public boolean matches(String value) {
return value.length() > 1;
}
});
// assertion will pass
assertThat(abc).areNot(moreThanOneCharacter);
// assertion will fail
assertThat(abcc).areNot(moreThanOneCharacter);
areNot
in interface ObjectEnumerableAssert<AbstractObjectArrayAssert<S extends AbstractObjectArrayAssert<S,T>,T>,T>
condition
- the given condition.this
object.public 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<String>() {
public boolean matches(String value) {
return value.length() == 1;
}
});
// assertion will pass
assertThat(abc).have(onlyOneCharacter);
// assertion will fail
assertThat(abcc).have(onlyOneCharacter);
have
in interface ObjectEnumerableAssert<AbstractObjectArrayAssert<S extends AbstractObjectArrayAssert<S,T>,T>,T>
condition
- the given condition.this
object.public 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<String>() {
public boolean matches(String value) {
return value.length() > 1;
}
});
// assertion will pass
assertThat(abc).doNotHave(moreThanOneCharacter);
// assertion will fail
assertThat(abcc).doNotHave(moreThanOneCharacter);
doNotHave
in interface ObjectEnumerableAssert<AbstractObjectArrayAssert<S extends AbstractObjectArrayAssert<S,T>,T>,T>
condition
- the given condition.this
object.public S areAtLeast(int times, Condition<? super T> condition)
Example :
Iterable<Integer> oneTwoThree = newArrayList(1, 2, 3);
Condition<Integer> oddNumber = new Condition<Integer>() {
public boolean matches(Integer value) {
return value % 2 == 1;
}
});
// assertion will pass
oneTwoThree.areAtLeast(2, oddNumber);
// assertion will fail
oneTwoThree.areAtLeast(3, oddNumber);
areAtLeast
in interface ObjectEnumerableAssert<AbstractObjectArrayAssert<S extends AbstractObjectArrayAssert<S,T>,T>,T>
times
- the minimum number of times the condition should be verified.condition
- the given condition.this
object.public S areAtLeastOne(Condition<? super T> condition)
areAtLeast(1, condition)
.
Example:
// jedi is a Condition<String>
assertThat(new String[]{"Luke", "Solo", "Leia"}).areAtLeastOne(jedi);
areAtLeastOne
in interface ObjectEnumerableAssert<AbstractObjectArrayAssert<S extends AbstractObjectArrayAssert<S,T>,T>,T>
haveAtLeast(int, Condition)
public S areAtMost(int times, Condition<? super T> condition)
Example :
Iterable<Integer> oneTwoThree = newArrayList(1, 2, 3);
Condition<Integer> oddNumber = new Condition<Integer>() {
public boolean matches(Integer value) {
return value % 2 == 1;
}
});
// assertions will pass
oneTwoThree.areAtMost(2, oddNumber);
oneTwoThree.areAtMost(3, oddNumber);
// assertions will fail
oneTwoThree.areAtMost(1, odd);
areAtMost
in interface ObjectEnumerableAssert<AbstractObjectArrayAssert<S extends AbstractObjectArrayAssert<S,T>,T>,T>
times
- the number of times the condition should be at most verified.condition
- the given condition.this
object.public S areExactly(int times, Condition<? super T> condition)
Example :
Iterable<Integer> oneTwoThree = newArrayList(1, 2, 3);
Condition<Integer> odd = new Condition<Integer>() {
public boolean matches(Integer value) {
return value % 2 == 1;
}
});
// assertion will pass
oneTwoThree.areExactly(2, odd);
// assertions will fail
oneTwoThree.areExactly(1, odd);
oneTwoThree.areExactly(3, odd);
areExactly
in interface ObjectEnumerableAssert<AbstractObjectArrayAssert<S extends AbstractObjectArrayAssert<S,T>,T>,T>
times
- the exact number of times the condition should be verified.condition
- the given condition.this
object.public S haveAtLeastOne(Condition<? super T> condition)
haveAtLeast(1, condition)
.
Example:
BasketBallPlayer[] bullsPlayers = {butler, rose};
// potentialMvp is a Condition<BasketBallPlayer>
assertThat(bullsPlayers).haveAtLeastOne(potentialMvp);
haveAtLeastOne
in interface ObjectEnumerableAssert<AbstractObjectArrayAssert<S extends AbstractObjectArrayAssert<S,T>,T>,T>
haveAtLeast(int, Condition)
public S haveAtLeast(int times, Condition<? super T> condition)
Example :
Iterable<Integer> oneTwoThree = newArrayList(1, 2, 3);
Condition<Integer> oddNumber = new Condition<Integer>() {
public boolean matches(Integer value) {
return value % 2 == 1;
}
});
// assertion will pass
oneTwoThree.haveAtLeast(2, oddNumber);
// assertion will fail
oneTwoThree.haveAtLeast(3, oddNumber);
This method is an alias for ObjectEnumerableAssert.areAtLeast(int, Condition)
.haveAtLeast
in interface ObjectEnumerableAssert<AbstractObjectArrayAssert<S extends AbstractObjectArrayAssert<S,T>,T>,T>
public S haveAtMost(int times, Condition<? super T> condition)
Example :
Iterable<Integer> oneTwoThree = newArrayList(1, 2, 3);
Condition<Integer> oddNumber = new Condition<Integer>() {
public boolean matches(Integer value) {
return value % 2 == 1;
}
});
// assertions will pass
oneTwoThree.haveAtMost(2, oddNumber);
oneTwoThree.haveAtMost(3, oddNumber);
// assertion will fail
oneTwoThree.haveAtMost(1, odd);
This method is an alias ObjectEnumerableAssert.areAtMost(int, Condition)
.haveAtMost
in interface ObjectEnumerableAssert<AbstractObjectArrayAssert<S extends AbstractObjectArrayAssert<S,T>,T>,T>
public S haveExactly(int times, Condition<? super T> condition)
Example :
Iterable<Integer> oneTwoThree = newArrayList(1, 2, 3);
Condition<Integer> oddNumber = new Condition<Integer>() {
public boolean matches(Integer value) {
return value % 2 == 1;
}
});
// assertion will pass
oneTwoThree.haveExactly(2, oddNumber);
// assertions will fail
oneTwoThree.haveExactly(1, oddNumber);
oneTwoThree.haveExactly(3, oddNumber);
This method is an alias ObjectEnumerableAssert.areExactly(int, Condition)
.haveExactly
in interface ObjectEnumerableAssert<AbstractObjectArrayAssert<S extends AbstractObjectArrayAssert<S,T>,T>,T>
public S hasAtLeastOneElementOfType(Class<?> type)
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);
hasAtLeastOneElementOfType
in interface ObjectEnumerableAssert<AbstractObjectArrayAssert<S extends AbstractObjectArrayAssert<S,T>,T>,T>
type
- the expected type.public S hasOnlyElementsOfType(Class<?> type)
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);
hasOnlyElementsOfType
in interface ObjectEnumerableAssert<AbstractObjectArrayAssert<S extends AbstractObjectArrayAssert<S,T>,T>,T>
type
- the expected type.public S isSorted()
All array elements must be primitive or implement the Comparable
interface and must be mutually comparable (that is,
e1.compareTo(e2) must not throw a ClassCastException for any elements e1 and e2 in the array), examples :
isSorted
in interface ArraySortedAssert<AbstractObjectArrayAssert<S extends AbstractObjectArrayAssert<S,T>,T>,T>
this
assertion object.public S isSortedAccordingTo(Comparator<? super T> comparator)
isSortedAccordingTo
in interface ArraySortedAssert<AbstractObjectArrayAssert<S extends AbstractObjectArrayAssert<S,T>,T>,T>
comparator
- the Comparator
used to compare array elementsthis
assertion object.public S containsAll(Iterable<? extends T> iterable)
Iterable
, in any order.
Example :
String[] abc = {"a", "b", "c"};
String[] bc = {"b", "c"};
// assertion will pass
assertThat(abc).containsAll(bc);
containsAll
in interface ObjectEnumerableAssert<AbstractObjectArrayAssert<S extends AbstractObjectArrayAssert<S,T>,T>,T>
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
.public S usingElementComparator(Comparator<? super T> elementComparator)
equals
method to compare group
elements for
incoming assertion checks.
Custom comparator is bound to assertion instance, meaning that if a new assertion is created, it will use default comparison strategy.
Examples :
// compares invoices by payee
assertThat(invoiceArray).usingComparator(invoicePayeeComparator).isEqualTo(expectedinvoiceArray).
// compares invoices by date, doesNotHaveDuplicates and contains both use the given invoice date comparator
assertThat(invoiceArray).usingComparator(invoiceDateComparator).doesNotHaveDuplicates().contains(may2010Invoice)
// as assertThat(invoiceArray) creates a new assertion, it falls back to standard comparison strategy
// based on Invoice's equal method to compare invoiceArray elements to lowestInvoice.
assertThat(invoiceArray).contains(lowestInvoice).
// standard comparison : the fellowshipOfTheRing includes Gandalf but not Sauron (believe me) ...
assertThat(fellowshipOfTheRing).contains(gandalf)
.doesNotContain(sauron);
// ... but if we compare only races, Sauron is in fellowshipOfTheRing because he's a Maia like Gandalf.
assertThat(fellowshipOfTheRing).usingElementComparator(raceComparator)
.contains(sauron);
usingElementComparator
in interface EnumerableAssert<AbstractObjectArrayAssert<S extends AbstractObjectArrayAssert<S,T>,T>,T>
customComparator
- the comparator to use for incoming assertion checks.this
assertion object.NullPointerException
- if the given comparator is null
.public S usingDefaultElementComparator()
This method should be used to disable a custom comparison strategy set by calling
EnumerableAssert.usingElementComparator(Comparator)
.
usingDefaultElementComparator
in interface EnumerableAssert<AbstractObjectArrayAssert<S extends AbstractObjectArrayAssert<S,T>,T>,T>
this
assertion object.public S usingFieldByFieldElementComparator()
equals
method to compare group elements for incoming assertion checks. Private fields
are included but this can be disabled using Assertions.setAllowExtractingPrivateFields(boolean)
.
This can be handy if equals
method of the objects to compare does not suit you.
Note that the comparison is not recursive, if one of the fields/properties is an Object, it will be compared
to the other field/property using its equals
method.
Example:
TolkienCharacter frodo = new TolkienCharacter("Frodo", 33, HOBBIT);
TolkienCharacter frodoClone = new TolkienCharacter("Frodo", 33, HOBBIT);
// Fail if equals has not been overridden in TolkienCharacter as equals default implementation only compares references
assertThat(array(frodo)).contains(frodoClone);
// frodo and frodoClone are equals when doing a field by field comparison.
assertThat(array(frodo)).usingFieldByFieldElementComparator().contains(frodoClone);
this
assertion object.public S usingElementComparatorOnFields(String... fields)
equals
method to compare group elements for
incoming assertion checks. Private fields are included but this can be disabled using
Assertions.setAllowExtractingPrivateFields(boolean)
.
This can be handy if equals
method of the objects to compare does not suit you.
Note that the comparison is not recursive, if one of the fields/properties is an Object, it will be compared
to the other field/property using its equals
method.
Example:
TolkienCharacter frodo = new TolkienCharacter("Frodo", 33, HOBBIT);
TolkienCharacter sam = new TolkienCharacter("Sam", 38, HOBBIT);
// frodo and sam both are hobbits, so they are equals when comparing only race
assertThat(array(frodo)).usingElementComparatorOnFields("race").contains(sam); // OK
// ... but not when comparing both name and race
assertThat(array(frodo)).usingElementComparatorOnFields("name", "race").contains(sam); // FAIL
this
assertion object.public S usingElementComparatorIgnoringFields(String... fields)
equals
method to compare group elements for
incoming assertion checks. Private fields are included but this can be disabled using
Assertions.setAllowExtractingPrivateFields(boolean)
.
This can be handy if equals
method of the objects to compare does not suit you.
Note that the comparison is not recursive, if one of the fields/properties is an Object, it will be compared
to the other field/property using its equals
method.
Example:
TolkienCharacter frodo = new TolkienCharacter("Frodo", 33, HOBBIT);
TolkienCharacter sam = new TolkienCharacter("Sam", 38, HOBBIT);
// frodo and sam both are hobbits, so they are equals when comparing only race (i.e. ignoring all other fields)
assertThat(array(frodo)).usingElementComparatorIgnoringFields("name", "age").contains(sam); // OK
// ... but not when comparing both name and race
assertThat(array(frodo)).usingElementComparatorIgnoringFields("age").contains(sam); // FAIL
this
assertion object.public ObjectArrayAssert<Object> extracting(String fieldOrProperty)
It allows you to test a field/property of the array's elements instead of testing the elements themselves, it can be sometimes much less work !
Let's take an example to make things clearer :
// Build a array of TolkienCharacter, a TolkienCharacter has a name (String) and a Race (a class)
// they can be public field or properties, both works when extracting their values.
TolkienCharacter[] fellowshipOfTheRing = new TolkienCharacter[] {
new TolkienCharacter("Frodo", 33, HOBBIT),
new TolkienCharacter("Sam", 38, HOBBIT),
new TolkienCharacter("Gandalf", 2020, MAIA),
new TolkienCharacter("Legolas", 1000, ELF),
new TolkienCharacter("Pippin", 28, HOBBIT),
new TolkienCharacter("Gimli", 139, DWARF),
new TolkienCharacter("Aragorn", 87, MAN,
new TolkienCharacter("Boromir", 37, MAN)
};
// let's verify the names of TolkienCharacter in fellowshipOfTheRing :
assertThat(fellowshipOfTheRing).extracting("name")
.contains("Boromir", "Gandalf", "Frodo")
.doesNotContain("Sauron", "Elrond");
// you can also extract nested field/property like the name of Race :
assertThat(fellowshipOfTheRing).extracting("race.name")
.contains("Hobbit", "Elf")
.doesNotContain("Orc");
A property with the given name is looked for first, if it does not exist then a field with the given name
is looked for.
Note that the order of extracted field/property values is consistent with the array order.
fieldOrProperty
- the field/property to extract from the array under testIntrospectionError
- if no field or property exists with the given namepublic <P> ObjectArrayAssert<P> extracting(String fieldOrProperty, Class<P> extractingType)
It allows you to test a field/property of the array's elements instead of testing the elements themselves, it can be sometimes much less work !
Let's take an example to make things clearer :
// Build an array of TolkienCharacter, a TolkienCharacter has a name (String) and a Race (a class)
// they can be public field or properties, both works when extracting their values.
TolkienCharacter[] fellowshipOfTheRing = new TolkienCharacter[] {
new TolkienCharacter("Frodo", 33, HOBBIT),
new TolkienCharacter("Sam", 38, HOBBIT),
new TolkienCharacter("Gandalf", 2020, MAIA),
new TolkienCharacter("Legolas", 1000, ELF),
new TolkienCharacter("Pippin", 28, HOBBIT),
new TolkienCharacter("Gimli", 139, DWARF),
new TolkienCharacter("Aragorn", 87, MAN,
new TolkienCharacter("Boromir", 37, MAN)
};
// let's verify the names of TolkienCharacter in fellowshipOfTheRing :
assertThat(fellowshipOfTheRing).extracting("name", String.class)
.contains("Boromir", "Gandalf", "Frodo")
.doesNotContain("Sauron", "Elrond");
// you can also extract nested field/property like the name of Race :
assertThat(fellowshipOfTheRing).extracting("race.name", String.class)
.contains("Hobbit", "Elf")
.doesNotContain("Orc");
A property with the given name is looked for first, if it does not exist then a field with the given name
is looked for.
Note that the order of extracted field/property values is consistent with the order of the array under test.
fieldOrProperty
- the field/property to extract from the array under testextractingType
- type to returnIntrospectionError
- if no field or property exists with the given namepublic ObjectArrayAssert<Tuple> extracting(String... propertiesOrFields)
// Build an array of TolkienCharacter, a TolkienCharacter has a name (String) and a Race (a class)
// they can be public field or properties, both works when extracting their values.
TolkienCharacter[] fellowshipOfTheRing = new TolkienCharacter[] {
new TolkienCharacter("Frodo", 33, HOBBIT),
new TolkienCharacter("Sam", 38, HOBBIT),
new TolkienCharacter("Gandalf", 2020, MAIA),
new TolkienCharacter("Legolas", 1000, ELF),
new TolkienCharacter("Pippin", 28, HOBBIT),
new TolkienCharacter("Gimli", 139, DWARF),
new TolkienCharacter("Aragorn", 87, MAN,
new TolkienCharacter("Boromir", 37, MAN)
};
// let's verify 'name' and 'age' of some TolkienCharacter in fellowshipOfTheRing :
assertThat(fellowshipOfTheRing).extracting("name", "age")
.contains(tuple("Boromir", 37),
tuple("Sam", 38),
tuple("Legolas", 1000));
// extract 'name', 'age' and Race name values.
assertThat(fellowshipOfTheRing).extracting("name", "age", "race.name")
.contains(tuple("Boromir", 37, "Man"),
tuple("Sam", 38, "Hobbit"),
tuple("Legolas", 1000, "Elf"));
A property with the given name is looked for first, if it does not exist the a field with the given name is
looked for.
Note that the order of extracted property/field values is consistent with the iteration order of the array under
test.propertiesOrFields
- the properties/fields to extract from the initial array under testIntrospectionError
- if one of the given name does not match a field or property in one of the initial
Iterable's element.public <U> ObjectArrayAssert<U> extracting(Extractor<? super T,U> extractor)
extracting(String)
, as it
doesn't utilize introspection.
Let's take a look an example:
// 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));
// this extracts the race
Extractor<TolkienCharacter, Race> race = new Extractor<TolkienCharacter, Race>() {
@Override
public Race extract(TolkienCharacter input) {
return input.getRace();
}
}
// fellowship has hobbitses, right, my presioussss?
assertThat(fellowshipOfTheRing).extracting(race).contains(HOBBIT);
Note that the order of extracted property/field values 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 values
order.extractor
- the object transforming input object to desired onepublic <U,C extends Collection<U>> ObjectArrayAssert<U> flatExtracting(Extractor<? super T,C> extractor)
CartoonCharacter bart = new CartoonCharacter("Bart Simpson");
CartoonCharacter lisa = new CartoonCharacter("Lisa Simpson");
CartoonCharacter maggie = new CartoonCharacter("Maggie Simpson");
CartoonCharacter homer = new CartoonCharacter("Homer Simpson");
homer.addChildren(bart, lisa, maggie);
CartoonCharacter pebbles = new CartoonCharacter("Pebbles Flintstone");
CartoonCharacter fred = new CartoonCharacter("Fred Flintstone");
fred.getChildren().add(pebbles);
Extractor<CartoonCharacter, List<CartoonCharacter>> childrenOf = new Extractor<CartoonCharacter, List<CartoonCharacter>>() {
@Override
public List<CartoonChildren> extract(CartoonCharacter input) {
return input.getChildren();
}
}
CartoonCharacter[] parents = new CartoonCharacter[] { homer, fred };
// check children
assertThat(parents).flatExtracting(childrenOf)
.containsOnly(bart, lisa, maggie, pebbles);
The order of extracted values is consisted with both the order of the collection itself, as well as the extracted
collections.extractor
- the object transforming input object to an Iterable of desired onespublic ObjectArrayAssert<Object> flatExtracting(String propertyName)
CartoonCharacter bart = new CartoonCharacter("Bart Simpson");
CartoonCharacter lisa = new CartoonCharacter("Lisa Simpson");
CartoonCharacter maggie = new CartoonCharacter("Maggie Simpson");
CartoonCharacter homer = new CartoonCharacter("Homer Simpson");
homer.addChildren(bart, lisa, maggie);
CartoonCharacter pebbles = new CartoonCharacter("Pebbles Flintstone");
CartoonCharacter fred = new CartoonCharacter("Fred Flintstone");
fred.getChildren().add(pebbles);
CartoonCharacter[] parents = new CartoonCharacter[] { homer, fred };
// check children
assertThat(parents).flatExtracting("children")
.containsOnly(bart, lisa, maggie, pebbles);
The order of extracted values is consisted with both the order of the collection itself, as well as the extracted
collections.propertyName
- the object transforming input object to an Iterable of desired onesIllegalArgumentException
- if one of the extracted property value was not an array or an iterable.public ObjectArrayAssert<Object> extractingResultOf(String method)
It allows you to test a method results of the array's elements instead of testing the elements themselves, it can be sometimes much less work!
It is especially useful for classes that does not conform to Java Bean's getter specification (i.e. public String toString() or public String status() instead of public String getStatus()).
Let's take an example to make things clearer :
// Build a array of WesterosHouse, a WesterosHouse has a method: public String sayTheWords()
WesterosHouse[] greatHousesOfWesteros = new WesterosHouse[] { new WesterosHouse("Stark", "Winter is Coming"),
new WesterosHouse("Lannister", "Hear Me Roar!"), new WesterosHouse("Greyjoy", "We Do Not Sow"),
new WesterosHouse("Baratheon", "Our is the Fury"), new WesterosHouse("Martell", "Unbowed, Unbent, Unbroken"),
new WesterosHouse("Tyrell", "Growing Strong") };
// let's verify the words of the great houses of Westeros:
assertThat(greatHousesOfWesteros).extractingResultOf("sayTheWords")
.contains("Winter is Coming", "We Do Not Sow", "Hear Me Roar")
.doesNotContain("Lannisters always pay their debts");
Following requirements have to be met to extract method results:
Note that the order of extracted values is consistent with the order of the array under test.
method
- the name of the method which result is to be extracted from the array under testIllegalArgumentException
- if no method exists with the given name, or method is not public, or method does
return void, or method accepts arguments.public <P> ObjectArrayAssert<P> extractingResultOf(String method, Class<P> extractingType)
It allows you to test a method results of the array's elements instead of testing the elements themselves, it can be sometimes much less work!
It is especially useful for classes that does not conform to Java Bean's getter specification (i.e. public String toString() or public String status() instead of public String getStatus()).
Let's take an example to make things clearer :
// Build a array of WesterosHouse, a WesterosHouse has a method: public String sayTheWords()
WesterosHouse[] greatHousesOfWesteros = new WesterosHouse[] { new WesterosHouse("Stark", "Winter is Coming"),
new WesterosHouse("Lannister", "Hear Me Roar!"), new WesterosHouse("Greyjoy", "We Do Not Sow"),
new WesterosHouse("Baratheon", "Our is the Fury"), new WesterosHouse("Martell", "Unbowed, Unbent, Unbroken"),
new WesterosHouse("Tyrell", "Growing Strong") };
// let's verify the words of the great houses of Westeros:
assertThat(greatHousesOfWesteros).extractingResultOf("sayTheWords", String.class)
.contains("Winter is Coming", "We Do Not Sow", "Hear Me Roar")
.doesNotContain("Lannisters always pay their debts");
Following requirements have to be met to extract method results:
Note that the order of extracted values is consistent with the order of the array under test.
method
- the name of the method which result is to be extracted from the array under testextractingType
- type to returnIllegalArgumentException
- if no method exists with the given name, or method is not public, or method does
return void, or method accepts arguments.public S inHexadecimal()
assertThat(new Byte[] { 0x10, 0x20 }).inHexadecimal().contains(new Byte[] { 0x30 });
With standard error message:
Expecting:
<[16, 32]>
to contain:
<[48]>
but could not find:
<[48]>
With Hexadecimal error message:
Expecting:
<[0x10, 0x20]>
to contain:
<[0x30]>
but could not find:
<[0x30]>
inHexadecimal
in class AbstractAssert<S extends AbstractObjectArrayAssert<S,T>,T[]>
this
assertion object.public S inBinary()
AbstractAssert
assertThat(1).inBinary().isEqualTo(2);
org.junit.ComparisonFailure:
Expected :0b00000000_00000000_00000000_00000010
Actual :0b00000000_00000000_00000000_00000001
inBinary
in class AbstractAssert<S extends AbstractObjectArrayAssert<S,T>,T[]>
this
assertion object.public S filteredOn(String propertyOrFieldName, Object expectedValue)
expectedValue
, the
property/field is specified by propertyOrFieldName
parameter.
The filter first tries to get the value from a property (named propertyOrFieldName
), if no such property
exists it tries to read the value from a field. Reading private fields is supported by default, this can be
globally disabled by calling Assertions.setAllowExtractingPrivateFields(false)
.
When reading nested property/field, if an intermediate value is null the whole nested property/field is considered to be null, thus reading "address.street.name" value will return null if "street" value is null.
As an example, let's check all employees 800 years old (yes, special employees):
Employee yoda = new Employee(1L, new Name("Yoda"), 800);
Employee obiwan = new Employee(2L, new Name("Obiwan"), 800);
Employee luke = new Employee(3L, new Name("Luke", "Skywalker"), 26);
Employee noname = new Employee(4L, null, 50);
Employee[] employees = new Employee[] { yoda, luke, obiwan, noname };
assertThat(employees).filteredOn("age", 800)
.containsOnly(yoda, obiwan);
Nested properties/fields are supported:
// Name is bean class with 'first' and 'last' String properties
// name is null for noname => it does not match the filter on "name.first"
assertThat(employees).filteredOn("name.first", "Luke")
.containsOnly(luke);
assertThat(employees).filteredOn("name.last", "Vader")
.isEmpty();
If you want to filter on null value, use filteredOnNull(String)
as Java will resolve the call to
filteredOn(String, FilterOperator)
instead of this method.
An IntrospectionError
is thrown if the given propertyOrFieldName can't be found in one of the array
elements.
You can chain filters:
// fellowshipOfTheRing is an array of TolkienCharacter having race and name fields
// 'not' filter is statically imported from Assertions.not
assertThat(fellowshipOfTheRing).filteredOn("race.name", "Man")
.filteredOn("name", not("Boromir"))
.containsOnly(aragorn);
If you need more complex filter, use filteredOn(Condition)
and provide a Condition
to specify the
filter to apply.propertyOrFieldName
- the name of the property or field to readexpectedValue
- the value to compare element's property or field withIllegalArgumentException
- if the given propertyOrFieldName is null
or empty.IntrospectionError
- if the given propertyOrFieldName can't be found in one of the array elements.public S filteredOnNull(String propertyOrFieldName)
propertyOrFieldName
is null.
exists it tries to read the value from a field. Reading private fields is supported by default, this can be
globally disabled by calling Assertions.setAllowExtractingPrivateFields(false)
.
When reading nested property/field, if an intermediate value is null the whole nested property/field is considered to be null, thus reading "address.street.name" value will return null if "street" value is null.
As an example, let's check all employees 800 years old (yes, special employees):
Employee yoda = new Employee(1L, new Name("Yoda"), 800);
Employee obiwan = new Employee(2L, new Name("Obiwan"), 800);
Employee luke = new Employee(3L, new Name("Luke", "Skywalker"), 26);
Employee noname = new Employee(4L, null, 50);
Employee[] employees = new Employee[] { yoda, luke, obiwan, noname };
assertThat(employees).filteredOnNull("name")
.containsOnly(noname);
Nested properties/fields are supported:
// Name is bean class with 'first' and 'last' String properties
assertThat(employees).filteredOnNull("name.last")
.containsOnly(yoda, obiwan, noname);
An IntrospectionError
is thrown if the given propertyOrFieldName can't be found in one of the array
elements.
If you need more complex filter, use filteredOn(Condition)
and provide a Condition
to specify the
filter to apply.
propertyOrFieldName
- the name of the property or field to readIntrospectionError
- if the given propertyOrFieldName can't be found in one of the array elements.public S filteredOn(String propertyOrFieldName, FilterOperator<?> filterOperator)
FilterOperator
, the property/field is specified by propertyOrFieldName
parameter.
The existing filters are :
Whatever filter is applied, it first tries to get the value from a property (named propertyOrFieldName
), if
no such property exists it tries to read the value from a field. Reading private fields is supported by default,
this can be globally disabled by calling Assertions.setAllowExtractingPrivateFields(false)
.
When reading nested property/field, if an intermediate value is null the whole nested property/field is considered to be null, thus reading "address.street.name" value will return null if "street" value is null.
As an example, let's check stuff on some special employees :
Employee yoda = new Employee(1L, new Name("Yoda"), 800);
Employee obiwan = new Employee(2L, new Name("Obiwan"), 800);
Employee luke = new Employee(3L, new Name("Luke", "Skywalker"), 26);
Employee[] employees = new Employee[] { yoda, luke, obiwan, noname };
// 'not' filter is statically imported from Assertions.not
assertThat(employees).filteredOn("age", not(800))
.containsOnly(luke);
// 'in' filter is statically imported from Assertions.in
// Name is bean class with 'first' and 'last' String properties
assertThat(employees).filteredOn("name.first", in("Yoda", "Luke"))
.containsOnly(yoda, luke);
// 'notIn' filter is statically imported from Assertions.notIn
assertThat(employees).filteredOn("name.first", notIn("Yoda", "Luke"))
.containsOnly(obiwan);
An IntrospectionError
is thrown if the given propertyOrFieldName can't be found in one of the array
elements.
Note that combining filter operators is not supported, thus the following code is not correct:
// Combining filter operators like not(in(800)) is NOT supported
// -> throws UnsupportedOperationException
assertThat(employees).filteredOn("age", not(in(800)))
.contains(luke);
You can chain filters:
// fellowshipOfTheRing is an array of TolkienCharacter having race and name fields
// 'not' filter is statically imported from Assertions.not
assertThat(fellowshipOfTheRing).filteredOn("race.name", "Man")
.filteredOn("name", not("Boromir"))
.containsOnly(aragorn);
If you need more complex filter, use filteredOn(Condition)
and provide a Condition
to specify the
filter to apply.propertyOrFieldName
- the name of the property or field to readfilterOperator
- the filter operator to applyIllegalArgumentException
- if the given propertyOrFieldName is null
or empty.public S filteredOn(Condition<? super T> condition)
Condition
.
Let's check old employees whose age > 100:
Employee yoda = new Employee(1L, new Name("Yoda"), 800);
Employee obiwan = new Employee(2L, new Name("Obiwan"), 800);
Employee luke = new Employee(3L, new Name("Luke", "Skywalker"), 26);
Employee noname = new Employee(4L, null, 50);
Employee[] employees = new Employee[] { yoda, luke, obiwan, noname };
// old employee condition, "old employees" describes the condition in error message
// you just have to implement 'matches' method
Condition<Employee> oldEmployees = new Condition<Employee>("old employees") {
@Override
public boolean matches(Employee employee) {
return employee.getAge() > 100;
}
};
}
assertThat(employees).filteredOn(oldEmployees)
.containsOnly(yoda, obiwan);
You can combine Condition
with condition operator like Not
:
// 'not' filter is statically imported from Assertions.not
assertThat(employees).filteredOn(not(oldEmployees))
.contains(luke, noname);
condition
- the filter condition / predicateIllegalArgumentException
- if the given condition is null
.Copyright © 2013–2016 AssertJ. All rights reserved.