Skip to content

Commit

Permalink
Update docs to reflect that the Java 8 assertions have "moved" to the…
Browse files Browse the repository at this point in the history
… main `Truth` class.

This includes deprecating the old class.

This continues work on #746.

RELNOTES=Deprecated `Truth8`. All its functionality is now supported through the main `Truth` API.
PiperOrigin-RevId: 603756994
  • Loading branch information
cpovirk authored and Google Java Core Libraries committed Feb 6, 2024
1 parent 2e96a2e commit 780928f
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 56 deletions.
7 changes: 2 additions & 5 deletions expect.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,18 +58,15 @@ Expect rule.

## What about non-standard test subjects (Truth extensions)?

If you use [Truth extensions] such as [`ProtoTruth`] and [`Truth8`],
`expect.that()` needs to know about the subject to provide the same methods:
If you use [Truth extensions] such as [`ProtoTruth`], `expect.that()` needs to
know about the subject to provide the same methods:

```java
import static com.google.common.truth.OptionalSubject.optionals;
import static com.google.common.truth.extensions.proto.ProtoTruth.protos;

expect.about(optionals()).that(myOptional).hasValue(2);
expect.about(protos()).that(myProto).isEqualToDefaultInstance();
```

[Truth]: https://truth.dev
[Truth extensions]: https://truth.dev/extension
[`ProtoTruth`]: https://truth.dev/protobufs
[`Truth8`]: faq#java8
4 changes: 1 addition & 3 deletions extension.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ we'll cover in the rest of this page.
Some subjects aren't part of core Truth but can be found in other parts of the
project. They include:

* [`Truth8`] for java8 types such as `java.util.Optional`
* [`ProtoTruth`] for `Message` style protocol buffers and lite versions

Other extensions that are not part of the Truth project itself include:
Expand Down Expand Up @@ -181,7 +180,7 @@ There are four parts to the example:

We recommend putting this method on your `Subject` class itself. Or, if
your library defines multiple `Subject` subclasses, you may wish to
create a single class (like [`Truth8`]) that contains all your
create a single class (like [`ProtoTruth`]) that contains all your
`assertThat` methods so that users can access them all with a single
static import.

Expand Down Expand Up @@ -383,4 +382,3 @@ There are four parts to the example:
[`Re2jSubjects`]: https://truth.dev/api/latest/com/google/common/truth/extensions/re2j/Re2jSubjects.html
[`Subject.Factory`]: https://truth.dev/api/latest/com/google/common/truth/Subject.Factory.html
[`Subject`]: https://truth.dev/api/latest/com/google/common/truth/Subject.html
[`Truth8`]: https://truth.dev/api/latest/com/google/common/truth/Truth8.html
49 changes: 17 additions & 32 deletions faq.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,43 +8,27 @@ title: Truth FAQ

## How do I use Truth with the Java 8 types? {#java8}

First, make sure you're depending on
`com.google.truth.extensions:truth-java8-extension:<your truth version>`
Assertions on Java 8
types are available through overloads of `Truth.assertThat`, as well as
overloads of the `that` method used in chains like
`assertWithMessage(...).that(stream).isEmpty()`.

You will usually need *both* of the following static imports:
This also means that you no longer need a dependency on
`com.google.truth.extensions:truth-java8-extension:`.

```java
import static com.google.common.truth.Truth.assertThat;
import static com.google.common.truth.Truth8.assertThat;
...
// This assertion uses Truth8.assertThat(Optional).
Optional<String> javaUtilOptional = someStream.map(...).filter(...).findFirst();
assertThat(javaUtilOptional).hasValue("duke");

// Other assertions will use the various Truth.assertThat(...) overloads.
```

To use the Java 8 types with `assertWithMessage`, `expect`, or other entry
points, use `about`. For example, for `Optional`:

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

assertWithMessage(...).about(optionals()).that(javaUtilOptional).hasValue("duke");
```

For more information, read about [the full fluent chain](#full-chain).
Assertions about other types (like protobuf types) still sometimes require that
you use [the full fluent chain](#full-chain).

## Why do I get a "`cannot find symbol .hasValue("foo");`" error for a type that should have `hasValue`? {#missing-import}
## Why do I get a "`cannot find symbol .comparingExpectedFieldsOnly();`" error for a type that should have `comparingExpectedFieldsOnly`? {#missing-import}

You need to static import the `assertThat` method for that type. (For example,
you might need to [import the method for Java 8 types](#java8).)
You need to static import the `assertThat` method for that type. (In this
example, you probably need to import [`ProtoTruth.assertThat`].)

The error you're seeing is telling you that `hasValue` doesn't exist on the base
`Subject` class, the one returned when you pass a type that Truth doesn't
recognize to `Truth.assertThat`. Once you import the `assertThat` method that
you want, your call will resolve to that method, and you'll have an instance of
the type with the method you're looking for.
The error you're seeing is telling you that `comparingExpectedFieldsOnly`
doesn't exist on the base `Subject` class, the one returned when you pass a type
that Truth doesn't recognize to `Truth.assertThat`. Once you import the
`assertThat` method that you want, your call will resolve to that method, and
you'll have an instance of the type with the method you're looking for.

## What if I have an import conflict with another `assertThat()` method? {#imports}

Expand Down Expand Up @@ -309,3 +293,4 @@ For a list of built-in behaviors, see the docs on [`FailureStrategy`].

[Ask your question here]: http://stackoverflow.com/questions/ask?tags=google-truth
[`FailureStrategy`]: https://truth.dev/api/latest/com/google/common/truth/FailureStrategy.html
[`ProtoTruth.assertThat`]: https://truth.dev/api/latest/com/google/common/truth/extensions/proto/ProtoTruth.html#assertThat(com.google.protobuf.Message)
6 changes: 0 additions & 6 deletions index.md
Original file line number Diff line number Diff line change
Expand Up @@ -144,9 +144,6 @@ dependencies {
}
```

To use the Java 8 extensions, also include
`com.google.truth.extensions:truth-java8-extension:{{ site.version }}`.

One warning: Truth depends on the "Android" version of [Guava], a subset of the
"JRE" version. If your project uses the JRE version, be aware that your build
system might select the Android version instead. If so, you may see "missing
Expand All @@ -161,9 +158,6 @@ checks for many kinds of bugs, including some we've seen in usages of Truth.
```java
import static com.google.common.truth.Truth.assertThat;
import static com.google.common.truth.Truth.assertWithMessage;

// for assertions on Java 8 types (Streams and java.util.Optional)
import static com.google.common.truth.Truth8.assertThat;
```

## 3. Write a test assertion:
Expand Down
15 changes: 5 additions & 10 deletions known_types.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,6 @@ Truth has built in support for the following types:
* [`long[]`][LongArray]
* [`short[]`][ShortArray]

* [Java 8 types]

* [`Optional`] - and `OptionalInt`, `OptionalLong`, and `OptionalDouble`
* [`Stream`] - and `IntStream`, `LongStream` (and maybe someday
`DoubleStream`)

* Other JDK types

* [`Object`] - since all types extend `Object`, you can make simple
Expand All @@ -40,6 +34,9 @@ Truth has built in support for the following types:
`BigInteger`, etc.)
* [`Iterable`] - this can be used for any `Iterable` type (`List`, `Set`,
etc.)
* [`Optional`] - and `OptionalInt`, `OptionalLong`, and `OptionalDouble`
* [`Stream`] - and `IntStream`, `LongStream` (and maybe someday
`DoubleStream`)
* [`Map`]
* [`Throwable`]
* [`Class`]
Expand All @@ -58,8 +55,6 @@ assertions on in this list, you can

<!-- References -->

<!-- TODO(kak): Update the 2 Java 8 links once they have public javadocs -->

[BooleanArray]: https://truth.dev/api/latest/com/google/common/truth/PrimitiveBooleanArraySubject
[ByteArray]: https://truth.dev/api/latest/com/google/common/truth/PrimitiveByteArraySubject
[CharacterArray]: https://truth.dev/api/latest/com/google/common/truth/PrimitiveCharArraySubject
Expand All @@ -82,10 +77,10 @@ assertions on in this list, you can
[`Multimap`]: https://truth.dev/api/latest/com/google/common/truth/MultimapSubject
[`Multiset`]: https://truth.dev/api/latest/com/google/common/truth/MultisetSubject
[`Object`]: https://truth.dev/api/latest/com/google/common/truth/Subject
[`Optional`]: https://truth.dev/api/latest/com/google/common/truth/OptionalSubject.html
[`Optional`]: https://truth.dev/api/latest/com/google/common/truth/OptionalSubject
[`SortedMap`]: https://truth.dev/api/latest/com/google/common/truth/SortedMapSubject
[`SortedSet`]: https://truth.dev/api/latest/com/google/common/truth/SortedSetSubject
[`Stream`]: https://truth.dev/api/latest/com/google/common/truth/StreamSubject.html
[`Stream`]: https://truth.dev/api/latest/com/google/common/truth/StreamSubject
[`String`]: https://truth.dev/api/latest/com/google/common/truth/StringSubject
[`Table`]: https://truth.dev/api/latest/com/google/common/truth/TableSubject
[`Throwable`]: https://truth.dev/api/latest/com/google/common/truth/ThrowableSubject
Expand Down

0 comments on commit 780928f

Please # to comment.