Class OptionalSubject

java.lang.Object
com.google.common.truth.Subject
com.google.common.truth.OptionalSubject

public final class OptionalSubject extends Subject
A subject for Optional values.
Since:
1.3.0 (previously part of truth-java8-extension)
Author:
Christian Gruber
  • Method Details

    • isPresent

      public void isPresent()
      Checks that the actual Optional contains a value.
    • isEmpty

      public void isEmpty()
      Checks that the actual Optional does not contain a value.
    • hasValue

      public void hasValue(@Nullable Object expected)
      Checks that the actual Optional contains the given value.

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

      
       assertThat(myOptional).isPresent();
       assertThat(myOptional.get()).contains("foo");
       
    • optionals

      @Deprecated public static Subject.Factory<OptionalSubject, Optional<?>> optionals()
      Deprecated.
      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.