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.

Introduction

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.

Prerequisites

  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!
jesus_ok___telegram_sticker_by_jinkazama84-d8qqmqi

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

Leave a comment

Your email address will not be published. Required fields are marked *