← Back to Blog

October 26, 2020 · 9 views

Cucumber 6 Spring Integration

Testing Automation
Cucumber 6 Spring Integration

Cucumber is a very powerful testing framework, 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.

Cucumber 6 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 the version easier, let’s create a maven property for the cucumber version. At top of your pom file add new section properties:

<properties>
   <cucumber.version>6.8.0</cucumber.version>
   <spring.boot.version>2.3.4.RELEASE</spring.boot.version>
</properties>

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

<dependency>
 <groupId>io.cucumber</groupId>
 <artifactId>cucumber-java</artifactId>
 <version>${cucumber.version}</version>
</dependency>

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

<dependency>
 <groupId>io.cucumber</groupId>
 <artifactId>cucumber-junit</artifactId>
 <version>${cucumber.version}</version>
</dependency>

And finally, the Spring and Cucumber dependency:

<dependency>
    <groupId>io.cucumber</groupId>
    <artifactId>cucumber-spring</artifactId>
    <version>${cucumber.version}</version>
</dependency>

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

2. Cucumber Spring Configurations

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

@Configuration
@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;

@CucumberContextConfiguration
@ContextConfiguration(classes = {TestConfig.class})
@SpringBootTest
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

@RunWith(Cucumber.class)
@CucumberOptions(features = "src/test/resources")
public class RunTests {
}

3. Sample feature

The 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
    System.out.println(message);
}
}

Having configured Cucumber with Spring, it will be handy to use Spring-configured components in BDD testing. This is a simple guide for Cucumber 6 Spring Integration into your application.

Frequently Asked Questions

How does Cucumber integrate with Spring?

Cucumber integrates with Spring by allowing you to inject Spring beans into your step definitions, enabling you to test your Spring-managed components and services directly within your BDD scenarios.

📚 How to Cite This Article

APA Format:

I enjoy building things that live on the internet. (2020). Cucumber 6 Spring Integration. Steti.info. https://steti.info/blog/cucumber-6-spring-integration

MLA Format:

I enjoy building things that live on the internet. "Cucumber 6 Spring Integration." Steti.info, 26 Oct. 2020. https://steti.info/blog/cucumber-6-spring-integration.

Chicago Style:

I enjoy building things that live on the internet. "Cucumber 6 Spring Integration." Steti.info. October 26, 2020. https://steti.info/blog/cucumber-6-spring-integration.

Published: October 26, 2020
Last Updated: November 29, 2025

About the Author

Author
I like to build from websites to web apps, I create digital experiences that solve real problems and delight users and the most important is that all that I build, I build with PEOPLE!
Learn more about the author →

Related Posts