Skip to content


Make it possible to write expect.that(optional).isPresent(), `asser…
Browse files Browse the repository at this point in the history
…tWithMessage(...).that(optional).isPresent()`, etc., including for `Stream` as well as `Optional`.

That is, you no longer need to use `about(optionals())` (or `about(streams())`). (Later, we'll do the same thing for lesser-used APIs like `optionalInts().)

This CL does _not_ make it possible to write `Truth.assertThat(optional).isPresent()`: For that, you still need to use `Truth8`. A future CL will eliminate the need to use `Truth8`.

This continues our work on #746.

RELNOTES=Added `that` overloads to make it possible to write type-specific assertions when using `expect.that(optional)` and `expect.that(stream)`.
PiperOrigin-RevId: 598637400
  • Loading branch information
cpovirk authored and Google Java Core Libraries committed Jan 15, 2024
1 parent f8ecaec commit ca7e8f4
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
Expand Up @@ -24,6 +24,8 @@
import java.math.BigDecimal;
import java.util.Map;
import java.util.Optional;
import org.checkerframework.checker.nullness.qual.Nullable;

Expand Down Expand Up @@ -168,6 +170,19 @@ public final TableSubject that(@Nullable Table<?, ?, ?> actual) {
return new TableSubject(metadata(), actual);

"Java7ApiChecker", // no more dangerous that wherever the user got the Optional
"NullableOptional", // Truth always accepts nulls, no matter the type
public final OptionalSubject that(@Nullable Optional<?> actual) {
return new OptionalSubject(metadata(), actual, "optional");

@SuppressWarnings("Java7ApiChecker") // no more dangerous that wherever the user got the Stream
public final StreamSubject that(@Nullable Stream<?> actual) {
return new StreamSubject(metadata(), actual);

* Returns a new instance that will output the given message before the main failure message. If
* this method is called multiple times, the messages will appear in the order that they were
Expand Down
Expand Up @@ -25,6 +25,8 @@
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.Optional;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
Expand All @@ -45,6 +47,9 @@ public void staticAssertThatMethodsMatchStandardSubjectBuilderInstanceMethods()
ImmutableSortedSet<TypeToken<?>> verbTypes =
.filter(input -> input.getName().equals("that"))
// TODO: b/166630734 - Remove this when we add the assertThat overloads.
.filter(input -> input.getParameterTypes()[0] != Optional.class)
.filter(input -> input.getParameterTypes()[0] != Stream.class)
ImmutableSortedSet<TypeToken<?>> truthTypes =
Expand Down

0 comments on commit ca7e8f4

Please sign in to comment.