This guide walks you through the process of setting up Cucumber for a Spring Boot project, writing feature files, and executing tests.
<!-- Cucumber Dependencies -->
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-java</artifactId>
<version>7.14.0</version>
</dependency>
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-spring</artifactId>
<version>7.14.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-junit</artifactId>
<version>7.14.0</version>
<!-- <scope>test</scope> -->
</dependency>
<!-- Spring Boot Starter Web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- Spring Boot Starter Test -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- JUnit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13</version>
<!-- <scope>test</scope> -->
</dependency>
<!-- Cucumber Reporting Plugin -->
<dependency>
<groupId>net.masterthought</groupId>
<artifactId>cucumber-reporting</artifactId>
<version>5.7.0</version>
<scope>test</scope>
</dependency>
- Update the maven-surefire-plugin in the build section of your pom.xml:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M5</version>
<configuration>
<testFailureIgnore>true</testFailureIgnore>
</configuration>
</plugin>
- Add the maven-cucumber-reporting plugin to the build section:
<plugin>
<groupId>net.masterthought</groupId>
<artifactId>maven-cucumber-reporting</artifactId>
<version>5.7.0</version>
<executions>
<execution>
<id>execution</id>
<phase>verify</phase>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<projectName>YourProjectName</projectName>
<outputDirectory>${project.build.directory}/cucumber-report-html</outputDirectory>
<cucumberOutput>${project.build.directory}/cucumber.json</cucumberOutput>
</configuration>
</execution>
</executions>
</plugin>
-
Create your feature files in the src/test/resources directory. Example:
-
src/test/resources/features/sample.feature
Feature: Sample Feature
Scenario: Sample Scenario
Given I have a sample step
When I perform some action
Then I should see the expected result
Create step definition classes in the src/test/java directory to implement the steps from your feature files.
Run the Cucumber tests using your IDE or execute the following Maven command:
mvn clean
mvn test
mvn verify
- After running the tests, view the generated Cucumber reports in the target/cucumber-report-html directory.
