artofcode.info

The beauty of coding!

API Functional Tests with Cucumber and Rest-assured

Author Avatar steti 24 Aug 2018
API Functional Tests with Cucumber and Rest-assured

API Functional Tests with Cucumber and Rest-assured


On projects where multiple systems undergo development at the same time, it’s crucial to maintain a clear picture of how they should interact. We commonly have a back-end system providing a REST API to multiple front-ends (browsers, mobile apps, chatbots, IoT, etc.). Because it will likely change over time, keeping the API picture clear and up to date can be a significant challenge. How can we efficiently describe the currently expected behavior and know if it’s working as expected?

In this case to provide a readable test scenarios it’s a good opportunity to use Cucumber.Cucumber helps us write readable requirements upfront that can be tied directly to executable tests. Here’s an example for a guestbook REST API:

 

I hope you have the basic knowledge about cucumber, if not you can found useful information about cucumber and rest-assured

In this post will present some generics post what will help you to test REST API, let’s start with :

Given user sets content type with value 'application/json'

this step sets content type for the request what you will perform, implementation:

@Given("^user sets content type with value '(.*)'$")
public void userSetsContentTypeWithValue(String contentType) {
RequestSpecification request = (RequestSpecification) scenarioContext.getData(RestKeys.REQUEST);
request.header("Content-Type", contentType);
}

 

Found a way to send JSON format with cucumber, this is one of more powerful step in automation process:

 Then the following json response is sent
"""
 {
    'dddd': 'dddd',
    'ggggg': 'ggggg',
    'somethingelse': {
      'thing': 'thingvalue',
      'thing2': 'thing2value'
    }
  }
"""

@Then("^the following json response is sent$")
public void theFollowingJsonResponseIsSent(String message) {
    request.body(message);
    request.post(kafkaTopics.concat(topicName));
}

Next is to verify response reason code:

Then the status code is 200

and implementation:

@Then("the status code is (\\d+)")
public void verifyStatusCode(int statusCode) {
    json = response.then().statusCode(statusCode);
}

And steps to check the response what we reserved:

And response includes the following
  | totalItems | 1             |
  | kind       | books#volumes |
And response includes the following in any order
  | items.volumeInfo.title     | Steve Jobs         |
  | items.volumeInfo.publisher | Simon and Schuster |
  | items.volumeInfo.pageCount | 630                |



Implementation for this steps is bellow:
/**
 * asserts on json fields with single values
 */
@When("response includes the following$")
public void response_equals(Map<String, String> responseFields) {
    for (Map.Entry<String, String> field : responseFields.entrySet()) {
        if (StringUtils.isNumeric(field.getValue())) {
            json.body(field.getKey(), equalTo(Integer.parseInt(field.getValue())));
        } else {
            json.body(field.getKey(), equalTo(field.getValue()));
        }
    }
}

@Then("response includes the following in any order")
public void response_contains_in_any_order(Map<String, String> responseFields) {
    for (Map.Entry<String, String> field : responseFields.entrySet()) {
        if (StringUtils.isNumeric(field.getValue())) {
            json.body(field.getKey(), containsInAnyOrder(Integer.parseInt(field.getValue())));
        } else {
            json.body(field.getKey(), containsInAnyOrder(field.getValue()));
        }
    }
}

Hope this will help you, how this helped me. Enjoy!

Frequently Asked Questions

Combining them allows you to write readable BDD scenarios (Cucumber) while leveraging the powerful HTTP testing capabilities of Rest-Assured.

How to Cite This Article

APA Format: steti. (2018). API Functional Tests with Cucumber and Rest-assured. Retrieved from https://artofcode.info/posts/api-functional-tests-with-cucumber-and-rest-assured
MLA Format: steti. "API Functional Tests with Cucumber and Rest-assured." Steti.info, 24 Aug 2018. https://artofcode.info/posts/api-functional-tests-with-cucumber-and-rest-assured
Chicago Format: steti. "API Functional Tests with Cucumber and Rest-assured." Steti.info. August 24, 2018. https://artofcode.info/posts/api-functional-tests-with-cucumber-and-rest-assured
Back to Posts