Photo by Fred Kearney on Unsplash

Set Up and Run Cucumber Tests In Spring Boot Application

Arun B Chandrasekaran

--

This article is to explain setting up and running Cucumber feature files in Spring Boot application.

It also explains using Cucumber TypeRegistryConfigurer class to convert Cucumber DataTable to Java objects and using Cucumber Report plugin to generate well formatted HTML reports.

Step 1: Create a Spring Boot Project using Spring Initializer.

Using Spring Boot initializer, create a Spring Boot Project with following dependencies.

  1. spring-boot-starter-web:jar:2.1.5.RELEASE:compile
  2. spring-boot-starter-data-jpa:jar:2.1.5.RELEASE:compile
  3. h2:jar:1.4.199:runtime
  4. spring-boot-starter-test:jar:2.1.5.RELEASE:test
  5. lombok:jar:1.18.8:compile (optional)

Project Structure

Note: Create directories & packages highlighted in yellow color in above image.

Step 2: Add following dependencies to Maven pom.xml

spring-boot-starter-test: This dependency imports Spring Boot test modules as well as JUnit 4, AssertJ, Hamcrest, Mockito, JSONAsset, JsonPath.

Note: Spring Boot starter test module contains @SpringBootTest annotation. We will use this annotation in below code.

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>

cucumber-java8: This dependency helps to create Cucumber Step Definitions as Lambdas (pure functions).

<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-java8</artifactId>
<version>4.2.0</version>
<scope>test</scope>
</dependency>

cucumber-spring: To Execute Cucumber scripts in Spring Context.

<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-spring</artifactId>
<version>4.2.0</version>
<scope>test</scope>
</dependency>

cucumber-junit: To run Cucumber test scenarios as JUnit tests

<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-junit</artifactId>
<version>4.2.0</version>
<scope>test</scope>
</dependency>

cucumber-reporting (Optional): To generate a HTML Report from cucumber-report.json.

I strongly suggest to use this reporting plugin to your project and expose the HTML report as static content along with the application package. Cucumber feature files are living documentation of software and this report helps to achieve that.

<dependency>
<groupId>net.masterthought</groupId>
<artifactId>cucumber-reporting</artifactId>
<version>4.2.3</version>
</dependency>

Note: If you refer to source code, you may find other utilities/libraries added as dependencies, these just for the sample source code to work, you may use them or use alternative libraries.

Source: pom.xml

For additional details about Cucumber Reporting refer project page at github.

Step 3: Class to make Cucumber to use Spring Context for test scenario execution

Source: CucumberSpringContextConfiguration.java

Step 4: To configure and run Cucumber

Source: CucumberTest.java

‘@CucumberOptions’ annotation is used to configure the directory that contains the feature files and output plugins.

A JSON report with the name “json:target/cucumber-report.json” will be generated by Cucumber. It is the input to Cucumber Report generator.

Cucumber automatically reads all directories under the package where it is configured. Make sure all Cucumber related directories are under directory ‘bdd’ so that it is automatically glued to Cucumber execution context. If you look at the source code, you can find all Cucumber related classes and step definitions are under com.arun.hello.bdd package in this example.

Step 5: Class to convert Cucumber all DataTable to Corresponding Java Object (POJO)

Source: CucumberTypeRegistryConfigurer.java

This class assumes that you will use field names in Java POJO as Cucumber DataTable column headers.

Step 6 (Optional): Cucumber Report Runner Configuration

Source: CucumberReportRunner.java

1. If you decide to use Cucumber Report plugin configured above, replace ‘@RunWith(Cucumber.class)’ with ‘@RunWith(CucumberReportRunner.class)’ in CucumberTest.java file.

2. CucumberReportRunner.java extends Cucumber.java

3. CucumberReportRunner generates reports from the file at target/cucumber-report.json

4. CucumberReportRunner copies the generated html reports to target/classes/static directory so that the reports are packaged along with the application.

Complete Source Code:

Refer github for source code.

--

--

Arun B Chandrasekaran

Principal Software Engineer at World Fuel Services. My job is my hobby.