Better Programming

Advice for programmers.

Follow publication

Member-only story

Android Unit Testing With MockWebServer and Truth Assertion Library

Farhan Tanvir
Better Programming
Published in
3 min readFeb 23, 2022

Testing is an important part of the software lifecycle. We developers tend to make mistakes. Testing ensures that our code behaves as expected. It also helps to find bugs early, maintain code standards, and save time.

First, we will know a little about MockWebServer and the truth Assertion Library.

MockWebServer

According to the official documentation:

This library makes it easy to test that your app Does The Right Thing when it makes HTTP and HTTPS calls. It lets you specify which responses to return and then verify that requests were made as expected.

If you want to more please check here.

Truth

According to the official documentation:

Truth is a library for performing assertions in tests. Truth is owned and maintained by the Guava team. It is used in the majority of the tests in Google’s own codebase.

If you want to know more please check here.

Show me what we are Testing.

It is a great app. Isn’t it?

We will use this GitHub repository in this article. Feel free to check it.

TL;DR

  1. Add dependency of MockWebServer, Truth, and OKHTTP in build.gradle
  2. If plus(+) sign exist in JUNIT dependency remove it and add a proper dependency
  3. CreateImageResponse.json file inside app/src/main/resources folder
  4. Create ImageApiServiceTest class under app/test
  5. Set up the MockWebServer and write test

Let’s Test

First add the dependency Add dependency of MockWebServer, Truth, and OKHTTP in build.gradle. Please add the latest Version.

Create an account to read the full story.

The author made this story available to Medium members only.
If you’re new to Medium, create a new account to read this story on us.

Or, continue in mobile web

Already have an account? Sign in

Farhan Tanvir
Farhan Tanvir

Written by Farhan Tanvir

Believes in learn by doing . Currently working as a software engineer. Love to share Knowledge. https://twitter.com/FarhanTanvirBD

No responses yet

Write a response