This document discusses using Cucumber and Citrus together for behavior driven integration testing. It provides examples of defining features and scenarios in Gherkin, implementing step definitions in Java, and using Citrus to test REST, messaging and other integrations. Citrus endpoints can be injected and configured, and Cucumber step definitions can use the Citrus DSL to define HTTP requests, JMS messages and more.
10. #DevoxxUS
Feature specification
Feature: Create voting
As a user I want to create new votings. Each voting is given default vote
options.
Scenario: Default voting options
Given voting title "Do you like Mondays?"
When I create new voting
Then voting should have 2 options
And voting should have option "yes"
And voting should have option „no"
And voting title should be "Do you like Mondays?"
@citrus_test@cucumberbdd
11. #DevoxxUS
Feature specification continued
Feature: Create voting
As a user I want to create new votings. Each voting is given default vote
options. The user should be able to set custom vote options.
Scenario: Default voting options
Given voting title "Do you like Mondays?"
When I create new voting
Then voting should have 2 options
And voting should have option "yes"
And voting should have option „no"
And voting title should be "Do you like Mondays?"
Scenario: Custom voting options
When I create new voting "What is your favorite color?"
And voting options are "green:red:blue"
Then voting title should be "What is your favorite color?"
And voting should have options
| green |
| red |
| blue |
@citrus_test@cucumberbdd
13. #DevoxxUS
JUnit Cucumber test
@RunWith(Cucumber.class)
public class VotingFeatureTest {
}
@citrus_test
voting-close.feature
voting-create.feature
voting-results.feature
public class VotingSteps {
@Given("^New default voting$")
public void defaultVoting() { ... }
@When("^I create new voting "(.+)"$")
public void createVotingWithTitle(String title) { ... }
@Then("^voting should have (d+) options$")
public void votingShouldHaveOptions(int optionCount) { ... }
}
@cucumberbdd
14. #DevoxxUS
Step definitions
@When("^I create new voting "(.+)"$")
public void createVotingWithTitle(String title) {
votingId = UUID.randomUUID();
Voting voting = new Voting(votingId, title);
votingService.add(voting);
}
@citrus_test
@Then("^voting should have (d+) options$")
public void votingShouldHaveOptions(int optionCount) {
Assert.assertEquals(optionCount,
votingService.get(votingId).getOptions().size());
}
When I create new voting "What is your favorite color?"
Then voting should have 3 options
@cucumberbdd
15. #DevoxxUS
Data tables
@Then("^(?:the )?voting should have options$")
public void votingShouldHaveOptions(DataTable dataTable) {
List<String> options = dataTable.asList(String.class);
votingShouldHaveOptions(options.size());
for(String option : options) {
votingShouldHaveOption(option);
}
}
@citrus_test
Then voting should have options
| green |
| red |
| blue |
@cucumberbdd
18. #DevoxxUS
Background
Feature: Show voting results
As a user I want to vote for an option. All voting results are stored
and the user should be able to get top vote option for each voting.
Background:
Given I create new voting "Do you like cucumbers?"
And voting options are "yes:no"
Scenario: Initial vote results
Then votes of option "yes" should be 0
And votes of option "no" should be 0
Scenario: Get vote results
When I vote for "yes"
Then votes of option "yes" should be 1
And votes of option "no" should be 0
And top vote should be "yes"
@citrus_test@cucumberbdd
19. #DevoxxUS
Scenario Outline
Feature: Show voting results
As a user I want to vote for an option. All voting results are stored
and the user should be able to get top vote option for each voting.
Scenario Outline: Get vote results
Given I create new voting "<title>"
And voting options are "yes:no"
When I vote for "yes" <yes_votes> times
And I vote for "no" <no_votes> times
Then votes of option "yes" should be <yes_votes>
And votes of option "no" should be <no_votes>
And top vote should be "<top_vote>"
Examples:
| title | yes_votes | no_votes | top_vote |
| Do you like hotdogs? | 12 | 5 | yes |
| Do you like crap sandwiches? | 1 | 25 | no |
@citrus_test@cucumberbdd
25. #DevoxxUS
Messaging Feature
Feature: Voting Http REST API
Background:
Given Voting list is empty
And New voting "Do you like donuts?"
And voting options are "yes:no"
Scenario: Top vote
When client creates the voting
And client votes for "no"
Then votes should be
| yes | 0 |
| no | 1 |
And top vote should be "no"
Scenario: Close voting
Given reporting is enabled
When client creates the voting
And client votes for "yes" 3 times
And client votes for "no" 2 times
And client closes the voting
Then participants should receive reporting mail
"""
Dear participants,
the voting '${title}' came to an end.
The top answer is 'yes'!
Have a nice day!
Your Voting-App Team
"""
@citrus_test@cucumberbdd
26. #DevoxxUS
Endpoint injection
public class VotingRestSteps {
@CitrusEndpoint
private HttpClient votingClient;
@CitrusEndpoint
private MailServer mailServer;
@CitrusResource
private TestRunner runner;
@Given("^Voting list is empty$")
public void clear() {
runner.http(action -> action.client(votingClient)
.send()
.delete("/voting"));
runner.http(action -> action.client(votingClient)
.receive()
.response(HttpStatus.OK));
}
}
@citrus_test@cucumberbdd
27. #DevoxxUS
Endpoint configuration
@Configuration
public class CitrusEndpointConfig {
@Bean
public HttpClient votingClient() {
return CitrusEndpoints.http()
.client()
.requestUrl("http://localhost:8080/rest/services")
.build();
}
@Bean
public MailServer mailServer() {
return CitrusEndpoints.mail()
.server()
.port(2222)
.autoStart(true)
.autoAccept(true)
.build();
}
}
@citrus_test@cucumberbdd
28. #DevoxxUS
Citrus endpoints
Component Description
citrus-http Http REST client and server
citrus-jms JMS queue or topic destination
citrus-ws SOAP client and server
citrus-mail SMTP mail client and server
citrus-docker Docker container management
citrus-camel Apache Camel endpoint
citrus-selenium Selenium browser endpoint
citrus-vertx Vert.x endpoint
citrus-kubernetes Kubernetes client
…
@cucumberbdd @citrus_test