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:
- 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.
- 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
- 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
- Api Package: Contains classes that do all the api work, setting up the interface and retrofit object.
- Model: Contains Java classes need for capturing the JSON output. I use and using jsonschema2pojo to help me generate my classes.
- 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.
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.
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.