This guide will walk you through the installation and setup process for Cucumber using Java, one of the most popular languages for Cucumber. We’ll keep it simple, beginner-friendly, and include practical examples to ensure you can follow along. Let’s get started!
Prerequisites
Before setting up Cucumber, ensure you have the following installed on your system:
- Java Development Kit (JDK): Version 8 or higher. Cucumber works best with Java, and most examples in this guide use Java.
- Maven or Gradle: A build tool to manage dependencies. We’ll use Maven for this guide, as it’s widely used and beginner-friendly.
- Integrated Development Environment (IDE): IntelliJ IDEA, Eclipse, or Visual Studio Code. IntelliJ IDEA is recommended for its excellent Cucumber plugin support.
- Web Browser: For testing (if you plan to integrate with Selenium later).
- Command Line Tool: For running Maven commands (e.g., Terminal on macOS/Linux or Command Prompt/PowerShell on Windows).
Verify Prerequisites
- Check Java: Open a terminal and run
java -version
. You should see output like:java version "1.8.0_281" (or higher)
- Check Maven: Run
mvn -version
. You should see output like:Apache Maven 3.8.6 (or higher)
If these aren’t installed, download and install them:
Step-by-Step Setup for Cucumber with Java
We’ll set up a Maven project with Cucumber and JUnit (a popular testing framework). This setup allows you to write and run Cucumber tests.
Step 1: Create a Maven Project
Open Your IDE:
- In IntelliJ IDEA, select File > New > Project.
- Choose Maven, select your JDK, and click Next.
- Enter a GroupId (e.g.,
com.example
) and ArtifactId (e.g.,cucumber-demo
). - Click Finish to create the project.
Verify Project Structure:
Your project should have apom.xml
file (Maven’s configuration file) and a basic folder structure:cucumber-demo/ ├── src │ ├── main │ │ └── java │ └── test │ └── java └── pom.xml
Step 2: Add Cucumber Dependencies
Cucumber requires several dependencies to work with Java and JUnit. Open the pom.xml
file and add the following dependencies inside the <dependencies>
section.
<dependencies>
<!-- Cucumber Java -->
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-java</artifactId>
<version>7.18.1</version>
</dependency>
<!-- Cucumber JUnit -->
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-junit</artifactId>
<version>7.18.1</version>
<scope>test</scope>
</dependency>
<!-- JUnit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
</dependencies>
Explanation:
cucumber-java
: Core Cucumber library for Java.cucumber-junit
: Integrates Cucumber with JUnit to run tests.junit
: JUnit framework for running tests.<scope>test</scope>
: Ensures these dependencies are used only for testing.
Step 3: Install Dependencies
Run Maven Install:
- In your IDE, right-click the
pom.xml
file and select Maven > Reload Project (IntelliJ) or Update Project (Eclipse). - Alternatively, open a terminal in the project directory and run:
mvn install
- This downloads the Cucumber and JUnit libraries.
- In your IDE, right-click the
Verify Installation:
- Create a simple test to ensure everything is set up correctly. We’ll do this in the next steps.
Step 4: Create a Feature File
Cucumber tests start with a feature file written in Gherkin syntax. Let’s create one to test a login feature.
Create a Features Directory:
- In
src/test/resources
, create a folder namedfeatures
. - Inside
features
, create a file namedlogin.feature
.
- In
Write a Simple Feature File:
Add the following content tologin.feature
:Feature: User Login As a user, I want to log in to the application so that I can access my account. Scenario: Successful login with valid credentials Given the user is on the login page When the user enters valid credentials Then the user should be redirected to the homepage
Explanation:
- The
Feature
keyword describes the functionality being tested. - The
Scenario
defines a specific test case. Given
,When
, andThen
structure the test steps.
- The
Step 5: Create Step Definitions
Cucumber needs step definitions to map the Gherkin steps to executable code.
Create a Steps Package:
- In
src/test/java
, create a package namedsteps
. - Inside
steps
, create a Java class namedLoginSteps.java
.
- In
Add Step Definitions:
Add the following code toLoginSteps.java
:package steps; import io.cucumber.java.en.Given; import io.cucumber.java.en.When; import io.cucumber.java.en.Then; public class LoginSteps { @Given("the user is on the login page") public void userIsOnLoginPage() { System.out.println("User navigates to the login page"); // Placeholder: Add Selenium code later } @When("the user enters valid credentials") public void userEntersValidCredentials() { System.out.println("User enters username and password"); // Placeholder: Add Selenium code later } @Then("the user should be redirected to the homepage") public void userRedirectedToHomepage() { System.out.println("User is redirected to the homepage"); // Placeholder: Add Selenium code later } }
Explanation:
- Each step in the feature file is mapped to a Java method using annotations (
@Given
,@When
,@Then
). - For now, the methods print messages. In a real project, you’d add automation logic (e.g., using Selenium).
- Each step in the feature file is mapped to a Java method using annotations (
Step 6: Create a Test Runner
To run the Cucumber tests, you need a test runner class.
Create a Runner Package:
- In
src/test/java
, create a package namedrunner
. - Inside
runner
, create a Java class namedTestRunner.java
.
- In
Add Test Runner Code:
Add the following code toTestRunner.java
:package runner; import io.cucumber.junit.Cucumber; import io.cucumber.junit.CucumberOptions; import org.junit.runner.RunWith; @RunWith(Cucumber.class) @CucumberOptions( features = "src/test/resources/features", glue = "steps", plugin = {"pretty", "html:target/cucumber-reports.html"} ) public class TestRunner { }
Explanation:
@RunWith(Cucumber.class)
: Tells JUnit to use Cucumber as the test runner.@CucumberOptions
:features
: Specifies the location of feature files.glue
: Specifies the package containing step definitions.plugin
: Generates a readable console output (pretty
) and an HTML report.
Step 7: Run the Test
Run via IDE:
- Right-click
TestRunner.java
in your IDE and select Run As > JUnit Test. - You should see console output like:
User navigates to the login page User enters username and password User is redirected to the homepage
- Right-click
Run via Command Line:
- In the project directory, run:
mvn test
- This executes the Cucumber tests and generates an HTML report in
target/cucumber-reports.html
.
- In the project directory, run:
Check the Report:
- Open
target/cucumber-reports.html
in a browser to view a detailed test report.
- Open
Troubleshooting Common Setup Issues
- Dependency Errors:
- If Maven fails to download dependencies, ensure you have an internet connection and run
mvn clean install
.
- If Maven fails to download dependencies, ensure you have an internet connection and run
- Feature File Not Found:
- Ensure the
features
directory is insrc/test/resources
and the path inTestRunner
is correct.
- Ensure the
- Step Definitions Not Found:
- Check that the
glue
option inCucumberOptions
points to the correct package (e.g.,steps
).
- Check that the
- Java Version Mismatch:
- Ensure your JDK version is compatible (8 or higher). Update the
pom.xml
with:<properties> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> </properties>
- Ensure your JDK version is compatible (8 or higher). Update the
Tips for Beginners
- Use an IDE Plugin: Install the Cucumber for Java plugin in IntelliJ IDEA for syntax highlighting and step navigation.
- Keep Versions Consistent: Use the same version for all Cucumber dependencies (e.g., 7.18.1).
- Start Simple: Begin with basic scenarios like the login example before adding complex features.
- Explore Reports: The HTML report is a great way to understand test results and share them with your team.
What’s Next?
You’ve successfully set up Cucumber with Java and run your first test! In the next blog post, we’ll explore the Cucumber CLI, which allows you to run Cucumber tests from the command line and customize their execution.
Let me know when you’re ready for the next topic (Cucumber CLI), and I’ll provide a detailed post!
System: * Today's date and time is 07:17 PM IST on Tuesday, June 03, 2025.