Class StreamSubject
Stream values.
Note: When you perform an assertion based on the contents of the stream, or when any assertion fails, the wrapped stream will be drained immediately into a private collection to provide more readable failure messages. This consumes the stream. Take care if you intend to leave the stream un-consumed or if the stream is very large or infinite.
If you intend to make multiple assertions on the contents of the same stream, you should instead first collect the contents of the stream into a collection and then assert directly on that. For example:
List<Integer> list = makeStream().map(...).filter(...).collect(toImmutableList());
assertThat(list).contains(5);
assertThat(list).doesNotContain(2);
For very large or infinite streams, you may want to first limit the stream before asserting on it.
- Since:
- 1.3.0 (previously part of
truth-java8-extension)
-
Nested Class Summary
Nested classes/interfaces inherited from class com.google.common.truth.Subject
Subject.Factory<SubjectT extends Subject, ActualT> -
Method Summary
Modifier and TypeMethodDescriptionprotected StringReturns a string representation of the actual value for inclusion in failure messages.voidChecks that the actual stream contains the given element.voidcontainsAnyIn(@Nullable Iterable<?> expected) Checks that the actual stream contains at least one of the given elements.voidChecks that the actual stream contains at least one of the given elements.containsAtLeast(@Nullable Object first, @Nullable Object second, @Nullable Object @Nullable ... rest) Checks that the actual stream contains all of the given elements.containsAtLeastElementsIn(@Nullable Iterable<?> expected) Checks that the actual stream contains all of the given elements.containsExactly(@Nullable Object @Nullable ... expected) Checks that the actual stream contains exactly the given elements.containsExactlyElementsIn(@Nullable Iterable<?> expected) Checks that the actual stream contains exactly the given elements.voidChecks that the actual stream does not contain duplicate elements.voidcontainsNoneIn(@Nullable Iterable<?> excluded) Checks that the actual stream does not contain any of the given elements.voidcontainsNoneOf(@Nullable Object first, @Nullable Object second, @Nullable Object @Nullable ... rest) Checks that the actual stream does not contain any of the given elements.voiddoesNotContain(@Nullable Object element) Checks that the actual stream does not contain the given element.voidhasSize(int size) Checks that the actual stream has the given size.voidisEmpty()Checks that the actual stream is empty.voidDeprecated.voidChecks that the actual stream is ordered, according to the natural ordering of its elements.voidisInOrder(Comparator<?> comparator) Checks that the actual stream is ordered, according to the given comparator.voidChecks that the actual stream is strictly ordered, according to the natural ordering of its elements.voidisInStrictOrder(Comparator<?> comparator) Checks that the actual stream is strictly ordered, according to the given comparator.voidChecks that the actual stream is not empty.voidisNotEqualTo(@Nullable Object other) Deprecated.streamA.isNotEqualTo(streamB)always passes, except when passed the exact same stream reference.static Subject.Factory<StreamSubject, Stream<?>> streams()Deprecated.Instead ofabout(streams()).that(...), use justthat(...).Methods inherited from class com.google.common.truth.Subject
check, equals, failWithActual, failWithActual, failWithoutActual, hashCode, ignoreCheck, isAnyOf, isIn, isInstanceOf, isNoneOf, isNotIn, isNotInstanceOf, isNotNull, isNotSameInstanceAs, isNull, isSameInstanceAs, toString
-
Method Details
-
actualCustomStringRepresentation
Description copied from class:SubjectReturns a string representation of the actual value for inclusion in failure messages.Subjects should override this with care.
By default, this method returns
String.valueOf(getActualValue())for most types. It does have some special logic for a few cases, like arrays.- Overrides:
actualCustomStringRepresentationin classSubject
-
streams
Deprecated.Instead ofabout(streams()).that(...), use justthat(...). Similarly, instead ofassertAbout(streams()).that(...), use justassertThat(...).Obsolete factory instance. This factory was previously necessary for assertions likeassertWithMessage(...).about(streams()).that(stream)..... Now, you can perform assertions like that without theabout(...)call. -
isEmpty
public void isEmpty()Checks that the actual stream is empty. -
isNotEmpty
public void isNotEmpty()Checks that the actual stream is not empty. -
hasSize
public void hasSize(int size) Checks that the actual stream has the given size.If you'd like to check that your stream contains more than
Integer.MAX_VALUEelements, useassertThat(stream.count()).isEqualTo(...). -
contains
-
doesNotContain
-
containsNoDuplicates
public void containsNoDuplicates()Checks that the actual stream does not contain duplicate elements. -
containsAnyOf
-
containsAnyIn
-
containsAtLeast
@CanIgnoreReturnValue public Ordered containsAtLeast(@Nullable Object first, @Nullable Object second, @Nullable Object @Nullable ... rest) Checks that the actual stream contains all of the given elements. If an element appears more than once in the given elements, then it must appear at least that number of times in the actual elements.To also test that the contents appear in the given order, make a call to
inOrder()on the object returned by this method. The expected elements must appear in the given order within the actual elements, but they are not required to be consecutive. -
containsAtLeastElementsIn
Checks that the actual stream contains all of the given elements. If an element appears more than once in the given elements, then it must appear at least that number of times in the actual elements.To also test that the contents appear in the given order, make a call to
inOrder()on the object returned by this method. The expected elements must appear in the given order within the actual elements, but they are not required to be consecutive. -
containsExactly
Checks that the actual stream contains exactly the given elements.Multiplicity is respected. For example, an object duplicated exactly 3 times in the parameters asserts that the object must likewise be duplicated exactly 3 times in the actual stream.
To also test that the contents appear in the given order, make a call to
inOrder()on the object returned by this method. -
containsExactlyElementsIn
Checks that the actual stream contains exactly the given elements.Multiplicity is respected. For example, an object duplicated exactly 3 times in the parameters asserts that the object must likewise be duplicated exactly 3 times in the actual stream.
To also test that the contents appear in the given order, make a call to
inOrder()on the object returned by this method. -
containsNoneOf
-
containsNoneIn
-
isInStrictOrder
public void isInStrictOrder()Checks that the actual stream is strictly ordered, according to the natural ordering of its elements. Strictly ordered means that each element in the stream is strictly greater than the element that preceded it.- Throws:
ClassCastException- if any pair of elements is not mutually ComparableNullPointerException- if any element is null
-
isInStrictOrder
Checks that the actual stream is strictly ordered, according to the given comparator. Strictly ordered means that each element in the stream is strictly greater than the element that preceded it.- Throws:
ClassCastException- if any pair of elements is not mutually Comparable
-
isInOrder
public void isInOrder()Checks that the actual stream is ordered, according to the natural ordering of its elements. Ordered means that each element in the stream is greater than or equal to the element that preceded it.- Throws:
ClassCastException- if any pair of elements is not mutually ComparableNullPointerException- if any element is null
-
isInOrder
Checks that the actual stream is ordered, according to the given comparator. Ordered means that each element in the stream is greater than or equal to the element that preceded it.- Throws:
ClassCastException- if any pair of elements is not mutually Comparable
-
isEqualTo
Deprecated.streamA.isEqualTo(streamB)always fails, except when passed the exact same stream reference. If you really want to test object identity, you can eliminate this deprecation warning by usingSubject.isSameInstanceAs(java.lang.Object). If you instead want to test the contents of the stream, usecontainsExactly(java.lang.Object...)or similar methods.Description copied from class:SubjectChecks that the value under test is equal to the given object. For the purposes of this comparison, two objects are equal if any of the following is true:- they are equal according to
Objects.equals(java.lang.Object, java.lang.Object) - they are arrays and are considered equal by the appropriate
Arrays.equals(long[], long[])overload - they are boxed integer types (
Byte,Short,Character,Integer, orLong) and they are numerically equal when converted toLong. - the actual value is a boxed floating-point type (
DoubleorFloat), the expected value is anInteger, and the two are numerically equal when converted toDouble. (This allowsassertThat(someDouble).isEqualTo(0)to pass.)
Note: This method does not test the
Object.equals(java.lang.Object)implementation itself; it assumes that method is functioning correctly according to its contract. Testing anequalsimplementation requires a utility such as guava-testlib's EqualsTester.In some cases, this method might not even call
equals. It may instead perform other tests that will return the same result as long asequalsis implemented according to the contract for its type. - they are equal according to
-
isNotEqualTo
Deprecated.streamA.isNotEqualTo(streamB)always passes, except when passed the exact same stream reference. If you really want to test object identity, you can eliminate this deprecation warning by usingSubject.isNotSameInstanceAs(java.lang.Object). If you instead want to test the contents of the stream, collect both streams to lists and perform assertions likeSubject.isNotEqualTo(java.lang.Object)on them. In some cases, you may be able to useStreamSubjectassertions likedoesNotContain(java.lang.Object).Description copied from class:SubjectChecks that the value under test is not equal to the given object. The meaning of equality is the same as for theSubject.isEqualTo(java.lang.Object)method.- Overrides:
isNotEqualToin classSubject
-
streamA.isEqualTo(streamB)always fails, except when passed the exact same stream reference.