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:

 
<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>
    <scope>test</scope>
</dependency>

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

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

And finally, the Spring and Cucumber dependency:

<dependency>
    <groupId>io.cucumber</groupId>
    <artifactId>cucumber-spring</artifactId>
    <version>${cucumber.version}</version>
    <scope>test</scope>
</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.

@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

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 integrating the Cucumber test in a Spring-Boot application.