https://www.baeldung.com/google-truth
< dependency > < groupId >com.google.truth</ groupId > < artifactId >truth</ artifactId > < version >0.32</ version > </ dependency > < dependency > < groupId >com.google.truth.extensions</ groupId > < artifactId >truth-java8-extension</ artifactId > < version >0.32</ version > < scope >test</ scope > </ dependency >
|
assertThat(aUser).isNull();
assertThat(aUser).isNotIn(Arrays.asList(1, 3, "Three"));
assertThat(aFloat).isGreaterThan(1.0f);
assertThat(map).containsKey("one");
assertThat(aMap).containsEntry("one", 1L);
assertThat(aList).containsAnyIn(Arrays.asList(1, 5, 10));
assertThat(aList)
.containsExactlyElementsIn(anotherList)
.inOrder();
Truth assertions are made with chained method calls, so IDEs can suggest the assertions appropriate for a given object.
- The Truth code is faster to type, especially with autocompletion.
- The Truth code is easier to read:
- It has less boilerplate. For example, in the app we’re testing,
projectsByTeam()
returns aListMultimap
, soprojectsByTeam().get(...)
will be equal only to anotherList
whose elements are in the same order. We don’t want to test ordering here, so we must convert toMultiset
. - Putting the actual value first sets the context for the values to come: When the assertion begins “guava, dagger, …,” the reader isn’t sure what is being tested until later: “All projects? Recently released projects? Oh, core-libraries projects.”
- It has less boilerplate. For example, in the app we’re testing,
import static com.google.common.truth.Truth.assertWithMessage;
// for assertions on Java 8 types
import static com.google.common.truth.Truth8.assertThat;
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();
- IDE autocompletion can suggest assertions that fit the value under test, including rich operations like containsExactly(permission.SEND_SMS, permission.READ_SMS).
- Failure messages can include the value under test and the expected result. Contrast this with the assertTrue call above, which lacks a failure message entirely.
We've designed Truth to have a simpler API and more readable failure messages.
Also, if you're developing for Android, try AndroidX Test. It includes Truth extensions that make assertions even easier to write and failure messages even clearer:
Truth is an open source, fluent testing framework for Java designed to make your test assertions and failure messages more readable.The fluent API makes reading (and writing) test assertions much more natural, prose-like, and discoverable in your IDE via autocomplete. For example, compare how the following assertion reads with JUnit vs. Truth:
Another benefit of Truth over JUnit is the addition of useful default failure messages. For example:
AssertionError: <[red, green, blue, yellow]> should have contained <orange>
Truth already supports specialized assertions for most of the common JDK types(Objects, primitives, arrays, Strings, Classes, Comparables, Iterables, Collections, Lists, Sets, Maps, etc.), as well as some Guava types (Optionals). Additional support for other popular types is planned as well (Throwables, Iterators, Multimaps, UnsignedIntegers, UnsignedLongs, etc.).
assertEquals("March", monthMap.get(3)); // JUnit assertThat(monthMap).containsEntry(3, "March"); // TruthBoth statements are asserting the same thing, but the assertion written with Truth can be easily read from left to right, while the JUnit example requires "mental backtracking".
Another benefit of Truth over JUnit is the addition of useful default failure messages. For example:
ImmutableSet<String> colors = ImmutableSet.of("red", "green", "blue", "yellow"); assertTrue(colors.contains("orange")); // JUnit assertThat(colors).contains("orange"); // TruthIn this example, both assertions will fail, but JUnit will not provide a useful failure message. However, Truth will provide a clear and concise failure message:
AssertionError: <[red, green, blue, yellow]> should have contained <orange>
Truth already supports specialized assertions for most of the common JDK types(Objects, primitives, arrays, Strings, Classes, Comparables, Iterables, Collections, Lists, Sets, Maps, etc.), as well as some Guava types (Optionals). Additional support for other popular types is planned as well (Throwables, Iterators, Multimaps, UnsignedIntegers, UnsignedLongs, etc.).