Testing API using Retrofit2
In this article we will see how we can test the APIs using Retrofit. Retrofit is a type-safe HTTP client for Android , Java and Kotlin developed by Square Open Source.
We will be using https://reqres.in/ to test the API using Retrofit. and TestNG as our testing framework.
Scenarios
We will be using 2 endpoints for our testing (see below)
1. /api/users/{id} where id is the user id for which we need to retrieve the details.
2. /api/users for creating a new userSetup
Create a maven project. Now Install the required dependencies for the project. Refer to the below screenshot for the dependencies.
Code
- First we will be creating a RetrofitBuilder class for creating the Retrofit instance .The
Retrofitinstance is the factory that creates REST clients from interfaces. You need to configure it using a builder pattern which takes at the very least the base URL of your service. We use Google GSON as a JSON converter. By default, Retrofit can only deserialize HTTP bodies into OkHttp’sResponseBodytype and it can only accept itsRequestBodytype for @Body.Converters can be added to support other types. There are other converter available as well. Please click here to know more. You can find different converters underRetrofit Configuration.
ConverterFactoryFunction class is responsible to provide you the converter depending on your choice. This class has a method getConverterAdapterFactory(final String converter) which takes “gson” or “jackson” as input and returns the appropriate converter for you. It is easy to add new converter, all you need to do is add a entry to MAP and define a Function for the same. Code for the same is attached below.
2. If your service needs to be authenticated, add headers to your requests using an authenticator or interceptor.
Along with this we will be creating a HeadersBuilder to create our headers and pass it to OkhttpClientBuilder.addHeaders(final Headers headers) method. Code for the same is attached.
3. There is another class RetrofitClientFactory.java which will be responsible to execute our request and return the response.
4. Let’s now create our API client to test our service. This class will have all the endpoints that we need to test. With Retrofit 2, endpoints are defined inside of an interface using retrofit annotations to encode details about the parameters and request method. The return value is always a parameterized as Call<T> . Different annotations for Retrofit is defined below.
Let’s have a look at our UserAPIClient class. We will have 2 APIs to test as mentioned earlier in this post.
5. Now we have different Models for Serializing and Deserializing the request and response
Get Mohd Jeeshan’s stories in your inbox
Join Medium for free to get updates from this writer.
First we have a User class, which will serve as Body to our POST request.
Let’s have other classes for deserializing the different responses.
Tests
We have all the available configuration and classes needed to test our APIs. Let’s create the tests by utilizing all the classes we have created so far.
In BeforeClass setup we are setting our headers and initialising the API which we need to test. We are using Faker to generate the test data. We have 2 different test methods which will fetch the user info(GET) and create a new User(POST).
That’s all for this article 😃 .









