Class OptionalSubject

  • public final class OptionalSubject
    extends Subject
    Propositions for Java 8 Optional subjects.
    1.3.0 (previously part of truth-java8-extension)
    Christian Gruber
    • Method Detail

      • isPresent

        public void isPresent()
        Fails if the Optional<T> is empty or the subject is null.
      • isEmpty

        public void isEmpty()
        Fails if the Optional<T> is present or the subject is null.
      • hasValue

        public void hasValue​(@Nullable java.lang.Object expected)
        Fails if the Optional<T> does not have the given value or the subject is null.

        To make more complex assertions on the optional's value split your assertion in two:

      • optionals

        public static Subject.Factory<OptionalSubject,​java.util.Optional<?>> optionals()
        Instead of about(optionals()).that(...), use just that(...). Similarly, instead of assertAbout(optionals()).that(...), use just assertThat(...).
        Obsolete factory instance. This factory was previously necessary for assertions like assertWithMessage(...).about(paths()).that(path)..... Now, you can perform assertions like that without the about(...) call.