generator.server.springboot.cucumber.cucumber.md.mustache Maven / Gradle / Ivy
# Usage
In `src/test/features` create a feature file:
```
Feature: Simple WebService test
Background:
Given I am logged in as "user"
Scenario: Calling WebService with static assertion
When I get simple bean "Bob"
Then I get simple response with name "Bob" and age 42
```
You'll then have to define the glue code:
```java
import static {{packageName}}.cucumber.rest.CucumberRestAssertions.*;
import io.cucumber.java.en.Then;
import io.cucumber.java.en.When;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.web.client.TestRestTemplate;
import {{packageName}}.cucumber.CucumberRestTemplate;
public class SimpleSteps {
@Autowired
private CucumberRestTemplate rest;
@When("I get simple bean {string}")
public void callSimpleWebService(String bean) {
rest.getForEntity("/test-resources/" + bean, Void.class);
}
@Then("I get simple response with name {string} and age {int}")
public void shouldGetResponse(String name, int age) {
assertThatLastResponse()
.hasOkStatus()
.hasElement("$.name")
.withValue(name)
.and()
.hasElement("$.age")
.withValue(age);
}
}
```
Use a `TestRestTemplate` to make your rest calls, so you'll have the `context` management: the stuff allowing easier assertions in the `Then` steps.
The `assertThatLastResponse()` is a fluent API to assert your WebServices results.
As an example, you can use results arrays in your features:
```
Scenario: Calling WebService with line presentation table
Given I am logged in as "user"
When I get simple bean "Bill"
Then I should get simple bean
| Name | Bill |
| Age | 42 |
Scenario: Calling WebServices with all users
When I get all simple beans
Then I should get simple beans
| Name | Age |
| Bob | 42 |
| Bill | 50 |
```
And validate them easily with those assertions:
```java
@Then("I should get simple bean")
public void shouldGetResponseContent(Map response) {
assertThatLastResponse().hasResponse().containing(response);
}
@Then("I should get simple beans")
public void shouldGetResponseContent(List