artofcode.info

The beauty of coding!

Cucumber 6 Spring Integration

Author Avatar steti 26 Oct 2020
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

It tells Cucumber which class contains the Spring context configuration, enabling dependency injection in your step definitions.

How to Cite This Article

APA Format: steti. (2020). Cucumber 6 Spring Integration. Retrieved from https://artofcode.info/posts/cucumber-6-spring-integration
MLA Format: steti. "Cucumber 6 Spring Integration." Steti.info, 26 Oct 2020. https://artofcode.info/posts/cucumber-6-spring-integration
Chicago Format: steti. "Cucumber 6 Spring Integration." Steti.info. October 26, 2020. https://artofcode.info/posts/cucumber-6-spring-integration
Back to Posts