Truth - Fluent assertions for Java

Truth makes your test assertions and failure messages more readable. Similar to AssertJ, it natively supports many JDK and Guava types, and it is extensible to others.

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

How to use Truth

1. Add the appropriate dependency to your build file:

Maven:

<dependency>
  <groupId>com.google.truth</groupId>
  <artifactId>truth</artifactId>
  <version>0.42</version>
  <scope>test</scope>
</dependency>

Gradle:

buildscript {
  repositories.mavenLocal()
}
dependencies {
  testCompile "com.google.truth:truth:0.42"
}

To use the Java 8 extensions, also include com.google.truth.extensions:truth-java8-extension:0.42.

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

import static com.google.common.truth.Truth.assertThat;
import static com.google.common.truth.Truth.assertWithMessage;

// for assertions on Java 8 types
import static com.google.common.truth.Truth8.assertThat;

3. Write a test assertion:

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

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

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