Android Integration Testing

If it’s not tested, don’t trust it.


If you are building software, you have to test it. If it isn’t tested then I promise it will fail in some horrible way. I learnt this the hard way and trust me it wasn’t fun. Staying up late, debugging code only to find out your endpoint has a hyphen or you were incrementing a variable by itself and now one.

You end up wanting to shoot yourself.

After learning to appreciate tests, I end up catching some of these silly mistakes and saving time. You end up finding bugs before your customers do. Trust me, you don’t want bad reviews.


By creating and running  tests against your code, you can easily verify that the logic of individual units is correct. Running unit tests after every build helps you to quickly catch and fix software regressions introduced by code changes to your app.

There are two main types of tests:

  1. Local Unit Tests: These tests run on the local JVM and do not have access to functional Android framework APIs. They don’t rely on the Android framework.
  2. Instrumented Tests: These are all tests that must run on an Android hardware device or an Android emulator. They are used to mock the actual behavior of the app. Eg storing data in SQL or UI elements.

For more info check out Android’s training guide.

In this tutorial, we’ll focus on Integration Testing, testing our endpoints ensuring we get the right response.

Enough talk. Let’s get this party started.


  1. TMDB Api Key: You’ll need to get an API key to be able to test this out.  Get your API key here.

If you are new to Retrofit, you might want to read this. I’ve been using this library for all my network call.

Package Structure

Screen Shot 2016-08-30 at 2.27.23 PM

  1. Api Package: Contains classes that do all the api work, setting up the interface and retrofit object.
  2. Model: Contains Java classes need for capturing the JSON output. I use  and using jsonschema2pojo to help me generate my classes.
  3. Util: Contains class utilities. Right now it only contains a class with constants used across the app.

Creating the Retrofit instance

To send out network requests to an API, we need to use the Retrofit builder class and specify the base URL for the service.


Create an Interceptor

When making any API request to TMDB, we require the API key. To do so, we create a class that extends Interceptor and add that to OkHttpClient .addInterceptor(). This will ensure that all requests are added to the request.

Define the Endpoints

With Retrofit 2, endpoints are defined inside of an interface using special retrofit annotations to encode details about the parameters and request method.

  We are now ready to test the our enpoints. telegram-i-migliori-stickers-da-aggiungere-alla-vostra-collezione-3

Testing the Endpoints

Unit Tests are in src/test/java. We’ll create a class which will have all your test. 1. Create an instance of the request object Call topRatedList = getTmdbApiClient().movieInterface().getTopRatedMovies(); 2. User Response to execute the request

Response movieResponse = topRatedList.execute();

Run Your Test

If all goes well all your tests should pass.

All Test Results

Touch down

That’s it. I now trust my endpoints and and so can you.

In the next post, I will show you how to test UI functionality. We will build on the same app.

Go yee and test your endpoints!

The code is available on Github. Instructions on where to put the API key are in the README file.

Android Custom ListView

This tutorial covers ‘Advanced’ custom listview concepts. The code is available on Github.

Android Hive provides an awesome tutorial on how to create a custom list view but you can still take things a notch higher and create a ListView like what Whatsapp or Telegram use. Their Lists allow you to click on either the contact or the profile picture.

Final Result

By the end of it we will be able to click on an item on the list eg the image. Below is the final output.


Enough talk..let’s code.

Tools of Trade

  1. RecyclerView: Our list will extend RecyclerView instead of BaseAdapter. RecyclerView is more flexible that ListView and has a better perefomance.  For more info check out
  2. Viewholder Pattern: This pattern consists of a simple class that holds the references to the UI components for each row in the ListView. This pattern avoids looking up the UI components all the time the system shows a row in the list.

Let’s get our hands dirty 🙂

1. We need to add RecyclerView dependency in our gradle file.

2. Creating a RecyclerView is very similar to the ListView.

3. In onCreate we setup the recyclerview.

4. Now create under model package. This model class will be used to provide movie objects data to listview after parsing the json.

5. Create a class named with below code. This is a custom list adapter class which provides data to list view. In other words it renders the movie_item_layout.xml in list by pre-filling appropriate information. This adapter must extend a class called RecyclerView.Adapter passing our class that implements the ViewHolder pattern. We then to define our data model that stands at the base of our UI that implements the ViewHolder design pattern.

6. I created a method onMemberClick() that handles onClick behavior when the add/remove button is clicked.

7. In the,I created volley’s JsonArrayRequest to get the json from url. Upon parsing the json, I stored all the json data into an ArrayList as Movie objects.


That is all folks. If you have any questions, comment below. If you have a better way of doing it, please, help me out.