truth icon indicating copy to clipboard operation
truth copied to clipboard

Wanted: Truth8Junit to support Java 8 types with assume()

Open kaibolay opened this issue 3 years ago • 2 comments

I found myself wanting to use a java.util.Optional with TruthJunit.assume():

    assume().that(javaUtilOptional).isPresent();

Unfortunately this doesn't work, since TruthJunit doesn't support Java 8 types. Hence this feature request: It's be nice to have a Truth8Junit (analogous to Truth8) to fill this gap.

kaibolay avatar Dec 15 '22 16:12 kaibolay

You can do this with:

import static com.google.common.truth.OptionalSubject.optionals;

assume().about(optionals()).that(javaUtilOptional).isPresent();

This is somewhat documented in https://truth.dev/faq#full-chain, but we were just recently discussing that we should mention it in https://truth.dev/faq#missing-import, too.

The longer-term plan is to make support for java.util.Optional part of core Truth (https://github.com/google/truth/issues/746) as part of dropping support for Java 7.

Sorry that it's confusing and verbose in the meantime.

cpovirk avatar Dec 15 '22 16:12 cpovirk

Thanks!

That workaround is certainly better than what I did:

assume().that(javaUtilOptional.isPresent()).isTrue();

Looking forward to #746,

kaibolay avatar Dec 18 '22 01:12 kaibolay

Belatedly closing, given that #746 was resolved in Truth 1.4.2.

cpovirk avatar Apr 17 '24 15:04 cpovirk