What is Google Truth?

  • Google Truth is a testing framework designed and developed by Google developers to make tests and their error messages more readable and understandable.
  • Google Truth is an open – source licensed software under Apache 2.0.
  • Google Truth is a assert Lib that makes testing test conditions easier.
  • Google Truth is not intended to remove JUnit assertions; instead it focuses on improving the readability of complex assertions and their failure messages.

How Google Truth helps in writing Test cases?

  • Google Truth Asserts are human readable.
  • Extensive API support to make test case looks clean.
  • Human understandable failure messages in case of test failure.

How to Setup Google Truth in Android Studio?

  • In order to use Google Truth you have to use gradle dependency.
1
2
3
    testCompile('com.google.truth:truth:0.27') {
        exclude group: 'junit' // Android has JUnit built in
    }

Now lets have a comparison between JUnit Assert and Truth Assert.
I have written few test cases to compare both.

The failure messages

Sometimes the raw error message is inappropriate in context, and difficult to understand.
Below is the snapshot of error message we get while using JUnit Assert and Truth Assert.

JUnitFailureMessage

TruthFailureMessage

Numeric and Comparable

Complicated comparison can be easily achieved via usage of Range.Lets have a look at the test case.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
    //JUnit Test Case
    @Test
    public void getSquareRoot_validInput_returnNumberWithinRange() {
        double minValue = 5.0;
        double maxValue = 10.0;
        double result = dataUtility.getSquareRoot(81);
        Assert.assertTrue("Result should be in range 5-10", result > minValue && result < maxValue);
    }
 
    //Truth Test Case
    @Test
    public void getSquareRoot_ValidInput_returnNumberWithinRange() {
        double result = dataUtility.getSquareRoot(81);
        Truth.assertThat(result).isIn(Range.open(5.0, 10.0));
    }

Collections

Here is an example of test case in order to find any duplicate value in the list.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
 //JUnit Test Case
    @Test
    public void getListOfNames_returnNoDuplicateNames() {
        List names = dataUtility.getListOfNames();
        Set uniqueValue = new HashSet<>();
        for (String result : names) {
            uniqueValue.add(result);
        }
        Assert.assertTrue("No Duplicate Names", uniqueValue.size() == names.size());
    }
 
    //Truth Test Case
    @Test
    public void getListOfNames_returnNoDuplicateNames1() {
        List names = dataUtility.getListOfNames();
        Truth.assertThat(names).containsNoDuplicates();
    }

One more Test case to test for the scenario if two lists have same element in order.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
    //JUnit Test Case
    @Test
    public void getListOfNames_wasCalled_returnListInOrder() {
        List names = dataUtility.getListOfNames();
        int flag = 0;
        for (int i = 0; i < names.size(); i++) {
            if (!(names.get(i).equals(expectedNames.get(i)))) {
                flag = 1;
                break;
            }
        }
        Assert.assertTrue("Result is Exactly In Order", flag == 0);
    }
 
    //Truth Test Case
    @Test
    public void getListOfNames_WasCalled_returnListInOrder() {
        List names = dataUtility.getListOfNames();
        Truth.assertThat(names).named("names").containsExactlyElementsIn(expectedNames).inOrder();
    }

Strings:

Simple example how string manipulation can be tested

1
2
3
4
5
6
    //Truth Test Case
    @Test
    public void getReverseName_validInputString_reverseString() {
        String result = dataUtility.getReverseName("xyz XYZ");
        Truth.assertThat(result).endsWith("zyx");
    }

Who is Using it?

  • Dagger (Square and Google)
  • Google Auto
  • Google Guava

References:

One comment on “Google Truth – Android

Leave a Reply

Your email address will not be published.