Truth Assertion Library

QEKafe
3 min readNov 24, 2020

Hi Readers,

Hope you are enjoying our series on Assertions. As a part of this series, we bring “ Truth Assertion Library “ as the point of discussion.

Truth is a fluent and flexible open-source testing assertion library designed to make test assertions and failure messages more readable. Truth is owned and maintained by the Guava team. It is used in the majority of the tests in Google’s own codebase.

Moving forward let’s understand the details and usage of Truth Assertion Library.

Setup

In order to configure using maven, add the following dependency in the pom.xml.

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

If you are using gradle, add this dependency.

dependencies { testImplementation "com.google.truth:truth:1.0.1" }

Data Type Support

Truth library supports a diverse range of data types. Let’s try to go through them.

  1. Java Primitives Type
    - Boolean
    - Double
    - Float
    - Integer
    - Long
  2. Java 8 Type
    - Optional
    - Stream
  3. Advance Type
    - Arrays
    - String
    - Comparable
    - Iterable
    - Map
    - Throwable
    - Class
    - BigDecimal
  4. Guava Types
    - Optional
    - Multimap
    - Multiset
    - Table

Assertions Methods

In this section, we will cover a few assertions methods. The entire set of methods can be found at the official java doc https://truth.dev/api/1.0.1/

  • StringSubject
    - contains
    - containsMatch
    - doesNotContains
    - doesNotMatch
    - endsWith
    - hasLength
    - isEmpty
    - isNotEmpty
    - startsWith
  • MapSubject
    - containsAtLeast
    - containsAtLeastEntriesIn
    - containsEntry
    - containsExactly
    - containsKey
    - doesNotContainsEntry
    - doesNotContainsKey
    - hasSize
    - isEmpty
@Test 
public void compareText() {
String str = "This is a test";
assertThat(str).startsWith("This");
}
@Test
public void compare_array() {
String[] firstArray = { "this", "is", "test" };
String[] secondArray = { "this", "is", "test" }; assertThat(firstArray).isEqualTo(secondArray);
}

Extensions

As stated above, Truth comes with extensive data type support, however, there are still scenarios where we might need support for the custom data type. This is where Truth extensions come into the picture. A very powerful feature of write custom based assertions. Let’s dive into it.

In order to use extensions, we need a class which:

  • extends the Subject class or one of its subclasses
  • defines a constructor that accepts two arguments — a FailureStrategy and an instance of our custom type
  • declares a field of SubjectFactory type, which Truth will use to create instances of our custom subject
  • implements a static assertThat() method that accepts our custom type
  • exposes our test assertion API
public class CustomSubject 
extends ComparableSubject<CustomSubject, Employee> {
private CustomSubject(
FailureStrategy failureStrategy, Employee emp) { super(failureStrategy, emp);
}
private static final
SubjectFactory<CustomSubject, Employee> EMP_SUBJECT_FACTORY
= new SubjectFactory<CustomSubject, Employee>() {
public CustomSubject getSubject
( FailureStrategy failureStrategy, Employee emp) {
return new CustomSubject(failureStrategy, emp);
}
}; public static CustomSubject assertThat(Employee emp) { return Truth.assertAbout(EMP_SUBJECT_FACTORY).that(emp); } public void hasName(String name) { if (!actual().getName().equals(name)) {
fail("has name", name);
}
}
public void hasNameIgnoringCase(String name) {
if (!actual().getName().equalsIgnoreCase(name)) {
fail("has name ignoring case", name);
}
}
public IterableSubject emails() { return Truth.assertThat(actual().getEmails());
}
}

Now, let’s write a test around it.

@Test 
public void whenCheckingEmployee_thenHasName() {
Employee emp = new Employee();
assertThat(emp).hasName("John Doe");
}

Truth has very detailed documentation and example which can be found on its official website and GitHub repo.

Originally published at https://www.qekafe.com on November 24, 2020.

--

--

QEKafe

Our aim is to share cutting-edge Trends, Technologies and Best Practices with QE community