diff --git a/expect.md b/expect.md index 5efc49ae4..b6d384d05 100644 --- a/expect.md +++ b/expect.md @@ -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 diff --git a/extension.md b/extension.md index cbddf0257..c7ce988f7 100644 --- a/extension.md +++ b/extension.md @@ -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: @@ -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. @@ -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 diff --git a/faq.md b/faq.md index 90f3286a9..1003a178a 100644 --- a/faq.md +++ b/faq.md @@ -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:` +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 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} @@ -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) diff --git a/index.md b/index.md index 796c0217e..a5c38971d 100644 --- a/index.md +++ b/index.md @@ -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 @@ -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: diff --git a/known_types.md b/known_types.md index 93bf77c96..9cac4f553 100644 --- a/known_types.md +++ b/known_types.md @@ -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 @@ -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`] @@ -58,8 +55,6 @@ assertions on in this list, you can - - [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 @@ -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