Cucumber step libraries
Chenile’s house rule is that every service is proved with Cucumber scenarios that make the whole call through the
real pipeline (why, how it runs). You rarely write step
definitions for that: Chenile ships a vocabulary of steps in a few small libraries, and a test picks the ones it needs
through the runner’s glue. This page lists every step, exactly as the code defines it.
| Library | Maven artifact | Glue path | What it drives |
|---|---|---|---|
| REST steps | org.chenile:cucumber-utils |
classpath:org/chenile/cucumber/rest |
HTTP requests through Spring MockMvc, in process |
| Service steps | org.chenile:cucumber-utils |
classpath:org/chenile/cucumber/nonrest |
a Chenile service operation, called through ChenileEntryPoint without HTTP |
| Workflow steps | org.chenile:cucumber-workflow-utils |
classpath:org/chenile/cucumber/workflow |
changing a workflow’s enablement configuration between scenarios |
| Security steps | org.chenile:cucumber-sec-utils (chenile-security) |
classpath:org/chenile/cucumber/security/rest |
fetching a Keycloak token and adding it as Authorization |
| Integration REST steps | org.chenile.bdd:it-cucumber-utils |
classpath:org/chenile/cucumber/rest |
the same request text over REST Assured, against a running server |
| Integration security steps | org.chenile.bdd:it-cucumber-sec-utils |
classpath:org/chenile/cucumber/security/rest |
Keycloak tokens for integration tests |
The REST and integration REST libraries use the same package, so a runner uses one or the other, never both.
Wiring a test
JGen’s service, workflow, query and batch blueprints generate this already. The runner lists your own bdd package
and the Chenile libraries:
@RunWith(Cucumber.class)
@CucumberOptions(features = "src/test/resources/features",
glue = {"classpath:com/mycompany/app/order/bdd",
"classpath:org/chenile/cucumber/security/rest", // only when security is on
"classpath:org/chenile/cucumber/workflow", // only for enablement scenarios
"classpath:org/chenile/cucumber/rest"})
public class CukesRestTest {}
Your own CukesSteps class carries the Spring test configuration (@CucumberContextConfiguration,
@SpringBootTest(webEnvironment = RANDOM_PORT, classes = SpringTestConfig.class), @AutoConfigureMockMvc,
@ActiveProfiles("unittest")) and a dummy @Given("dummy") step, because Cucumber wants at least one step in it.
Write a custom step there only when nothing below fits.
Variables
Steps share values through a variable map (VariableHelper). Put a value in with a step, read it with ${name}:
Given that "tenant" equals "tenant0"
When I construct a REST request with header "x-chenile-tenant-id" and value "${tenant}"
And I POST a REST request to URL "/order" with payload
"""
{ "item": "book" }
"""
Then success is true
And store "$.payload.id" from response to "id"
${…}is replaced in URLs, payloads, header names and values, and the expected values of the assertion steps (Apache CommonsStringSubstitutor).- The map is not cleared between scenarios, which is how an id stored in one scenario is used by the next. Feature files are therefore written as a sequence: create, then read, then change.
- Everything else a step remembers (headers, the last response) lives in
CukesContextand is cleared before every scenario. Headers must be set again in each scenario. CukesContextkeeps one map per thread. Before 2.1.37 only the first thread got a map, so scenarios had to run on one thread. The variable map is shared by all threads, so scenarios that store and reuse variables still belong on one thread.
REST steps (cucumber-utils)
org.chenile.cucumber.rest.RestCukesSteps. Every request is sent with JSON content type and accept headers, through
MockMvc into the real Spring MVC and Chenile pipeline; no port is opened.
Building and sending a request
| Step | Notes |
|---|---|
Given that "<name>" equals "<value>" |
Sets a variable. |
When I construct a REST request with header "<name>" and value "<value>" |
Adds a header to the next request in this scenario. Repeat for more headers. |
When I POST a REST request to URL "<url>" with payload |
The doc string that follows is the body. |
When I PUT a REST request to URL "<url>" with payload |
|
When I PATCH a REST request to URL "<url>" with payload |
Workflow events: PATCH /{svc}/{id}/{event}. |
When I DELETE a REST request to URL "<url>" with payload |
|
When I GET a REST request to URL "<url>" |
No body. |
Status and envelope
| Step | Checks |
|---|---|
Then the http status code is <int> |
The HTTP status. |
Then success is true / Then success is false |
$.success in the GenericResponse. |
Then the top level code is <int> |
$.code. |
Then the top level subErrorCode is <int> / … is "<string>" |
$.subErrorCode. |
Then the top level description is "<text>" |
$.description (already localized). |
Then the error array size is <int> |
Number of entries in $.errors. |
Payload keys
Keys are JSON paths relative to $.payload, so mutatedEntity.currentState.stateId means
$.payload.mutatedEntity.currentState.stateId.
| Step | Checks |
|---|---|
Then the REST response contains key "<key>" |
The key exists. |
Then the REST response does not contain key "<key>" |
The key is absent. |
Then the REST response key "<key>" is "<value>" |
Equality. |
And the REST response key "<key>" contains string "<text>" |
Substring. |
Then the REST response is null |
$.payload does not exist. |
Then the REST response is empty |
$.payload is empty. |
Then the REST expression "<path>" size is <int> |
Size of $.<path> (relative to the root, not the payload). |
Then the value of "<json path>" is "<value>" |
Equality at any absolute JSON path, such as $.errors[0].field. |
Collections
Available since 2.1.31
| Step | Checks |
|---|---|
And the rest response key "<key>" collection has an item "<value>" |
A list of scalars contains the value. |
And the rest response key "<key>" collection has an item with values: |
A list of scalars contains every value in the one-column table. |
And the rest response key "<key>" collection has an item with keys and values: |
Some object in the list has every key/value row of the table. |
And the REST response key "<key>" collection has an item with key "<k>" and value "<v>" |
Older form for one pair. |
And the rest response key "mutatedEntity.activities" collection has an item with keys and values:
| key | value |
| activityName | ${event} |
| activityComment | ${comment} |
There is also an older the REST response key … collection has an item with keys and values: step, with REST in
capitals, that reads only the first two rows. Prefer the lower-case rest form, which takes any number of rows and
does not depend on order.
Errors and warnings
Errors and warnings both arrive in the errors array of the envelope; a warning leaves success true.
| Step | Checks |
|---|---|
Then a REST exception is thrown with status <int> and message code <int> |
HTTP status and the error’s sub-error code. |
Then a REST exception is thrown with message code <int> |
The same with status 400. |
Then a REST exception is thrown with message "<text>" |
$.description. |
Then a REST exception is thrown with param number <n> value "<text>" |
Parameter n (from 1) of the first error. |
Then a REST warning must be thrown with code <int> |
Some message has this sub-error code. |
Then a REST warning must be thrown that says "<text>" with code <int> |
Text and code together. |
Then a REST warning must be thrown that says "<text>" with code <int> and http status <int> |
Text, code and the message’s status. |
Then a REST warning must be thrown with param number <n> value "<text>" |
A message parameter. |
Then a REST warning must be thrown that has field "<field>" |
The field a validation message points at. |
Storing values from a response
| Step | Effect |
|---|---|
Then store "<json path>" from response to "<name>" |
Reads an absolute JSON path (for example $.payload.mutatedEntity.id) into a variable. |
Then fix and store JSON string "<json path>" as "<name>" from response |
Stores a value so it can be pasted into a later JSON payload: valid JSON is kept, any other text is escaped and quoted. |
Service steps without HTTP (cucumber-utils)
org.chenile.cucumber.nonrest.CukesSteps calls an operation the way an event or a proxy would: it builds a
ChenileExchange for a service and operation and runs it through ChenileEntryPoint, so every interceptor still runs.
Use it for services that have no HTTP binding.
| Step | Notes |
|---|---|
When I POST a request to service "<service id>" and operation "<operation>" with payload |
The doc string is the body, transformed to the operation’s input type. No headers are sent. |
Then the response contains key "<key>" |
A SpEL expression on the response’s payload (data.<key>). |
Then the response does not contain key "<key>" |
|
Then the response key "<key>" is "<value>" |
|
Then the response is null |
The payload is null. |
Then an exception is thrown with message code <int> |
The exception is an ErrorNumException with this sub-error code. |
Then an exception is thrown with param number <n> value "<text>" |
|
Then a warning must be thrown that says "<text>" |
|
Then a warning must be thrown with code <int> |
|
Then a warning must be thrown with param number <n> value "<text>" |
These steps do not substitute ${variables}.
Workflow steps (cucumber-workflow-utils)
org.chenile.cucumber.workflow.CucumberWorkflowSteps changes a workflow at run time through its enablement
configuration, so one test can prove the behaviour of states, events and activities that a tenant switches on or off
(enablement). Point it at the workflow’s ConfigProviderImpl bean first; JGen’s
wfservice blueprint names it <service>ConfigProvider, with the flow’s prefix as the prefix.
| Step | Property written |
|---|---|
Given that config strategy is "<bean>" |
Selects the config provider bean. |
Given that config strategy is "<bean>" with prefix "<prefix>" |
The same, with every property prefixed by <prefix>.. |
Given that all enablements are cleared |
Clears every property written so far. |
Given that a new state "<state>" is added to flow "<flow>" |
<flow>.state.add.<state>= |
Given that a new transition "<event>" is added from state "<from>" to state "<to>" in flow "<flow>" |
<flow>.<from>.transition.add.<event>=<to> |
Given that state "<state>" in flow "<flow>" is enabled / … is disabled |
<flow>.<state>.enabled=true or false |
Given that transition "<event>" in state "<state>" in flow "<flow>" is enabled |
<flow>.<state>.<event>.enabled=true |
Given that transition "<event>" for state "<state>" in flow "<flow>" is disabled |
<flow>.<state>.<event>.enabled=false (note “for”, not “in”) |
Given that state "<state>" in flow "<flow>" has metadata with key "<k>" and value "<v>" |
<flow>.<state>.meta.<k>=<v> |
Given that transition "<event>" from state "<state>" in flow "<flow>" has metadata with key "<k>" and value "<v>" |
<flow>.<state>.<event>.meta.<k>=<v> |
Given that a new mandatory activity "<a>" is added from state "<from>" to state "<to>" in flow "<flow>" |
adds the transition and meta.activity=MANDATORY |
Given that a new optional activity "<a>" is added from state "<from>" to state "<to>" in flow "<flow>" |
meta.activity=OPTIONAL |
Given that a new activity completion checker "<a>" is added from state "<from>" to state "<to>" in flow "<flow>" |
meta.activity=COMPLETION_CHECKER |
Scenario: A tenant adds a mandatory activity to CLOSED
Given that config strategy is "issueConfigProvider" with prefix "Issue"
And that a new mandatory activity "verifyLogs" is added from state "CLOSED" to state "AreClosedActivitiesComplete" in flow "IssueFlow"
When I PATCH a REST request to URL "/issue/${id}/verifyLogs" with payload
"""
{ "comment": "All logs have been verified." }
"""
Then the REST response key "mutatedEntity.currentState.stateId" is "CLOSED"
Property values accept ${variables}.
Security steps
With security on, a scenario needs a real token. Both security libraries add one step:
When I construct a REST request with authorization header in realm "tenant0" for user "t0-premium" and password "t0-premium"
It fetches a token with the password grant from <keycloak>/realms/<realm>/protocol/openid-connect/token and adds
Authorization: Bearer <token> to the next request.
- Unit tests (
cucumber-sec-utils): the test class extendsBaseSecurityTest, which starts a Keycloak Testcontainer with realmstenant0andtenant1and ready-made users, such ast0-normalandt0-premium. The step reads the container’s address. - Integration tests (
it-cucumber-sec-utils): the Keycloak address comes fromchenile.security.keycloak.host.
Messaging tests get brokers the same way: cucumber-mqtt-utils has KafkaBaseTest and MqttBaseTest, which start
Kafka and MQTT Testcontainers.
Integration tests against a running server (it-cucumber-utils)
chenile-bdd’s it-cucumber-utils defines the REST request steps with the same text, but sends them with REST
Assured to chenile.bdd.target.host and chenile.bdd.target.port. JGen’s it blueprint generates a project for it.
The generated feature creates an entity, stores its id and reads it back.
It defines the same steps as the REST library above, with the same text and the same checks, so a feature file written for a unit test runs unchanged against a deployed server.
Requires 2.1.37 · unreleased
Before 2.1.37 the integration library implemented only part of the vocabulary. Headers were sent before
${variables} were substituted, the store step had two spaces (from response), the warning steps that check a
code could not be used, and about ten assertions failed with a ClassCastException. 2.1.37 rewrites the steps on
REST Assured responses, adds the collection steps, the value of …, the REST expression … size is …,
the REST response is empty and fix and store JSON string …, and keeps the two-space store step as an alias.
Related
- Concept: BDD testing and How it works: testing
- JGen blueprints: the generated runners and feature files
- Workflow internals: enablement and activities