cucumber rest steps

I like using Cucumber to do BDD Testing. Even when I have to test a REST API I use it, so I’ve created a library to do it: Cucumber REST Steps

Cucumber REST Steps in action

First of all, you need to add it to your pom.xml

<dependency>
  <groupId>es.rubenjgarcia</groupId>
  <artifactId>cucumber-rest-steps</artifactId>
  <version>1.0.0</version>
  <scope>test</scope>
</dependency>

Now, let’s create a Test Case class to add the steps

import cucumber.api.CucumberOptions;
import cucumber.api.junit.Cucumber;
import org.junit.runner.RunWith;

@RunWith(Cucumber.class)
@CucumberOptions(glue = {"es.rubenjgarcia.cucumber.rest.steps"}, features = "classpath:features")
public class CucumberTestCase {

}

You are ready to use the steps. Let’s see a couple of examples

Scenario: Empty response
  Given I call GET "http://localhost:8080"
  Then The response status should be 200
  And The response is empty

Easy, isn’t it?

Let’s see how you can verify the response

Scenario: With data
  Given I call POST "http://localhost:8080/withData" with data:
  """
    test: "test"
  """
  Then The response status should be 200
  And The response should be:
  """
    foo: "bar"
  """
  And The response should contain "foo"
  And The response should contain "foo" with value "bar"
  And The response should not contain "foo" with value "wee"
  And The response should not contain "bar"

If you want to verify complex objects, you can

Scenario: With array
  Given I call GET "http://localhost:8080/withArray"
  Then The response status should be 200
  And The response should contain array with size 3
  And The response should contain 3 entities
  And The response should contain at least 2 entity
  And The response should contain at most 4 entities
  And The response should contain more than 2 entities
  And The response should contain less than 4 entities
  And The response should be array:
  """
    - foo: "bar"
    - foo: 3
    - foos:
      - bar
      - wee
  """
  And Response entity "[0]" should contain "foo"
  And Response entity "[0]" should not contain "bar"
  And Response entity "[0]" should contain "foo" with value "bar"
  And Response entity "[0]" should not contain "foo" with value "wee"
  And Response entity "[2].foos" should contain array:
  """
    - bar
    - wee
  """
  And Response entity "[2].foos" should contain 2 entities
  And Response entity "[2].foos" should contain at least 1 entity
  And Response entity "[2].foos" should contain at most 3 entities
  And Response entity "[2].foos" should contain more than 1 entities
  And Response entity "[2].foos" should contain less than 3 entities

Find out more steps and examples in the official repository