Truth - Fluent assertions for Java and Android

What is Truth?

Truth is a library for performing assertions in tests:


Truth is owned and maintained by the Guava team. It is used in the majority of the tests in Google’s own codebase.

Why use an assertion library?

In other words, why depend on a new library when you can use the methods built into your testing framework, like assertEquals?

    ImmutableMultiset.of("guava", "dagger", "truth", "auto", "caliper"),

Here’s the equivalent using Truth:

    .containsExactly("guava", "dagger", "truth", "auto", "caliper");

Consider also the failure message:

java.lang.AssertionError: expected:<[guava, dagger, truth, auto, caliper]> but was:<[dagger, auto, caliper, guava]>
  at org.junit.Assert.failNotEquals( <2 internal calls>
  at <19 internal calls>

This is fine for a simple assertion, but consider:

Here’s the message generated by Truth:

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]}

Truth vs. AssertJ

Truth and AssertJ are very similar. This raises the question: Why did we create Truth? The reason is historical: AssertJ didn’t exist when we started Truth. By the time it was created, we’d begun using Truth widely at Google, and we’d made some decisions that would be difficult to retrofit onto AssertJ.

Both Truth and AssertJ have their advantages. We prefer Truth for its simpler API:

Also, Truth works on Android devices by default, without requiring users to use an older version or import a different class than usual.

Truth vs. Hamcrest

Truth and Hamcrest differ significantly. We prefer Truth because:

How to use Truth

1. Add the appropriate dependency to your build file:




buildscript {
dependencies {
  testCompile ""

To use the Java 8 extensions, also include

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 symbol” errors. The easiest fix is usually to add a direct dependency on the newest JRE version of Guava.

2. Add static imports for Truth’s entry points:

import static;
import static;

// for assertions on Java 8 types
import static;

3. Write a test assertion:

String string = "awesome";
assertWithMessage("Without me, it's just aweso").that(string).contains("me");

Iterable<Color> googleColors = googleLogo.getColors();
    .containsExactly(BLUE, RED, YELLOW, BLUE, GREEN, RED)

If you’re using an IDE with autocompletion, it will suggest a list of assertions you can make about the given type. If not, consult the API docs. For example, if you’re looking for assertions about a Map, look at the documentation for MapSubject.

More information