Overview
Truth is similar to AssertJ. An assertion written with either library looks like this:
assertThat(notificationText).contains("testuser@google.com");
assertThat(notificationText).contains("testuser@google.com")
Truth differs significantly from Hamcrest. An assertion written with Hamcrest looks like this:
assertThat(notificationText, containsString("testuser@google.com"));
assertThat(notificationText, containsString("testuser@google.com"))
Why create Truth when AssertJ already exists?
The reason is historical: AssertJ didn’t exist when we started Truth. By the time it was created, we’d begun migrating Google code to Truth, and we’d made some design decisions that would be difficult to retrofit onto AssertJ.
Truth vs. Hamcrest
Because Truth and Hamcrest differ so significantly, we’ll cover only the main points:
- Truth assertions are made with chained method calls, so IDEs can suggest the assertions appropriate for a given object.
- Hamcrest is a more general “matching” library, used not only for making assertions but also for setting expectations on mocking frameworks, with matchers composed together in arbitrary ways. But this flexibility requires complex generics and makes it hard for Hamcrest to produce readable failure messages.
Truth vs. AssertJ
Again, the two are very similar. We prefer Truth for its simpler API:
- Truth provides fewer assertions, while still covering the most common needs of Google’s codebase. Compare:
- Truth aims to provide a single way to perform most tasks. This makes tests easier to understand, and it lets us spend more time improving core features.
We also usually prefer Truth’s failure messages.
Additionally, Truth works on Android devices by default, without requiring users to use an older version or import a different class than usual.
Truth vs. AssertJ, more details
Number of assertion methods
AssertJ has more: more classes of assertions (AssertJ, Truth) and more methods per class (AssertJ, Truth).
It’s easy to understand how every extra feature can be a good thing. We have found, though, that more is not always better:
- When a library has more APIs, it’s harder to find what you’re looking for.
- Users need to understand the behavior of many different methods.
- Users have to choose between multiple ways of doing the same thing.
- Different projects develop their own “dialects,” so an assertion in one project may look different than an equivalent assertion in another.
- Assertions that use a mixture of approaches (e.g., both chained method calls and composable matchers) can be harder to understand that assertions that stick to a single approach.
- As a practical matter, when there are more features, it’s hard to spend as much time on designing each API and failure message. In the worst case, this can lead to puzzlers.
Failure messages
We usually prefer Truth’s. Here’s an example:
value of : projectsByTeam().valuesForKey(corelibs)
missing (1) : truth
───
expected : [guava, dagger, truth, auto, caliper]
but was : [guava, auto, dagger, caliper]
multimap was: {corelibs=[guava, auto, dagger, caliper]}
at com.google.common.truth.example.DemoTest.testTruth(DemoTest.java:71)
This is similar to AssertJ’s message:
java.lang.AssertionError:
Expecting:
<["guava", "auto", "dagger", "caliper"]>
to contain exactly in any order:
<["guava", "dagger", "truth", "auto", "caliper"]>
but could not find the following elements:
<["truth"]>
at com.google.common.truth.example.DemoTest.testTruth(DemoTest.java:71) <19 internal calls>
But note a few differences:
- The Truth message has fewer quotes and brackets, plus no
java.lang.AssertionError:orinternal calls. - The Truth message includes a
value ofline describing the value under test. - The Truth message includes the contents of the full multimap.
We can see other differences by making other assertions. For example, compare AssertJ…
org.junit.ComparisonFailure: expected:<... <version>0.4[5</version>
<scope>test</scope>
<exclusions>
<exclusion>
<!-- use the guava we're building. -->
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.google.truth.extensions</groupId>
<artifactId>truth-java8-extension</artifactId>
<version>0.45]</version>
<...> but was:<... <version>0.4[4</version>
<scope>test</scope>
<exclusions>
<exclusion>
<!-- use the guava we're building. -->
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.google.truth.extensions</groupId>
<artifactId>truth-java8-extension</artifactId>
<version>0.44]</version>
<...>
…to Truth…
diff:
@@ -7,7 +7,7 @@
<dependency>
<groupId>com.google.truth</groupId>
<artifactId>truth</artifactId>
- <version>0.45</version>
+ <version>0.44</version>
<scope>test</scope>
<exclusions>
<exclusion>
@@ -20,7 +20,7 @@
<dependency>
<groupId>com.google.truth.extensions</groupId>
<artifactId>truth-java8-extension</artifactId>
- <version>0.45</version>
+ <version>0.44</version>
<scope>test</scope>
<exclusions>
<exclusion>
Or compare AssertJ…
java.lang.AssertionError:
Expecting:
<[year: 2019
month: 7
day: 15
]>
to contain exactly in any order:
<[year: 2019
month: 6
day: 30
]>
elements not found:
<[year: 2019
month: 6
day: 30
]>
and elements not expected:
<[year: 2019
month: 7
day: 15
]>
…to Truth…
value of:
iterable.onlyElement()
expected:
year: 2019
month: 6
day: 30
but was:
year: 2019
month: 7
day: 15
Also note that the exception thrown by Truth is a ComparisonFailure
(useful for IDEs) in both of the last two cases, not just one
of the two as with AssertJ.
Platform support (Android, GWT)
Both libraries support Android. However, to use AssertJ on Android, you must fall back to AssertJ 2.x, and you can’t use “soft assertions.”
Truth supports Android in its main Truth class, and its equivalent to soft
assertions (Expect) works under Android. Truth also supports GWT.
Both libraries have third-party extensions for Android types: AssertJ has AssertJ-Android (which is deprecated), and Truth has AndroidX Test.
Puzzlers
In our years developing Truth, we have found that even the most “obvious” APIs can turn out to be misused, especially when they’re used by many tests across many projects by many developers. We’re fortunate to have enough users that we can justify digging deeply into the design of those APIs. (Here’s an example.) As we do so, we’re also fortunate to have the tools to search our codebase and run other teams’ tests.
Based on our experiences, we present some AssertJ puzzlers, which we have designed Truth to avoid:
assertThat(uniqueIdGenerator.next()).isNotSameAs(uniqueIdGenerator.next());
assertThat(primaryColors).containsAll(RED, YELLOW, BLUE);
assertThat(Longs.tryParse(s)).isEqualTo(parsedValues.get(s));
assertThat(event.getText())
.usingComparator(comparing(Object::toString))
.contains("2 suggestions");
assertThat(defaults).has(new Condition<>(x -> x instanceof String, "a string"));
assertThat(uniqueIdGenerator.next()).isNotSameAs(uniqueIdGenerator.next())
assertThat(primaryColors).containsAll(RED, YELLOW, BLUE)
assertThat(Longs.tryParse(s)).isEqualTo(parsedValues[s])
assertThat(event.text)
.usingComparator(compareBy(Any::toString))