More readable test assertions
Truth’s fluent API allows users to write more readable test assertions. For example:
Optional<String> middleName = user.getMiddleName();
assertThat(middleName).isAbsent();
With JUnit, you’d have to negate the assertion:
Optional<String> middleName = user.getMiddleName();
assertFalse(middleName.isPresent());
More readable failure messages
Truth provides readable messages by default when your assertion fails. For example:
assertThat(googleColors).contains(PINK);
If this assertion fails, you’ll get the following message:
expected to contain: PINK
but was : [BLUE, RED, YELLOW, BLUE, GREEN, RED]
With JUnit, most people would write:
assertTrue(googleColors.contains(PINK));
However, if that assertion fails, JUnit will throw an AssertionFailedError
without any failure message! If you want a useful failure message with JUnit,
you’re forced to duplicate the data under test in a custom failure message:
assertTrue("expected to contain PINK but was " + googleColors, googleColors.contains(PINK));
Readable complex assertions
Truth allows you to express complex assertions in a readable way. For example, with Truth you could write:
assertThat(googleColors).containsNoneOf(PINK, BLACK, WHITE, ORANGE);
With JUnit, you’d have to write several assertions:
assertFalse(googleColors.contains(PINK));
assertFalse(googleColors.contains(BLACK));
assertFalse(googleColors.contains(WHITE));
assertFalse(googleColors.contains(ORANGE));