cucumber run with java and spring integration quick notes
Steve Fenton getting started with bdd intellij
- Maven dependencies
note: cucumber-java8 is not required it allows you to use lambda steps definition
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-junit</artifactId>
<version>${cucumber.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-java</artifactId>
<version>${cucumber.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-spring</artifactId>
<version>${cucumber.version}</version>
</dependency>
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-java8</artifactId>
<version>${cucumber.version}</version>
<scope>test</scope>
</dependency>
- Define a runner
The runner below run features in src/test/resources/features, with steps defs and hooks in com.your.company.steps.package, create an html report in target/cucumber directory and skips @wip tagged features/scenarios.
@RunWith(Cucumber.class)
@CucumberOptions(
features={"src/test/resources/features"},
glue = {"com.your.company.steps.package"},
plugin = {"pretty", "html:target/cucumber"},
// skip WIP features
tags = {"~@wip"}
)
public class RunAllFeatures {}
- Add maven dependency
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-spring</artifactId>
<version>${cucumber.version}</version>
</dependency>
- Declare bean context configuration on top of step class
@ContextConfiguration(classes = SpringConfiguration.class)
public class YourStepdefs {
...
}
- Using "World" bean/component
If you want to inject a common instance between your steps definitions class, you can declare a @component scenario scoped. ie a bean that will be instantiate at the beginning of a scenario and destroyed at the end of each scenario. You will need to use the cucumber-glue scope.
@Component
@Scope("cucumber-glue")
public class World {
...
}
In order to use natural language in you features like so :
Scenario: Scheduling a 90-day Review
Given an employee is hired "today"
When I schedule a review
Then the review should be scheduled "in 90 days"