Spring Boot -Behavioral Driven Development with Cucumber

Krushna Dash
3 min readJan 19, 2020

BDD helps us to create the test scripts from both the Business/Customer and developer point of view. In the beginning, all the stockholders like the developer, product owner, Scrum master, QA testers all come together and identify the business test scenario.

Cucumber is one of the tools written in ruby to do Behavioral-driven development. In Cucumber test scenario is written in a plain English language called Gherkin.

Advantages of Cucumber

  • Multi-language support like java, .net, ruby
  • It acts as a bridge between business and technical’
  • Test cases/scenario can be written without knowing any coding language which involves businesspeople closely on writing scenarios.
  • It can be used for end to end test framework and UAT

From my point of view, BDD is almost similar to TDD and it is on top TDD with an extra step that has the feature file where you write the different business scenarios in plain English, which is understandable by business people.

Against these feature files, you will have the step-definition classes where each scenario will present into multiple-step method to write the logic inside that. The step definition class will be similar to your traditional JUNIT test class at TDD development.

How to integrate cucumber into Spring boot?

To integrate cucumber into spring boot you need 3 maven dependency to be added to your pom.xml

  1. cucumber-java
  2. cucumber-junit
  3. cucumber-spring

You can find the latest version of these dependence from here https://mvnrepository.com/artifact/io.cucumber

Create a new spring boot project using spring initializer from https://start.spring.io/ and add you’re relevant dependency. In this example, I will add only the spring-boot-starter-web dependency.
Now add cucumber-java, cucumber-junit and cucumber-sprint dependency to pom.xml. Once you add the above dependency your pom file should look similar to below.

In the pom.xml file if you noticed we have commented the exclusion of junit-vintage-engine, as cucumber is not supported Junit 5 yet more here (https://github.com/cucumber/cucumber-jvm/issues/1149) and spring boot 2.2 comes with Junit 5
so we need the junit-vintage-engine to run the Junit 4 test cases.

Let’s add a controller class like below

Wrtie the feature file
Create a source folder like src/test/resources and add the feature file like below.

You can generate the step definition step method by running the feature file as a cucumber feature and copy the method signature from the console output. See the example class.

Then add the Cucumber runner class like below.

package com.test.springbdd;
import org.junit.runner.RunWith;
import io.cucumber.junit.Cucumber;
import io.cucumber.junit.CucumberOptions;
@RunWith(Cucumber.class)
@CucumberOptions(features = "src/test/resources/")
public class CucumberTest {
}

Note: When you add the second step definition file, do not initialize a new spring-context again, this will cause issues, cucumber can not use multiple spring context configuration. Refer to these issues
https://github.com/cucumber/cucumber-jvm/issues/1420
https://github.com/cucumber/cucumber-jvm/issues/1390

So your second step definition class should look like below.

The source code of this can be downloaded from Github https://github.com/kanhu/spring-bdd

--

--