public static class IterableSubject.UsingCorrespondence<A,E> extends Object
Iterable
under test) are compared to expected elements using a Correspondence
. The
expected elements are of type E
. Call methods on this object to actually execute the
check.Modifier and Type | Method and Description |
---|---|
void |
contains(E expected)
Checks that the subject contains at least one element that corresponds to the given expected
element.
|
Ordered |
containsAllIn(E[] expected)
Checks that the subject contains elements that corresponds to all of the expected elements,
i.e.
|
Ordered |
containsAllIn(Iterable<? extends E> expected)
Checks that the subject contains elements that corresponds to all of the expected elements,
i.e.
|
Ordered |
containsAllOf(E first,
E second,
E... rest)
Checks that the subject contains elements that corresponds to all of the expected elements,
i.e.
|
void |
containsAnyIn(E[] expected)
Checks that the subject contains at least one element that corresponds to at least one of the
expected elements.
|
void |
containsAnyIn(Iterable<? extends E> expected)
Checks that the subject contains at least one element that corresponds to at least one of the
expected elements.
|
void |
containsAnyOf(E first,
E second,
E... rest)
Checks that the subject contains at least one element that corresponds to at least one of the
expected elements.
|
Ordered |
containsExactly(E... expected)
Checks that subject contains exactly elements that correspond to the expected elements, i.e.
|
Ordered |
containsExactlyElementsIn(E[] expected)
Checks that subject contains exactly elements that correspond to the expected elements, i.e.
|
Ordered |
containsExactlyElementsIn(Iterable<? extends E> expected)
Checks that subject contains exactly elements that correspond to the expected elements, i.e.
|
void |
containsNoneIn(E[] excluded)
Checks that the subject contains no elements that correspond to any of the given elements.
|
void |
containsNoneIn(Iterable<? extends E> excluded)
Checks that the subject contains no elements that correspond to any of the given elements.
|
void |
containsNoneOf(E firstExcluded,
E secondExcluded,
E... restOfExcluded)
Checks that the subject contains no elements that correspond to any of the given elements.
|
IterableSubject.UsingCorrespondence<A,E> |
displayingDiffsPairedBy(Function<? super A,? extends Object> actualKeyFunction,
Function<? super E,? extends Object> expectedKeyFunction)
Specifies a way to pair up unexpected and missing elements in the message when an assertion
fails.
|
IterableSubject.UsingCorrespondence<A,E> |
displayingDiffsPairedBy(Function<? super E,? extends Object> keyFunction)
Specifies a way to pair up unexpected and missing elements in the message when an assertion
fails.
|
void |
doesNotContain(E excluded)
Checks that none of the actual elements correspond to the given element.
|
public IterableSubject.UsingCorrespondence<A,E> displayingDiffsPairedBy(Function<? super E,? extends Object> keyFunction)
assertThat(actualRecords)
.comparingElementsUsing(RECORD_CORRESPONDENCE)
.displayingDiffsPairedBy(Record::getId)
.containsExactlyElementsIn(expectedRecords);
Important: The {code keyFunction} function must be able to accept both the actual
and the unexpected elements, i.e. it must satisfy Function<? super A, ? extends
Object>
as well as Function<? super E, ? extends Object>
. If that constraint is not
met then a subsequent method may throw ClassCastException
. Use the two-parameter
overload if you need to specify different key functions for the actual and expected elements.
On assertions where it makes sense to do so, the elements are paired as follows: they are
keyed by keyFunction
, and if an unexpected element and a missing element have the
same non-null key then the they are paired up. (Elements with null keys are not paired.) The
failure message will show paired elements together, and a diff will be shown if the Correspondence.formatDiff(A, E)
method returns non-null.
The expected elements given in the assertion should be uniquely keyed by keyFunction
. If multiple missing elements have the same key then the pairing will be
skipped.
Useful key functions will have the property that key equality is less strict than the
correspondence, i.e. given actual
and expected
values with keys actualKey
and expectedKey
, if correspondence.compare(actual, expected)
is
true then it is guaranteed that actualKey
is equal to expectedKey
, but there
are cases where actualKey
is equal to expectedKey
but correspondence.compare(actual, expected)
is false.
Note that calling this method makes no difference to whether a test passes or fails, it just improves the message if it fails.
public IterableSubject.UsingCorrespondence<A,E> displayingDiffsPairedBy(Function<? super A,? extends Object> actualKeyFunction, Function<? super E,? extends Object> expectedKeyFunction)
assertThat(actualFoos)
.comparingElementsUsing(FOO_BAR_CORRESPONDENCE)
.displayingDiffsPairedBy(Foo::getId, Bar::getFooId)
.containsExactlyElementsIn(expectedBar);
On assertions where it makes sense to do so, the elements are paired as follows: the
unexpected elements are keyed by actualKeyFunction
, the missing elements are keyed by
expectedKeyFunction
, and if an unexpected element and a missing element have the same
non-null key then the they are paired up. (Elements with null keys are not paired.) The
failure message will show paired elements together, and a diff will be shown if the Correspondence.formatDiff(A, E)
method returns non-null.
The expected elements given in the assertion should be uniquely keyed by expectedKeyFunction
. If multiple missing elements have the same key then the pairing will be
skipped.
Useful key functions will have the property that key equality is less strict than the
correspondence, i.e. given actual
and expected
values with keys actualKey
and expectedKey
, if correspondence.compare(actual, expected)
is
true then it is guaranteed that actualKey
is equal to expectedKey
, but there
are cases where actualKey
is equal to expectedKey
but correspondence.compare(actual, expected)
is false.
Note that calling this method makes no difference to whether a test passes or fails, it just improves the message if it fails.
public void contains(@NullableDecl E expected)
public void doesNotContain(@NullableDecl E excluded)
@SafeVarargs @CanIgnoreReturnValue public final Ordered containsExactly(@NullableDecl E... expected)
To also test that the contents appear in the given order, make a call to inOrder()
on the object returned by this method.
To test that the iterable contains the elements corresponding to those in an array, prefer
containsExactlyElementsIn(Object[])
. It makes clear that the given array is a list
of elements, not an element itself. This helps human readers and avoids a compiler warning.
@CanIgnoreReturnValue public Ordered containsExactlyElementsIn(Iterable<? extends E> expected)
To also test that the contents appear in the given order, make a call to inOrder()
on the object returned by this method.
@CanIgnoreReturnValue public Ordered containsExactlyElementsIn(E[] expected)
To also test that the contents appear in the given order, make a call to inOrder()
on the object returned by this method.
@SafeVarargs @CanIgnoreReturnValue public final Ordered containsAllOf(@NullableDecl E first, @NullableDecl E second, @NullableDecl E... rest)
To also test that the contents appear in the given order, make a call to inOrder()
on the object returned by this method. The elements must appear in the given order within the
subject, but they are not required to be consecutive.
@CanIgnoreReturnValue public Ordered containsAllIn(Iterable<? extends E> expected)
To also test that the contents appear in the given order, make a call to inOrder()
on the object returned by this method. The elements must appear in the given order within the
subject, but they are not required to be consecutive.
@CanIgnoreReturnValue public Ordered containsAllIn(E[] expected)
To also test that the contents appear in the given order, make a call to inOrder()
on the object returned by this method. The elements must appear in the given order within the
subject, but they are not required to be consecutive.
@SafeVarargs public final void containsAnyOf(@NullableDecl E first, @NullableDecl E second, @NullableDecl E... rest)
public void containsAnyIn(Iterable<? extends E> expected)
public void containsAnyIn(E[] expected)
@SafeVarargs public final void containsNoneOf(@NullableDecl E firstExcluded, @NullableDecl E secondExcluded, @NullableDecl E... restOfExcluded)
public void containsNoneIn(Iterable<? extends E> excluded)
public void containsNoneIn(E[] excluded)
Copyright © 2018. All rights reserved.