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:

  1. Java Development Kit (JDK): Version 8 or higher. Cucumber works best with Java, and most examples in this guide use Java.
  2. Maven or Gradle: A build tool to manage dependencies. We’ll use Maven for this guide, as it’s widely used and beginner-friendly.
  3. Integrated Development Environment (IDE): IntelliJ IDEA, Eclipse, or Visual Studio Code. IntelliJ IDEA is recommended for its excellent Cucumber plugin support.
  4. Web Browser: For testing (if you plan to integrate with Selenium later).
  5. 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

  1. 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.
  2. Verify Project Structure:
    Your project should have a pom.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

  1. 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.
  2. 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.

  1. Create a Features Directory:

    • In src/test/resources, create a folder named features.
    • Inside features, create a file named login.feature.
  2. Write a Simple Feature File:
    Add the following content to login.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, and Then structure the test steps.

Step 5: Create Step Definitions

Cucumber needs step definitions to map the Gherkin steps to executable code.

  1. Create a Steps Package:

    • In src/test/java, create a package named steps.
    • Inside steps, create a Java class named LoginSteps.java.
  2. Add Step Definitions:
    Add the following code to LoginSteps.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).

Step 6: Create a Test Runner

To run the Cucumber tests, you need a test runner class.

  1. Create a Runner Package:

    • In src/test/java, create a package named runner.
    • Inside runner, create a Java class named TestRunner.java.
  2. Add Test Runner Code:
    Add the following code to TestRunner.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

  1. 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
      
  2. 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.
  3. Check the Report:

    • Open target/cucumber-reports.html in a browser to view a detailed test report.

Troubleshooting Common Setup Issues

  • Dependency Errors:
    • If Maven fails to download dependencies, ensure you have an internet connection and run mvn clean install.
  • Feature File Not Found:
    • Ensure the features directory is in src/test/resources and the path in TestRunner is correct.
  • Step Definitions Not Found:
    • Check that the glue option in CucumberOptions points to the correct package (e.g., steps).
  • 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>
      

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.