Nearest — test automation library for API interaction

Oleksandr Podoliako
3 min readDec 16, 2022

--

I have thought how classic architecture approach for building test automation framework for API testing with java can be improved for last two months. The first iteration was described in the article. Nearest is result of second iteration, where I have decided to incapsulate logic with generics by moving it to dedicated library.

Nearest is applicable and efficient for two from three common tasks in automation testing, namely, to test business logic of application and to create pre- / post-conditions for UI tests. Nearest reduces amount of code, which needed to be written for interacting with REST services, by using generics. Nearest provides global logging configuration. Also, Nearest unifies manual and automation verifications by export/import functionality, which allows to use the same requests for both types of testing. Nearest supports import from Postman. Nearest does not limit Rest Assured functionality and provides easy access to it.

Nearest inherent HTTP two level structure. On first level Nearest has RequestWrapper and ResponseWrapper. On second level RequestWrapper has headers and body. ResponseWrapper has body and raw response. Also, Nearest has RestAssuredWrapper, which allow to do more specific actions with raw Rest Assured. Also, Nearest has IRestClient, which contains already implemented CRUD operations.

There are just 4 steps to set up Nearest

Add Nearest to the project

<dependency>
<groupId>io.github.oleksandrpodoliako</groupId>
<artifactId>nearest</artifactId>
<version>${LAST_VERSION}</version>
</dependency>

Create POJO model

@Getter
@Setter
@Builder
@AllArgsConstructor
@JsonIgnoreProperties(ignoreUnknown = true)
public class Post {
public int userId;
public int id;
public String title;
public String body;

public Post() {
}
}

Create Client class and add interface IRestClient. Specify request’s and response’s body types in interface

public class PostClient implements IRestClient<Post, Post> {
}

Now client class can be instantiated. Client object has all CRUD operations already implemented

public class Main {
private static final String URL = "https://jsonplaceholder.typicode.com/posts/";

public static void main(String[] args) {

RequestWrapper<Post> requestWrapper = RequestWrapper.<Post>builder()
.url(URL)
.apiMethod(APIMethod.GET_ARRAY)
.build();

ResponseWrapper<Post> responseWrapper = new PostClient().send(requestWrapper);

}
}

By default the global request and response logging levels are NONE. It can be changed easily

NearestConfig.setRequestLogging(RequestLoggingLevel.ALL);
NearestConfig.setResponseLogging(ResponseLoggingLevel.ALL);

Nearest allows to switch off mapping to object functionality

NearestConfig.setMappingStrategy(MappingStrategy.NOT_TO_MAP);

Nearest allows to use plain Rest Assured functionality

RestAssuredWrapper.given().when().then();

Nearest allows to convert all used requests to curl and store it to file. All you need to do is to set export strategy to ExportStrategy.TO_FILE(By default NOT_TO_EXPORT).

All requests will be converted to curl and stored to targets/export.txt. File can be changed with NearestConfig.setExportFileName() method

NearestConfig.setExportStrategy(ExportStrategy.TO_FILE);

Nearest allows to convert specific request to curl

Converter.toCurl(requestWrapper);

Nearest allows to convert curl to RequestWrapper

RequestWrapper<Post> requestWrapper = Converter.toRequestWrapper(curl, Post.class);

Export/import functionality has limitations. Not all curl syntax is supported. Example of supported curl syntax provided below

curl --request GET 'http://localhost:44444/posts/1' \
--header 'Content-type: application/json; charset=UTF-8' \
--data-raw {"userId":72810,"id":78256,"title":"Some Buried Caesar","body":"Chick Publications"}

Nearest allows to import requests from Postman export json. All you need is to export collection from Postman, instantiate PostmanIntegration class with path to exported Postman collection json and generate requestWrapper with getRequestWrapper() method, which has three parameters: request name, body class, folders names

PostmanIntegration postmanIntegration = new PostmanIntegration(exportedPostmanCollaction.json);
RequestWrapper<Post> requestWrapper = postmanIntegration.getRequestWrapper("request1", Post.class, "folder1level", "folder2level");

Nearest source can be got by link

--

--

Oleksandr Podoliako
Oleksandr Podoliako

Written by Oleksandr Podoliako

Motivated Test Automation Engineer with experience in testing web and mobile applications.

No responses yet