Class StreamSubject


  • public final class StreamSubject
    extends Subject
    Propositions for Stream subjects.

    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 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)
    Author:
    Kurt Alfred Kluever
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods 
      Modifier and Type Method Description
      protected java.lang.String actualCustomStringRepresentation()
      Supplies the direct string representation of the actual value to other methods which may prefix or otherwise position it in an error message.
      void contains​(@Nullable java.lang.Object element)
      Fails if the subject does not contain the given element.
      void containsAnyIn​(java.lang.Iterable<?> expected)
      Fails if the subject does not contain at least one of the given elements.
      void containsAnyOf​(@Nullable java.lang.Object first, @Nullable java.lang.Object second, @Nullable java.lang.Object @Nullable ... rest)
      Fails if the subject does not contain at least one of the given elements.
      Ordered containsAtLeast​(@Nullable java.lang.Object first, @Nullable java.lang.Object second, @Nullable java.lang.Object @Nullable ... rest)
      Fails if the subject does not contain all of the given elements.
      Ordered containsAtLeastElementsIn​(java.lang.Iterable<?> expected)
      Fails if the subject does not contain all of the given elements.
      Ordered containsExactly​(@Nullable java.lang.Object @Nullable ... varargs)
      Fails if the subject does not contain exactly the given elements.
      Ordered containsExactlyElementsIn​(java.lang.Iterable<?> expected)
      Fails if the subject does not contain exactly the given elements.
      void containsNoDuplicates()
      Fails if the subject contains duplicate elements.
      void containsNoneIn​(java.lang.Iterable<?> excluded)
      Fails if the subject contains any of the given elements.
      void containsNoneOf​(@Nullable java.lang.Object first, @Nullable java.lang.Object second, @Nullable java.lang.Object @Nullable ... rest)
      Fails if the subject contains any of the given elements.
      void doesNotContain​(@Nullable java.lang.Object element)
      Fails if the subject contains the given element.
      void hasSize​(int expectedSize)
      Fails if the subject does not have the given size.
      void isEmpty()
      Fails if the subject is not empty.
      void isEqualTo​(@Nullable java.lang.Object expected)
      Deprecated.
      streamA.isEqualTo(streamB) always fails, except when passed the exact same stream reference.
      void isInOrder()
      Fails if the subject is not ordered, according to the natural ordering of its elements.
      void isInOrder​(java.util.Comparator<?> comparator)
      Fails if the subject is not ordered, according to the given comparator.
      void isInStrictOrder()
      Fails if the subject is not strictly ordered, according to the natural ordering of its elements.
      void isInStrictOrder​(java.util.Comparator<?> comparator)
      Fails if the subject is not strictly ordered, according to the given comparator.
      void isNotEmpty()
      Fails if the subject is empty.
      void isNotEqualTo​(@Nullable java.lang.Object unexpected)
      Deprecated.
      streamA.isNotEqualTo(streamB) always passes, except when passed the exact same stream reference.
      static Subject.Factory<StreamSubject,​java.util.stream.Stream<?>> streams()
      Deprecated.
      Instead of about(streams()).that(...), use just that(...).
      • Methods inherited from class java.lang.Object

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

      • actualCustomStringRepresentation

        protected java.lang.String actualCustomStringRepresentation()
        Description copied from class: Subject
        Supplies the direct string representation of the actual value to other methods which may prefix or otherwise position it in an error message. This should only be overridden to provide an improved string representation of the value under test, as it would appear in any given error message, and should not be used for additional prefixing.

        Subjects should override this with care.

        By default, this returns String.ValueOf(getActualValue()).

        Overrides:
        actualCustomStringRepresentation in class Subject
      • streams

        @Deprecated
        public static Subject.Factory<StreamSubject,​java.util.stream.Stream<?>> streams()
        Deprecated.
        Instead of about(streams()).that(...), use just that(...). Similarly, instead of assertAbout(streams()).that(...), use just assertThat(...).
        Obsolete factory instance. This factory was previously necessary for assertions like assertWithMessage(...).about(streams()).that(stream)..... Now, you can perform assertions like that without the about(...) call.
      • isEmpty

        public void isEmpty()
        Fails if the subject is not empty.
      • isNotEmpty

        public void isNotEmpty()
        Fails if the subject is empty.
      • hasSize

        public void hasSize​(int expectedSize)
        Fails if the subject does not have the given size.

        If you'd like to check that your stream contains more than Integer.MAX_VALUE elements, use assertThat(stream.count()).isEqualTo(...).

      • contains

        public void contains​(@Nullable java.lang.Object element)
        Fails if the subject does not contain the given element.
      • doesNotContain

        public void doesNotContain​(@Nullable java.lang.Object element)
        Fails if the subject contains the given element.
      • containsNoDuplicates

        public void containsNoDuplicates()
        Fails if the subject contains duplicate elements.
      • containsAnyOf

        public void containsAnyOf​(@Nullable java.lang.Object first,
                                  @Nullable java.lang.Object second,
                                  @Nullable java.lang.Object @Nullable ... rest)
        Fails if the subject does not contain at least one of the given elements.
      • containsAnyIn

        public void containsAnyIn​(java.lang.Iterable<?> expected)
        Fails if the subject does not contain at least one of the given elements.
      • containsAtLeast

        @CanIgnoreReturnValue
        public Ordered containsAtLeast​(@Nullable java.lang.Object first,
                                       @Nullable java.lang.Object second,
                                       @Nullable java.lang.Object @Nullable ... rest)
        Fails if the subject does not contain 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

        @CanIgnoreReturnValue
        public Ordered containsAtLeastElementsIn​(java.lang.Iterable<?> expected)
        Fails if the subject does not contain 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

        @CanIgnoreReturnValue
        public Ordered containsExactly​(@Nullable java.lang.Object @Nullable ... varargs)
        Fails if the subject does not contain 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 subject.

        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​(java.lang.Iterable<?> expected)
        Fails if the subject does not contain 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 subject.

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

      • containsNoneOf

        public void containsNoneOf​(@Nullable java.lang.Object first,
                                   @Nullable java.lang.Object second,
                                   @Nullable java.lang.Object @Nullable ... rest)
        Fails if the subject contains any of the given elements. (Duplicates are irrelevant to this test, which fails if any of the actual elements equal any of the excluded.)
      • containsNoneIn

        public void containsNoneIn​(java.lang.Iterable<?> excluded)
        Fails if the subject contains any of the given elements. (Duplicates are irrelevant to this test, which fails if any of the actual elements equal any of the excluded.)
      • isInStrictOrder

        public void isInStrictOrder()
        Fails if the subject is not 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:
        java.lang.ClassCastException - if any pair of elements is not mutually Comparable
        java.lang.NullPointerException - if any element is null
      • isInStrictOrder

        public void isInStrictOrder​(java.util.Comparator<?> comparator)
        Fails if the subject is not 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:
        java.lang.ClassCastException - if any pair of elements is not mutually Comparable
      • isInOrder

        public void isInOrder()
        Fails if the subject is not 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:
        java.lang.ClassCastException - if any pair of elements is not mutually Comparable
        java.lang.NullPointerException - if any element is null
      • isInOrder

        public void isInOrder​(java.util.Comparator<?> comparator)
        Fails if the subject is not 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:
        java.lang.ClassCastException - if any pair of elements is not mutually Comparable
      • isEqualTo

        @Deprecated
        public void isEqualTo​(@Nullable java.lang.Object expected)
        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 using Subject.isSameInstanceAs(java.lang.Object). If you instead want to test the contents of the stream, use containsExactly(java.lang.Object...) or similar methods.
        Description copied from class: Subject
        Fails if the subject is not 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.equal(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, or Long) and they are numerically equal when converted to Long.
        • the actual value is a boxed floating-point type (Double or Float), the expected value is an Integer, and the two are numerically equal when converted to Double. (This allows assertThat(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 an equals implementation 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 as equals is implemented according to the contract for its type.

        Overrides:
        isEqualTo in class Subject