ELEMENT
- the type of elements of the "actual" value.public class ObjectArrayAssert<ELEMENT> extends AbstractObjectArrayAssert<ObjectArrayAssert<ELEMENT>,ELEMENT>
To create an instance of this class, invoke
.
Assertions.assertThat(Object[])
actual, info, myself, throwUnsupportedExceptionOnEquals
Constructor and Description |
---|
ObjectArrayAssert(AtomicReferenceArray<ELEMENT> actual) |
ObjectArrayAssert(ELEMENT[] actual) |
Modifier and Type | Method and Description |
---|---|
ObjectArrayAssert<ELEMENT> |
contains(ELEMENT... values)
Verifies that the actual array contains the given values, in any order.
|
ObjectArrayAssert<ELEMENT> |
containsAnyOf(ELEMENT... values)
Verifies that the actual array contains at least one of the given values.
|
ObjectArrayAssert<ELEMENT> |
containsExactly(ELEMENT... values)
Verifies that the actual array contains exactly the given values and nothing else, in order.
|
ObjectArrayAssert<ELEMENT> |
containsExactlyInAnyOrder(ELEMENT... values)
Verifies that the actual array contains exactly the given values and nothing else, in any order.
|
ObjectArrayAssert<ELEMENT> |
containsOnly(ELEMENT... values)
Verifies that the actual array contains only the given values and nothing else, in any order and ignoring duplicates (i.e.
|
ObjectArrayAssert<ELEMENT> |
containsOnlyOnce(ELEMENT... values)
Verifies that the actual array contains the given values only once.
|
ObjectArrayAssert<ELEMENT> |
containsSequence(ELEMENT... sequence)
Verifies that the actual array contains the given sequence in the correct order and without extra values between the sequence values.
|
ObjectArrayAssert<ELEMENT> |
containsSubsequence(ELEMENT... sequence)
Verifies that the actual array contains the given subsequence in the correct order (possibly with other values between them).
|
ObjectArrayAssert<ELEMENT> |
doesNotContain(ELEMENT... values)
Verifies that the actual array does not contain the given values.
|
ObjectArrayAssert<ELEMENT> |
doesNotContainSequence(ELEMENT... sequence)
Verifies that the actual array does not contain the given sequence in the given order and without extra values between the sequence values.
|
ObjectArrayAssert<ELEMENT> |
doesNotContainSubsequence(ELEMENT... sequence)
Verifies that the actual array does not contain the given subsequence in the correct order (possibly with other
values between them).
|
ObjectArrayAssert<ELEMENT> |
endsWith(ELEMENT first,
ELEMENT... rest)
Verifies that the actual array ends with the given sequence of objects, without any other objects between them.
|
AbstractListAssert<?,List<? extends Tuple>,Tuple,ObjectAssert<Tuple>> |
extracting(Function<? super ELEMENT,?>... extractors)
|
ObjectArrayAssert<ELEMENT> |
isSubsetOf(ELEMENT... values)
Verifies that all elements of actual are present in the given values.
|
protected ObjectArrayAssert<ELEMENT> |
newObjectArrayAssert(ELEMENT[] array) |
allMatch, allMatch, allSatisfy, anyMatch, anySatisfy, are, areAtLeast, areAtLeastOne, areAtMost, areExactly, areNot, as, as, contains, containsAll, containsAnyElementsOf, containsExactlyElementsOf, containsExactlyInAnyOrderElementsOf, containsNull, containsOnlyElementsOf, containsOnlyNulls, containsSequence, containsSubsequence, doesNotContain, doesNotContainAnyElementsOf, doesNotContainNull, doesNotContainSequence, doesNotContainSubsequence, doesNotHaveAnyElementsOfTypes, doesNotHaveDuplicates, doNotHave, endsWith, extracting, extracting, extracting, extracting, extracting, extractingResultOf, extractingResultOf, filteredOn, filteredOn, filteredOn, filteredOn, filteredOnAssertions, filteredOnNull, flatExtracting, flatExtracting, flatExtracting, getComparatorsByType, getComparatorsForElementPropertyOrFieldTypes, hasAtLeastOneElementOfType, hasOnlyElementsOfType, hasOnlyElementsOfTypes, hasOnlyOneElementSatisfying, hasSameElementsAs, hasSameSizeAs, hasSameSizeAs, hasSize, hasSizeBetween, hasSizeGreaterThan, hasSizeGreaterThanOrEqualTo, hasSizeLessThan, hasSizeLessThanOrEqualTo, have, haveAtLeast, haveAtLeastOne, haveAtMost, haveExactly, inBinary, inHexadecimal, isEmpty, isNotEmpty, isNullOrEmpty, isSorted, isSortedAccordingTo, isSubsetOf, newListAssertInstance, noneMatch, noneSatisfy, startsWith, usingComparatorForElementFieldsWithNames, usingComparatorForElementFieldsWithType, usingComparatorForType, usingDefaultElementComparator, usingElementComparator, usingElementComparatorIgnoringFields, usingElementComparatorOnFields, usingFieldByFieldElementComparator, usingRecursiveFieldByFieldElementComparator, zipSatisfy
asList, asString, describedAs, describedAs, descriptionText, doesNotHave, doesNotHaveSameClassAs, equals, failWithMessage, getWritableAssertionInfo, has, hashCode, hasSameClassAs, hasSameHashCodeAs, hasToString, is, isEqualTo, isExactlyInstanceOf, isIn, isIn, isInstanceOf, isInstanceOfAny, isInstanceOfSatisfying, isNot, isNotEqualTo, isNotExactlyInstanceOf, isNotIn, isNotIn, isNotInstanceOf, isNotInstanceOfAny, isNotNull, isNotOfAnyClassIn, isNotSameAs, isNull, isOfAnyClassIn, isSameAs, matches, matches, overridingErrorMessage, satisfies, satisfies, satisfiesAnyOf, satisfiesAnyOf, setCustomRepresentation, throwAssertionError, usingComparator, usingComparator, usingDefaultComparator, withFailMessage, withRepresentation, withThreadDumpOnError
public ObjectArrayAssert(ELEMENT[] actual)
public ObjectArrayAssert(AtomicReferenceArray<ELEMENT> actual)
protected ObjectArrayAssert<ELEMENT> newObjectArrayAssert(ELEMENT[] array)
newObjectArrayAssert
in class AbstractObjectArrayAssert<ObjectArrayAssert<ELEMENT>,ELEMENT>
@SafeVarargs public final AbstractListAssert<?,List<? extends Tuple>,Tuple,ObjectAssert<Tuple>> extracting(Function<? super ELEMENT,?>... extractors)
AbstractObjectArrayAssert
Function
s to extract the values from the array's elements into a new list
composed of Tuple
s (a simple data structure containing the extracted values), this new list becoming the
object under test.
It allows you to test values from the array's elements instead of testing the elements themselves, which sometimes can be much less work!
The Tuple
data corresponds to the extracted values from the arrays's elements, for instance if you pass functions
extracting "id", "name" and "email" values then each Tuple
's data will be composed of an id, a name and an email
extracted from the element of the initial array (the Tuple's data order is the same as the given functions order).
Let's take a look at an example to make things clearer :
// Build an array of TolkienCharacter, a TolkienCharacter has a name (String) and a Race (a class)
// they can be public field or properties, both works when extracting their values.
TolkienCharacter[] fellowshipOfTheRing = new TolkienCharacter[] {
new TolkienCharacter("Frodo", 33, HOBBIT),
new TolkienCharacter("Sam", 38, HOBBIT),
new TolkienCharacter("Gandalf", 2020, MAIA),
new TolkienCharacter("Legolas", 1000, ELF),
new TolkienCharacter("Pippin", 28, HOBBIT),
new TolkienCharacter("Gimli", 139, DWARF),
new TolkienCharacter("Aragorn", 87, MAN,
new TolkienCharacter("Boromir", 37, MAN)
};
// let's verify 'name', 'age' and Race of some TolkienCharacter in fellowshipOfTheRing :
assertThat(fellowshipOfTheRing).extracting(TolkienCharacter::getName,
character -> character.getAge(),
TolkienCharacter::getRace)
.containsOnly(tuple("Frodo", 33, HOBBIT),
tuple("Sam", 38, HOBBIT),
tuple("Gandalf", 2020, MAIA),
tuple("Legolas", 1000, ELF),
tuple("Pippin", 28, HOBBIT),
tuple("Gimli", 139, DWARF),
tuple("Aragorn", 87, MAN),
tuple("Boromir", 37, MAN));
You can use lambda expression or a method reference to extract the expected values.
Use Tuple.tuple(Object...)
to initialize the expected values.
Note that the order of the extracted tuples list is consistent with the iteration order of the array under test,
for example if it's a HashSet
, you won't be able to make any assumptions on the extracted tuples order.
extracting
in class AbstractObjectArrayAssert<ObjectArrayAssert<ELEMENT>,ELEMENT>
extractors
- the extractor functions to extract a value from an element of the array under test.@SafeVarargs public final ObjectArrayAssert<ELEMENT> contains(ELEMENT... values)
AbstractObjectArrayAssert
Example :
String[] abc = {"a", "b", "c"};
// assertions will pass
assertThat(abc).contains("b", "a");
assertThat(abc).contains("b", "a", "b");
// assertions will fail
assertThat(abc).contains("d");
assertThat(abc).contains("c", "d");
contains
in interface ObjectEnumerableAssert<AbstractObjectArrayAssert<ObjectArrayAssert<ELEMENT>,ELEMENT>,ELEMENT>
contains
in class AbstractObjectArrayAssert<ObjectArrayAssert<ELEMENT>,ELEMENT>
values
- the given values.this
assertion object.@SafeVarargs public final ObjectArrayAssert<ELEMENT> containsOnly(ELEMENT... values)
AbstractObjectArrayAssert
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 String[] abc = {"a", "b", "c"};
// assertions succeed
assertThat(abc).containsOnly("c", "b", "a");
// duplicates are ignored
assertThat(abc).containsOnly("a", "a", "b", "c", "c");
// ... on both actual and expected values
assertThat(new String[] { "a", "a", "b" }).containsOnly("a", "b")
.containsOnly("a", "a", "b", "b");
// assertion will fail because the given values do not contain "c"
assertThat(abc).containsOnly("a", "b");
// assertion will fail because abc does not contain "d"
assertThat(abc).containsOnly("a", "b", "c", "d");
containsOnly
in interface ObjectEnumerableAssert<AbstractObjectArrayAssert<ObjectArrayAssert<ELEMENT>,ELEMENT>,ELEMENT>
containsOnly
in class AbstractObjectArrayAssert<ObjectArrayAssert<ELEMENT>,ELEMENT>
values
- the given values.this
assertion object.@SafeVarargs public final ObjectArrayAssert<ELEMENT> containsOnlyOnce(ELEMENT... values)
AbstractObjectArrayAssert
Examples :
// array is 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("Arya", "Stark", "daughter", "of", "Ned", "Stark")).containsOnlyOnce("Stark");
assertThat(array("Arya", "Stark", "daughter", "of", "Ned", "Stark")).containsOnlyOnce("Stark", "Lannister", "Arya");
containsOnlyOnce
in interface ObjectEnumerableAssert<AbstractObjectArrayAssert<ObjectArrayAssert<ELEMENT>,ELEMENT>,ELEMENT>
containsOnlyOnce
in class AbstractObjectArrayAssert<ObjectArrayAssert<ELEMENT>,ELEMENT>
values
- the given values.this
assertion object.@SafeVarargs public final ObjectArrayAssert<ELEMENT> containsExactly(ELEMENT... values)
AbstractObjectArrayAssert
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<ObjectArrayAssert<ELEMENT>,ELEMENT>,ELEMENT>
containsExactly
in class AbstractObjectArrayAssert<ObjectArrayAssert<ELEMENT>,ELEMENT>
values
- the given values.this
assertion object.@SafeVarargs public final ObjectArrayAssert<ELEMENT> containsExactlyInAnyOrder(ELEMENT... values)
AbstractObjectArrayAssert
Example :
Ring[] elvesRings = {vilya, nenya, narya, vilya};
// assertion will pass
assertThat(elvesRings).containsExactlyInAnyOrder(vilya, vilya, nenya, narya);
// assertion will fail as vilya exists twice in elvesRings
assertThat(elvesRings).containsExactlyInAnyOrder(nenya, vilya, narya);
containsExactlyInAnyOrder
in interface ObjectEnumerableAssert<AbstractObjectArrayAssert<ObjectArrayAssert<ELEMENT>,ELEMENT>,ELEMENT>
containsExactlyInAnyOrder
in class AbstractObjectArrayAssert<ObjectArrayAssert<ELEMENT>,ELEMENT>
values
- the given values.this
assertion object.@SafeVarargs public final ObjectArrayAssert<ELEMENT> containsAnyOf(ELEMENT... values)
AbstractObjectArrayAssert
Example :
String[] abc = {"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");
containsAnyOf
in interface ObjectEnumerableAssert<AbstractObjectArrayAssert<ObjectArrayAssert<ELEMENT>,ELEMENT>,ELEMENT>
containsAnyOf
in class AbstractObjectArrayAssert<ObjectArrayAssert<ELEMENT>,ELEMENT>
values
- the values whose at least one which is expected to be in the array under test.this
assertion object.@SafeVarargs public final ObjectArrayAssert<ELEMENT> isSubsetOf(ELEMENT... values)
AbstractObjectArrayAssert
Example:
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<ObjectArrayAssert<ELEMENT>,ELEMENT>,ELEMENT>
isSubsetOf
in class AbstractObjectArrayAssert<ObjectArrayAssert<ELEMENT>,ELEMENT>
values
- the values that should be used for checking the elements of actual.@SafeVarargs public final ObjectArrayAssert<ELEMENT> containsSequence(ELEMENT... sequence)
AbstractObjectArrayAssert
Use AbstractObjectArrayAssert.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<ObjectArrayAssert<ELEMENT>,ELEMENT>,ELEMENT>
containsSequence
in class AbstractObjectArrayAssert<ObjectArrayAssert<ELEMENT>,ELEMENT>
sequence
- the sequence of objects to look for.@SafeVarargs public final ObjectArrayAssert<ELEMENT> doesNotContainSequence(ELEMENT... sequence)
AbstractObjectArrayAssert
Use AbstractObjectArrayAssert.doesNotContainSubsequence(Object...)
to also ensure the sequence does not exist with values between the expected sequence values.
Example:
Ring[] elvesRings = {vilya, nenya, narya};
// assertion will pass, the elements order is correct but there is a value between them (nenya)
assertThat(elvesRings).containsSequence(vilya, narya);
assertThat(elvesRings).containsSequence(nenya, vilya);
// assertions will fail
assertThat(elvesRings).containsSequence(vilya, nenya);
assertThat(elvesRings).containsSequence(nenya, narya);
doesNotContainSequence
in interface ObjectEnumerableAssert<AbstractObjectArrayAssert<ObjectArrayAssert<ELEMENT>,ELEMENT>,ELEMENT>
doesNotContainSequence
in class AbstractObjectArrayAssert<ObjectArrayAssert<ELEMENT>,ELEMENT>
sequence
- the sequence of objects to look for.@SafeVarargs public final ObjectArrayAssert<ELEMENT> containsSubsequence(ELEMENT... sequence)
AbstractObjectArrayAssert
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<ObjectArrayAssert<ELEMENT>,ELEMENT>,ELEMENT>
containsSubsequence
in class AbstractObjectArrayAssert<ObjectArrayAssert<ELEMENT>,ELEMENT>
sequence
- the subsequence of objects to look for.@SafeVarargs public final ObjectArrayAssert<ELEMENT> doesNotContainSubsequence(ELEMENT... sequence)
AbstractObjectArrayAssert
Example:
Ring[] elvesRings = {vilya, nenya, narya};
// assertions will pass
assertThat(elvesRings).doesNotContainSubsequence(nenya, vilya);
// assertion will fail
assertThat(elvesRings).doesNotContainSubsequence(vilya, nenya);
assertThat(elvesRings).doesNotContainSubsequence(vilya, narya);
doesNotContainSubsequence
in interface ObjectEnumerableAssert<AbstractObjectArrayAssert<ObjectArrayAssert<ELEMENT>,ELEMENT>,ELEMENT>
doesNotContainSubsequence
in class AbstractObjectArrayAssert<ObjectArrayAssert<ELEMENT>,ELEMENT>
sequence
- the subsequence of objects to look for.@SafeVarargs public final ObjectArrayAssert<ELEMENT> doesNotContain(ELEMENT... values)
AbstractObjectArrayAssert
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<ObjectArrayAssert<ELEMENT>,ELEMENT>,ELEMENT>
doesNotContain
in class AbstractObjectArrayAssert<ObjectArrayAssert<ELEMENT>,ELEMENT>
values
- the given values.this
assertion object.@SafeVarargs public final ObjectArrayAssert<ELEMENT> endsWith(ELEMENT first, ELEMENT... rest)
AbstractObjectArrayAssert
AbstractObjectArrayAssert.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<ObjectArrayAssert<ELEMENT>,ELEMENT>,ELEMENT>
endsWith
in class AbstractObjectArrayAssert<ObjectArrayAssert<ELEMENT>,ELEMENT>
first
- the first element of the end sequence of objects to look for.rest
- the rest of the end sequence of objects to look for.Copyright © 2014–2019 AssertJ. All rights reserved.