3

I am new to cucumber. I've configured the environment for cucumber with required jars. I want to test rest api with cucumber. And so first created .feature file and generated basic step definitions.

.feature file:

Feature: Test

  Scenario: List accounts
    Given the system knows about the following details:
      | name | value |
      | unit | 01    |
      | dept | 001   |
    When the client requests accounts
    Then the response code should be 200
    And the response should contain following details:
      | name    | value   |
      | unit    | 01      |
      | dept    | 001     |
      | acctype | current |

And TestClass.java as below:

package testPackage;

import org.junit.runner.RunWith;

import cucumber.api.CucumberOptions;
import cucumber.api.DataTable;
import cucumber.api.PendingException;
import cucumber.api.java.en.*;
import cucumber.api.junit.Cucumber;

@RunWith(Cucumber.class)
@CucumberOptions(
        features={"classpath:cucumber.features/"},
        glue = {"testPackage"}      
        )

public class TestClass {

    @Given("^the system knows about the following details:$")
    public void the_system_knows_about_the_following_details(DataTable arg1) throws Throwable {
        // Write code here that turns the phrase above into concrete actions
        // For automatic transformation, change DataTable to one of
        // List<YourType>, List<List<E>>, List<Map<K,V>> or Map<K,V>.
        // E,K,V must be a scalar (String, Integer, Date, enum etc)
        throw new PendingException();
    }

    @When("^the client requests accounts$")
    public void the_client_requests_accounts() throws Throwable {
        // Write code here that turns the phrase above into concrete actions
        throw new PendingException();
    }

    @Then("^the response code should be (\\d+)$")
    public void the_response_code_should_be(int arg1) throws Throwable {
        // Write code here that turns the phrase above into concrete actions
        throw new PendingException();
    }

    @Then("^the response should contain following details:$")
    public void the_response_should_contain_following_details(DataTable arg1) throws Throwable {
        // Write code here that turns the phrase above into concrete actions
        // For automatic transformation, change DataTable to one of
        // List<YourType>, List<List<E>>, List<Map<K,V>> or Map<K,V>.
        // E,K,V must be a scalar (String, Integer, Date, enum etc)
        throw new PendingException();
    }

}

I searched a lot but couldn't able to find to send GET request from .java file.

How to send GET request from cucumber step definitions and compare json response ?

pnuts
  • 54,806
  • 9
  • 74
  • 122
Valay
  • 1,923
  • 2
  • 32
  • 80
  • 4
    The steps generated are just methods, so you can do anything in them that you could in any other java method. I think [this link](http://stackoverflow.com/questions/2793150/using-java-net-urlconnection-to-fire-and-handle-http-requests) will help you make HTTP Requests. – Tyler MacMillan Aug 28 '15 at 20:43

1 Answers1

0

If you're willing to use Spring, you can use Spring's REST Template to send HTTP requests in a RESTful manner.

Jackson is also pretty good to parse the responses/requests to/from predefined java data objects, which makes dealing with JSON significantly less painful.

burythehammer
  • 345
  • 1
  • 6