Authoring API test to Create a user resource
Sample REST APIs
Let us use Reqres as our APIs under test.
Create User API
To start with let us understand the Create User API.
curl -X POST \
https://reqres.in/api/users \
-H 'content-type: application/json' \
-d '{
"name": "morpheus",
"job": "leader"
}'
Request JSON:
{
"name": "morpheus",
"job": "leader"
}
Response JSON:
{
"name": "morpheus",
"job": "leader",
"id": "752",
"createdAt": "2021-09-01T14:57:39.722Z"
}
Create a postman request for the Create User API
Create a new request with the name “CreateUser” as shown below
Now let us get back to IntelliJ Idea and see how do we call this API
Specify baseUrl
Open src/test/resources/api/hosts.json
and specify baseUrl
as below.
{
"baseUrl": "https://reqres.in/"
}
Specify hosts in config
Open config/default.properties
file & add the api.hosts
property as below
# Api Config
api.hosts: hosts
Generate a model or POJO to represent Request and Response
As we understood from postman CreateUser API has a Request body and Response body. Let us generate model/POJO classes for both.
From postman, copy the request JSON of CreateUser API to the clipboard.
Right-click on the api/user/createUser
folder, choose New → Generate POJO from JSON
- Paste the JSON request from clipboard.
- Choose Lombok.
- Enter Root object name as CreateUserRequest.
- Click Generate button.
This would generate POJO class representing the JSON.
CreateUserRequest.java
package ekam.example.api.user.createUser;
import lombok.Getter;
@Getter
public class CreateUserRequest{
private String name;
private String job;
}
Note: Please replace the @Data
annotation of Lombok with @Getter
in all the generated classes.
In the same way, please generate CreateUserResponse POJO representing the response JSON.
CreateUserResponse.java
package ekam.example.api.user.createUser;
import lombok.Getter;
@Getter
public class CreateUserResponse{
private String createdAt;
private String name;
private String id;
private String job;
}
Generate Retrofit Service & Client
In the previous section, we have already generated the UserClient. Let us add support for Create User API
Generate CreateUser Service method
Copy the postman collection JSON to the clipboard
Place the cursor within the interface body. Press Control + Enter (or Code → Generate… menu option) to invoke the below popup.
Choose the “Ekam Retrofit2 Generator” option.
This will open up a dialog “Retrofit API signature generator“
Click OK button. This will generate the below code.
@Headers({"Content-Type: application/json"})
@POST("/api/users")
Call<CreateUserResponse> createUser(@Body CreateUserRequest request);
The complete file would look like below.
package ekam.example.api.user;
import com.google.inject.Inject;
import com.google.inject.name.Named;
import com.testvagrant.ekam.api.retrofit.RetrofitBaseClient;
import com.testvagrant.ekam.reports.annotations.APIStep;
import ekam.example.api.user.createUser.CreateUserRequest;
import ekam.example.api.user.createUser.CreateUserResponse;
import ekam.example.api.user.getSingleUser.GetSingleUserResponse;
import retrofit2.Call;
import retrofit2.http.*;
public class UserClient extends RetrofitBaseClient {
private interface UserService {
@GET("/api/users/{id}}")
Call<GetSingleUserResponse> getSingleUser(@Path("id") int userId);
@Headers({"Content-Type: application/json"})
@POST("/api/users")
Call<CreateUserResponse> createUser(@Body CreateUserRequest request);
}
private final UserService service;
@Inject
public UserClient(@Named("baseUrl") String baseUrl) {
super(baseUrl);
service = httpClient.getService(UserService.class);
}
@APIStep(keyword = "When", description = "I invoke getSingleUser API")
public GetSingleUserResponse getSingleUser(int userId) {
Call<GetSingleUserResponse> call = service.getSingleUser(userId);
return httpClient.execute(call);
}
}
## Generate a method to invoke CreateUser API Let us generate a method that would invoke CreateUser API. To do this, Ekam provides a live template. Press Control+Enter (Code → Generate…) and choose API Step
Complete the template to generate code as below.
@APIStep(keyword = "When", description = "I invoke createUser API")
public CreateUserResponse createUser(CreateUserRequest request) {
Call<CreateUserResponse> call = service.createUser(request);
return httpClient.execute(call);
}
The UserClient class would like as below:
package ekam.example.api.user;
import com.google.inject.Inject;
import com.google.inject.name.Named;
import com.testvagrant.ekam.api.retrofit.RetrofitBaseClient;
import com.testvagrant.ekam.reports.annotations.APIStep;
import ekam.example.api.user.createUser.CreateUserRequest;
import ekam.example.api.user.createUser.CreateUserResponse;
import ekam.example.api.user.getSingleUser.GetSingleUserResponse;
import retrofit2.Call;
import retrofit2.http.GET;
import retrofit2.http.Headers;
import retrofit2.http.POST;
import retrofit2.http.Path;
public class UserClient extends RetrofitBaseClient {
private interface UserService {
@GET("/api/users/{id}}")
Call<GetSingleUserResponse> getSingleUser(@Path("id") int userId);
@Headers({"Content-Type: application/json"})
@POST("/api/users")
Call<CreateUserResponse> createUser(@Body CreateUserRequest request);
}
private final UserService service;
@Inject
public UserClient(@Named("baseUrl") String baseUrl) {
super(baseUrl);
service = httpClient.getService(UserService.class);
}
@APIStep(keyword = "When", description = "I invoke getSingleUser API")
public GetSingleUserResponse getSingleUser(int userId) {
Call<GetSingleUserResponse> call = service.getSingleUser(userId);
return httpClient.execute(call);
}
@APIStep(keyword = "When", description = "I invoke createUser API")
public CreateUserResponse createUser(CreateUserRequest request) {
Call<CreateUserResponse> call = service.createUser(request);
return httpClient.execute(call);
}
}
Generate Test
Now that our Client class is ready, let us add a test calling this API
Open UserTests.java
. Let us call the createUser
method in the UserClient
.
Here is the test.
@Test(groups = "SmokeTest")
public void shouldCreateUser() {
CreateUserRequest request = CreateUserRequest.builder().name("Bob").job("Builder").build();
CreateUserResponse response = Client(UserClient.class)
.createUser(request);
assertEquals(response.getName(), "Bob");
}
Execute Test
We need to make an IntelliJ IDEA setting change to execute tests via IDE
Preferences → Build, Execution, Deployment → Build Tools → Gradle
Run tests using: IntelliJ IDEA
Execute the test !!
Congratulations!! on your test