Cucumber 6 Spring Integration

Cucumber is a very powerful testing framework written in the Ruby programming language, which follows the BDD (behavior-driven development) methodology. It enables developers to write high-level use cases in plain text that can be verified by non-technical stakeholders, and turn them into executable tests, written in a language called Gherkin.

And the Cucumber-Spring Integration is intended to make test automation easier. Once we have the Cucumber tests integrated with Spring, we should be able to execute them along with the Maven build.

1. Adding Maven Dependencies

In order to reduce the code, and manage version easier , let’s create a maven property for cucumber version. In top of your pom file add a new section properties:


Let’s get started using the Cucumber-Spring integration by defining the Maven dependencies – starting with the Cucumber-JVM dependency:


Next, we’ll add the JUnit and Cucumber testing dependency:


And finally, the Spring and Cucumber dependency:


Again, we can check out the most recent version of Cucumber Spring over here.

2. Configuration

Let’s configure cucumber 6 with spring, let’s start with configuration for Spring, will create a configuration class that will scan our folders to identify all services and components.

@ComponentScan(basePackages = {"com.example.spring"})
public class TestConfig {

Please change values from basePackages to your Packages.

Now we will create a Cucumber-Spring Integration class.


import io.cucumber.spring.CucumberContextConfiguration;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ContextConfiguration;

@ContextConfiguration(classes = {TestConfig.class})
public class SpringIntegrationCucumber {

We are using the latest Cucumber  and to make Cucumber aware of our test configuration we can annotate a configuration class on our glue path with @CucumberContextConfiguration and with one of the following annotations: @ContextConfiguration@ContextHierarchy or @BootstrapWith. Because we are using SpringBoot, you can annotate our configuration class with @SpringBootTest.


The last step is to create Runner

@CucumberOptions(features = "src/test/resources")
public class RunTests {



3. Sample feature

Last step is to create a sample feature file and to implement a huge logic under step implementation.

Under test resources we will create a sample feature with a sample step implementation

Scenario: Implemented step
  Given user prints something


And implementation for this sample test:

public class BasicSteps {

    @Given("^user prints something$")
    public void userPrint() {
    final String message = "Holla amigos";
    // SOME huge logic

Having configured Cucumber with Spring, it will be handy to use Spring-configured components in BDD testing. This is a simple guide for integrating the Cucumber test in a Spring-Boot application.