This is the simplest possible setup for Cucumber-JVM using Java with Maven. There is nothing fancy like a webapp or browser testing. All this does is to show you how to set up and run Cucumber! If this is your first time using Cucumber have a look at the 10-minute tutorial first.
To write assertions the project comes with AssertJ included.
Git:
git clone https://github.com/cucumber/cucumber-jvm-starter-maven-java.git
cd cucumber-jvm-starter-maven-java
Or download a zip file.
Open a command window and run:
On macOS/Linux:
./mvnw testOn Windows PowerShell:
.\mvnw.cmd testThis runs Cucumber features using Cucumber's JUnit Platform Engine. The Suite
annotation on the RunCucumberTest class tells JUnit to kick off Cucumber.
[INFO] Running com.example.project.RunCucumberTest
Scenario: a few cukes # classpath:com/example/project/belly.feature:3
✔ Given I have 42 cukes in my belly # com.example.project.StepDefinitions.I_have_cukes_in_my_belly(int)
↷ When I wait 1 hour # com.example.project.StepDefinitions.i_wait_seconds(java.lang.Integer)
org.opentest4j.TestAbortedException: TODO: Implement me
at com.example.project.StepDefinitions.i_wait_seconds(StepDefinitions.java:19)
at ✽.I wait 1 hour(classpath:com/example/project/belly.feature:5)
↷ Then my belly should growl
The output show that there is a single feature file with one scenario. The scenario has three steps, one passing, one pending, and one undefined. See if you make can each step pass.
The Cucumber JUnit Platform Engine uses configuration parameters to know what features to run,
where the glue code lives, what plugins to use, etc. When using JUnit, these
configuration parameters are provided through the @ConfigurationParameter
annotation on your test.
For available parameters see: io.cucumber.junit.platform.engine.Constants
Specify a particular scenario by line
@SelectClasspathResource(value = "com/example/project/belly.feature", line = 3)
In case you have multiple feature files or scenarios to run against repeat the annotation.
You can also specify what to run by tag.
First add a tag to a scenario:
Feature: Belly
@Zucchini
Scenario: a few cukesThen add an annotation to RunCucumberTest.
@IncludeTags("Zucchini")Tags can be selected from the CLI using the groups and excludedGroups parameters. These take a
JUnit5 Tag Expression.
Note: When using JUnit, the @ is not part of the tag.
mvn verify -DexcludedGroups="Haricots" -Dgroups="Zucchini | Gherkin"
Maven does not (yet) support selecting single features or scenarios
with JUnit selectors. As a work around the cucumber.features property can be
used. Because this property will cause Cucumber to ignore any other selectors
from JUnit it is prudent to only execute the Cucumber engine.
To select the scenario on line 3 of the belly.feature file use:
./mvnw test -Dsurefire.includeJUnit5Engines=cucumber -Dcucumber.features=src/test/resources/com/example/project/belly.feature:3
Note: Add -Dcucumber.plugin=pretty to get a more detailed output during test
execution.
