Sitemap

Testing API using Retrofit2

4 min readAug 7, 2022

--

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 user

Setup

Create a maven project. Now Install the required dependencies for the project. Refer to the below screenshot for the dependencies.

Press enter or click to view image in full size
pom.xml

Code

  1. First we will be creating a RetrofitBuilder class for creating the Retrofit instance .The Retrofit instance 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’s ResponseBody type and it can only accept its RequestBody type 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 under Retrofit Configuration .
Press enter or click to view image in full size
RetrofitBuilder.java

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.

Press enter or click to view image in full size

2. If your service needs to be authenticated, add headers to your requests using an authenticator or interceptor.

Press enter or click to view image in full size
OkhttpClientBuilder.java

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.

Press enter or click to view image in full size
HeadersBuilder.java

3. There is another class RetrofitClientFactory.java which will be responsible to execute our request and return the response.

Press enter or click to view image in full size
RetrofitClientFactory.java

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.

Press enter or click to view image in full size

Let’s have a look at our UserAPIClient class. We will have 2 APIs to test as mentioned earlier in this post.

Press enter or click to view image in full size
UserAPIClient.java

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.

Press enter or click to view image in full size
User.class

Let’s have other classes for deserializing the different responses.

Press enter or click to view image in full size
SingleUserResponse.java
Press enter or click to view image in full size
Support.java
Press enter or click to view image in full size
UserCreated.class
Press enter or click to view image in full size
Data.java

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.

Press enter or click to view image in full size
UserAPITests.java

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 😃 .

Note: We can also use interceptor to add the additional headers. Add below method in OkhttpClientBuilder.class

Press enter or click to view image in full size
OkhttpClientBuilder.java

Github repo for the code

https://github.com/jeeshan12/RetrofitAPIAutomation.git

--

--