Class IterableSubject.UsingCorrespondence<A extends @Nullable Object, E extends @Nullable Object>

java.lang.Object
com.google.common.truth.IterableSubject.UsingCorrespondence<A,E>
Direct Known Subclasses:
PrimitiveDoubleArraySubject.DoubleArrayAsIterable, PrimitiveFloatArraySubject.FloatArrayAsIterable
Enclosing class:
IterableSubject

public static class IterableSubject.UsingCorrespondence<A extends @Nullable Object, E extends @Nullable Object> extends Object
A partially specified check in which the actual elements (normally the elements of the 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.
Author:
Kurt Alfred Kluever, Pete Gillin
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    contains(E expected)
    Checks that the actual iterable contains at least one element that corresponds to the given expected element.
    void
    containsAnyIn(E @Nullable [] expected)
    Checks that the actual iterable contains at least one element that corresponds to at least one of the expected elements.
    void
    containsAnyIn(Iterable<? extends E> expected)
    Checks that the actual iterable contains at least one element that corresponds to at least one of the expected elements.
    final void
    containsAnyOf(E first, E second, E @Nullable ... rest)
    Checks that the actual iterable contains at least one element that corresponds to at least one of the expected elements.
    final Ordered
    containsAtLeast(E first, E second, E @Nullable ... rest)
    Checks that the actual iterable contains elements that correspond to all the expected elements, i.e. that there is a 1:1 mapping between any subset of the actual elements and the expected elements where each pair of elements correspond.
    Checks that the actual iterable contains elements that correspond to all the expected elements, i.e. that there is a 1:1 mapping between any subset of the actual elements and the expected elements where each pair of elements correspond.
    containsAtLeastElementsIn(Iterable<? extends E> expected)
    Checks that the actual iterable contains elements that correspond to all the expected elements, i.e. that there is a 1:1 mapping between any subset of the actual elements and the expected elements where each pair of elements correspond.
    final Ordered
    Checks that actual iterable contains exactly elements that correspond to the expected elements, i.e. that there is a 1:1 mapping between the actual elements and the expected elements where each pair of elements correspond.
    Checks that actual iterable contains exactly elements that correspond to the expected elements, i.e. that there is a 1:1 mapping between the actual elements and the expected elements where each pair of elements correspond.
    Checks that actual iterable contains exactly elements that correspond to the expected elements, i.e. that there is a 1:1 mapping between the actual elements and the expected elements where each pair of elements correspond.
    void
    containsNoneIn(E @Nullable [] 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 actual iterable contains no elements that correspond to any of the given elements.
    final void
    containsNoneOf(E first, E second, E @Nullable ... rest)
    Checks that the actual iterable contains no elements that correspond to any of the given elements.
    displayingDiffsPairedBy(Function<? super A, ?> actualKeyFunction, Function<? super E, ?> expectedKeyFunction)
    Specifies a way to pair up unexpected and missing elements in the message when an assertion fails.
    displayingDiffsPairedBy(Function<? super E, ?> keyFunction)
    Specifies a way to pair up unexpected and missing elements in the message when an assertion fails.
    void
    doesNotContain(E element)
    Checks that none of the actual elements correspond to the given element.
    final boolean
    Deprecated.
    Object.equals(Object) is not supported on Truth subjects or intermediate classes.
    final int
    Deprecated.
    Object.hashCode() is not supported on Truth types.
    final String
    Deprecated.
    Object.toString() is not supported on Truth subjects.

    Methods inherited from class java.lang.Object

    clone, finalize, getClass, notify, notifyAll, wait, wait, wait
  • Method Details

    • equals

      @Deprecated public final boolean equals(@Nullable Object other)
      Deprecated.
      Object.equals(Object) is not supported on Truth subjects or intermediate classes. If you are writing a test assertion (actual vs. expected), use methods liks containsExactlyElementsIn(Iterable) instead.
      Overrides:
      equals in class Object
      Throws:
      UnsupportedOperationException - always
    • hashCode

      @Deprecated public final int hashCode()
      Deprecated.
      Object.hashCode() is not supported on Truth types.
      Overrides:
      hashCode in class Object
      Throws:
      UnsupportedOperationException - always
    • toString

      @Deprecated public final String toString()
      Deprecated.
      Object.toString() is not supported on Truth subjects.
      Overrides:
      toString in class Object
      Throws:
      UnsupportedOperationException - always
    • displayingDiffsPairedBy

      public IterableSubject.UsingCorrespondence<A,E> displayingDiffsPairedBy(Function<? super E, ?> keyFunction)
      Specifies a way to pair up unexpected and missing elements in the message when an assertion fails. For example:
      
       assertThat(actualRecords)
           .comparingElementsUsing(RECORD_CORRESPONDENCE)
           .displayingDiffsPairedBy(MyRecord::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, ?> as well as Function<? super E, ?>. 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 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.

      If the apply method on the key function throws an exception then the element will be treated as if it had a null key and not paired. (The first such exception will be noted in the failure message.)

      Note that calling this method makes no difference to whether a test passes or fails, it just improves the message if it fails.

    • displayingDiffsPairedBy

      public IterableSubject.UsingCorrespondence<A,E> displayingDiffsPairedBy(Function<? super A, ?> actualKeyFunction, Function<? super E, ?> expectedKeyFunction)
      Specifies a way to pair up unexpected and missing elements in the message when an assertion fails. For example:
      
       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 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.

      If the apply method on either of the key functions throws an exception then the element will be treated as if it had a null key and not paired. (The first such exception will be noted in the failure message.)

      Note that calling this method makes no difference to whether a test passes or fails, it just improves the message if it fails.

    • contains

      public void contains(E expected)
      Checks that the actual iterable contains at least one element that corresponds to the given expected element.
    • doesNotContain

      public void doesNotContain(E element)
      Checks that none of the actual elements correspond to the given element.
    • containsExactly

      @SafeVarargs @CanIgnoreReturnValue public final Ordered containsExactly(@Nullable E @Nullable ... expected)
      Checks that actual iterable contains exactly elements that correspond to the expected elements, i.e. that there is a 1:1 mapping between the actual elements and the expected elements where each pair of elements correspond.

      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.

    • containsExactlyElementsIn

      @CanIgnoreReturnValue public Ordered containsExactlyElementsIn(@Nullable Iterable<? extends E> expected)
      Checks that actual iterable contains exactly elements that correspond to the expected elements, i.e. that there is a 1:1 mapping between the actual elements and the expected elements where each pair of elements correspond.

      To also test that the contents appear in the given order, make a call to inOrder() on the object returned by this method.

    • containsExactlyElementsIn

      @CanIgnoreReturnValue public Ordered containsExactlyElementsIn(E @Nullable [] expected)
      Checks that actual iterable contains exactly elements that correspond to the expected elements, i.e. that there is a 1:1 mapping between the actual elements and the expected elements where each pair of elements correspond.

      To also test that the contents appear in the given order, make a call to inOrder() on the object returned by this method.

    • containsAtLeast

      @SafeVarargs @CanIgnoreReturnValue public final Ordered containsAtLeast(E first, E second, E @Nullable ... rest)
      Checks that the actual iterable contains elements that correspond to all the expected elements, i.e. that there is a 1:1 mapping between any subset of the actual elements and the expected elements where each pair of elements correspond.

      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 actual iterable, but they are not required to be consecutive.

    • containsAtLeastElementsIn

      @CanIgnoreReturnValue public Ordered containsAtLeastElementsIn(Iterable<? extends E> expected)
      Checks that the actual iterable contains elements that correspond to all the expected elements, i.e. that there is a 1:1 mapping between any subset of the actual elements and the expected elements where each pair of elements correspond.

      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 actual iterable, but they are not required to be consecutive.

    • containsAtLeastElementsIn

      @CanIgnoreReturnValue public Ordered containsAtLeastElementsIn(E @Nullable [] expected)
      Checks that the actual iterable contains elements that correspond to all the expected elements, i.e. that there is a 1:1 mapping between any subset of the actual elements and the expected elements where each pair of elements correspond.

      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 actual iterable, but they are not required to be consecutive.

    • containsAnyOf

      @SafeVarargs public final void containsAnyOf(E first, E second, E @Nullable ... rest)
      Checks that the actual iterable contains at least one element that corresponds to at least one of the expected elements.
    • containsAnyIn

      public void containsAnyIn(Iterable<? extends E> expected)
      Checks that the actual iterable contains at least one element that corresponds to at least one of the expected elements.
    • containsAnyIn

      public void containsAnyIn(E @Nullable [] expected)
      Checks that the actual iterable contains at least one element that corresponds to at least one of the expected elements.
    • containsNoneOf

      @SafeVarargs public final void containsNoneOf(E first, E second, E @Nullable ... rest)
      Checks that the actual iterable contains no elements that correspond to any of the given elements.
    • containsNoneIn

      public void containsNoneIn(Iterable<? extends E> excluded)
      Checks that the actual iterable contains no elements that correspond to any of the given elements.
    • containsNoneIn

      public void containsNoneIn(E @Nullable [] excluded)
      Checks that the subject contains no elements that correspond to any of the given elements.