|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object org.assertj.core.api.AbstractAssert<S,T[]> org.assertj.core.api.AbstractObjectArrayAssert<S,T>
T
- the type of elements of the "actual" value.public abstract class AbstractObjectArrayAssert<S extends AbstractObjectArrayAssert<S,T>,T>
Assertion methods for arrays of objects.
To create an instance of this class, invoke
.
Assertions#assertThat(T[])
Field Summary |
---|
Fields inherited from class org.assertj.core.api.AbstractAssert |
---|
actual, info, myself |
Constructor Summary | |
---|---|
protected |
AbstractObjectArrayAssert(T[] actual,
Class<?> selfType)
|
Method Summary | ||
---|---|---|
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 group 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 group contains the given object at the given index. |
|
S |
containsAll(Iterable<? extends T> iterable)
Verifies that the actual group contains all the elements of given Iterable , in any order. |
|
S |
containsExactly(T... values)
Verifies that the actual group contains only the given values and nothing else, in order. This assertion should only be used with group that have a consistent iteration order (i.e. |
|
S |
containsNull()
Verifies that the actual group contains at least a null element. |
|
S |
containsOnly(T... values)
Verifies that the actual group contains only the given values and nothing else, in any order. |
|
S |
containsOnlyOnce(T... values)
Verifies that the actual array contains the given values only once. |
|
S |
containsSequence(T... sequence)
Verifies that the actual group contains the given sequence, without any other values between them. |
|
S |
containsSubsequence(T... subsequence)
Verifies that the actual group contains the given subsequence (possibly with other values between them). |
|
S |
doesNotContain(T... values)
Verifies that the actual group does not contain the given values. |
|
S |
doesNotContain(T value,
Index index)
Verifies that the actual group does not contain the given object at the given index. |
|
S |
doesNotContainNull()
Verifies that the actual group does not contain null elements. |
|
S |
doesNotHaveDuplicates()
Verifies that the actual group does not contain duplicates. |
|
S |
doNotHave(Condition<? super T> condition)
Verifies that each element value does not satisfy the given condition |
|
S |
endsWith(T... sequence)
Verifies that the actual group ends with the given sequence of objects, without any other objects between them. |
|
|
extracting(Extractor<? super T,U> extractor)
Extract the values from Iterable's elements after test 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. |
|
|
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. |
|
|
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. |
|
|
flatExtracting(Extractor<? super T,C> extractor)
|
|
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 |
hasSameSizeAs(Iterable<?> other)
Verifies that the actual group has the same size as given Iterable . |
|
S |
hasSameSizeAs(Object other)
Verifies that the actual group has the same size as 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 each element value satisfies 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)
This method is an alias ObjectEnumerableAssert.areAtMost(int, Condition) . |
|
S |
haveExactly(int times,
Condition<? super T> condition)
This method is an alias ObjectEnumerableAssert.areExactly(int, Condition) . |
|
S |
inBinary()
Use binary object representation instead of standard representation in error messages. |
|
S |
inHexadecimal()
Enable hexadecimal object representation of Itearble 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 |
startsWith(T... sequence)
Verifies that the actual group 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> customComparator)
Use given custom comparator instead of relying on actual type A equals method to compare group elements for
incoming assertion checks. |
Methods inherited from class org.assertj.core.api.AbstractAssert |
---|
as, as, describedAs, describedAs, descriptionText, doesNotHave, doesNotHaveSameClassAs, equals, failWithMessage, getWritableAssertionInfo, has, hashCode, hasSameClassAs, is, isEqualTo, isExactlyInstanceOf, isIn, isIn, isInstanceOf, isInstanceOfAny, isNot, isNotEqualTo, isNotExactlyInstanceOf, isNotIn, isNotIn, isNotInstanceOf, isNotInstanceOfAny, isNotNull, isNotOfAnyClassIn, isNotSameAs, isNull, isOfAnyClassIn, isSameAs, overridingErrorMessage, usingComparator, usingDefaultComparator |
Methods inherited from class java.lang.Object |
---|
clone, finalize, getClass, notify, notifyAll, toString, wait, wait, wait |
Constructor Detail |
---|
protected AbstractObjectArrayAssert(T[] actual, Class<?> selfType)
Method Detail |
---|
public void isNullOrEmpty()
null
or empty.
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()
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()
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)
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)
hasSameSizeAs
in interface EnumerableAssert<AbstractObjectArrayAssert<S extends AbstractObjectArrayAssert<S,T>,T>,T>
other
- the array to compare size with actual group.
this
assertion object.public S hasSameSizeAs(Iterable<?> other)
Iterable
.
hasSameSizeAs
in interface EnumerableAssert<AbstractObjectArrayAssert<S extends AbstractObjectArrayAssert<S,T>,T>,T>
other
- the Iterable
to compare size with actual group.
this
assertion object.public S contains(T... values)
contains
in interface ObjectEnumerableAssert<AbstractObjectArrayAssert<S extends AbstractObjectArrayAssert<S,T>,T>,T>
values
- the given values.
this
assertion object.public S containsOnly(T... values)
containsOnly
in interface ObjectEnumerableAssert<AbstractObjectArrayAssert<S extends AbstractObjectArrayAssert<S,T>,T>,T>
values
- the given values.
this
assertion object.public S containsOnlyOnce(T... values)
Examples :
// assertion will pass assertThat(newArrayList("winter", "is", "coming")).containsOnlyOnce("winter"); assertThat(newArrayList("winter", "is", "coming")).containsOnlyOnce("coming", "winter"); // assertions will fail assertThat(newArrayList("Aria", "Stark", "daughter", "of", "Ned", "Stark")).containsOnlyOnce("Stark"); assertThat(newArrayList("winter", "is", "coming")).containsOnlyOnce("Lannister"); assertThat(newArrayList("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.public S containsExactly(T... values)
HashSet
, prefer ObjectEnumerableAssert.containsOnly(Object...)
in that case).
Example :
Iterable<Ring> elvesRings = newArrayList(vilya, nenya, narya); // assertion will pass assertThat(elvesRings).containsExactly(vilya, nenya, narya); // assertion will fail as actual and expected orders 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.public S containsSequence(T... sequence)
Example: Iterable<Ring> elvesRings = newArrayList(vilya, nenya, narya); // assertion will pass assertThat(elvesRings).containsSequence(vilya, nenya); // assertion will fail 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.
public S containsSubsequence(T... subsequence)
Example: Iterable<Ring> elvesRings = newArrayList(vilya, nenya, narya); // assertion 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>
subsequence
- the sequence of objects to look for.
public S contains(T value, Index index)
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.
public S doesNotContain(T value, Index index)
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 be stored in the actual group.
public S doesNotContain(T... values)
doesNotContain
in interface ObjectEnumerableAssert<AbstractObjectArrayAssert<S extends AbstractObjectArrayAssert<S,T>,T>,T>
values
- the given values.
this
assertion object.public S doesNotHaveDuplicates()
doesNotHaveDuplicates
in interface ObjectEnumerableAssert<AbstractObjectArrayAssert<S extends AbstractObjectArrayAssert<S,T>,T>,T>
this
assertion object.public S startsWith(T... sequence)
ObjectEnumerableAssert.containsSequence(Object...)
, but it also verifies that the first element in the
sequence is also first element of the actual group.
startsWith
in interface ObjectEnumerableAssert<AbstractObjectArrayAssert<S extends AbstractObjectArrayAssert<S,T>,T>,T>
sequence
- the sequence of objects to look for.
public S endsWith(T... sequence)
ObjectEnumerableAssert.containsSequence(Object...)
, but it also verifies that the last element in the
sequence is also last element of the actual group.
endsWith
in interface ObjectEnumerableAssert<AbstractObjectArrayAssert<S extends AbstractObjectArrayAssert<S,T>,T>,T>
sequence
- the sequence of objects to look for.
public S containsNull()
containsNull
in interface ObjectEnumerableAssert<AbstractObjectArrayAssert<S extends AbstractObjectArrayAssert<S,T>,T>,T>
this
assertion object.public S doesNotContainNull()
doesNotContainNull
in interface ObjectEnumerableAssert<AbstractObjectArrayAssert<S extends AbstractObjectArrayAssert<S,T>,T>,T>
this
assertion object.public S are(Condition<? super T> condition)
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)
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)
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)
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)
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(newLinkedHashSet("Luke", "Solo", "Leia")).areAtLeastOne(jedi);
areAtLeastOne
in interface ObjectEnumerableAssert<AbstractObjectArrayAssert<S extends AbstractObjectArrayAssert<S,T>,T>,T>
ObjectEnumerableAssert.haveAtLeast(int, Condition)
public S areAtMost(int times, Condition<? super T> condition)
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)
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:
List<BasketBallPlayer> bullsPlayers = newArrayList(noah, rose); // potentialMvp is a Condition<BasketBallPlayer> assertThat(bullsPlayers).haveAtLeastOne(potentialMvp);
haveAtLeastOne
in interface ObjectEnumerableAssert<AbstractObjectArrayAssert<S extends AbstractObjectArrayAssert<S,T>,T>,T>
ObjectEnumerableAssert.haveAtLeast(int, Condition)
public S haveAtLeast(int times, Condition<? super T> condition)
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)
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)
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 elements
this
assertion object.public S containsAll(Iterable<? extends T> iterable)
Iterable
, in any order.
containsAll
in interface ObjectEnumerableAssert<AbstractObjectArrayAssert<S extends AbstractObjectArrayAssert<S,T>,T>,T>
iterable
- the given Iterable
we will get elements from.
this
assertion object.public S usingElementComparator(Comparator<? super T> customComparator)
EnumerableAssert
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(invoiceList).usingComparator(invoicePayeeComparator).isEqualTo(expectedInvoiceList). // compares invoices by date, doesNotHaveDuplicates and contains both use the given invoice date comparator assertThat(invoiceList).usingComparator(invoiceDateComparator).doesNotHaveDuplicates().contains(may2010Invoice) // as assertThat(invoiceList) creates a new assertion, it falls back to standard comparison strategy // based on Invoice's equal method to compare invoiceList elements to lowestInvoice. assertThat(invoiceList).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.public S usingDefaultElementComparator()
EnumerableAssert
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 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 field with the given name is looked for first, if it is not accessible (ie. does not exist or is not public) then a property with the given name is looked for.
It works only if all objects have the field or all objects have the property with the given name, i.e. it won't work if half of the objects have the field and the other the property.
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 test
IntrospectionError
- if no field or property exists with the given name (or field exists but is not public)public <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 field with the given name is looked for first, if it is not accessible (ie. does not exist or is not public) then a property with the given name is looked for.
It works only if all objects have the field or all objects have the property with the given name, i.e. it won't work if half of the objects have the field and the other the property.
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 return
IntrospectionError
- if no field or property exists with the given name (or field exists but is not public)public 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 doesn't exist then a field with the given name is looked for, if no field accessible (ie. does not exist or is not public) an IntrospectionError is thrown. It only works if all objects have the field or all objects have the property with the given name, i.e. it won't work if half of the objects have the field and the other the property. 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 test
IntrospectionError
- if one of the given name does not match a field or property (or field exists but is not
public) 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 one
public <U,C extends Collection<U>> ObjectArrayAssert<U> flatExtracting(Extractor<? super T,C> extractor)
public ObjectArrayAssert<Object> extractingResultOf(String method)
It allows you to test a method reslts of the array's elements instead of testing the elements themselves, it can be sometimes much less work!
It is especially usefull 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 Comming"), 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 great houses in Westeros: assertThat(greatHousesOfWesteros).extractingResultOf("sayTheWords") .contains("Winter is Comming", "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 test
IllegalArgumentException
- 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 reslts of the array's elements instead of testing the elements themselves, it can be sometimes much less work!
It is especially usefull 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 Comming"), 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 great houses in Westeros: assertThat(greatHousesOfWesteros).extractingResultOf("sayTheWords", String.class) .contains("Winter is Comming", "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 return
IllegalArgumentException
- 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.
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |