You are currently viewing Cucumber with Java

Cucumber with Java

Cucumber is a popular Behavior Driven Development (BDD) framework that allows you to write test cases in simple English text. It helps bridge the gap between business stakeholders and developers by enabling tests to be written in a readable and understandable format. In this tutorial, we will explore how to set up and use Cucumber with Java for writing BDD tests.

Prerequisites

  • Java installed on your machine
  • An IDE such as IntelliJ IDEA or Eclipse
  • Basic understanding of Java and Maven

Step 1: Setting up the Project

1. Create a Maven Project

You can create a new Maven project in your preferred IDE or use Maven command line tools.

2. Add Dependencies

Open your pom.xml file and add the following dependencies:

<dependencies>
    <dependency>
        <groupId>io.cucumber</groupId>
        <artifactId>cucumber-java</artifactId>
        <version>7.2.3</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>io.cucumber</groupId>
        <artifactId>cucumber-junit</artifactId>
        <version>7.2.3</version>
        <scope>test</scope>
    </dependency>
</dependencies>

3. Create Package Structure

Create a package structure for your Cucumber tests. For example:

src
  └── test
      └── java
          └── cucumber
              └── steps

Step 2: Writing Feature Files

1. Create a Feature File

Create a new file with a .feature extension, for example, calculator.feature. This is where you will write your BDD scenarios.

Feature: Calculator
  Scenario: Add two numbers
    Given I have a calculator
    When I add 5 and 7
    Then the result should be 12

Step 3: Implementing Step Definitions

1. Create Step Definitions

Under the steps package, create a Java class, for example, CalculatorSteps.java. This class will contain the step definitions for your scenarios.

package cucumber.steps;

import io.cucumber.java.en.Given;
import io.cucumber.java.en.Then;
import io.cucumber.java.en.When;
import org.junit.Assert;

public class CalculatorSteps {
    private int result;

    @Given("I have a calculator")
    public void i_have_a_calculator() {
        // Setup code if needed
    }

    @When("I add {int} and {int}")
    public void i_add_two_numbers(int num1, int num2) {
        result = num1 + num2;
    }

    @Then("the result should be {int}")
    public void the_result_should_be(int expectedResult) {
        Assert.assertEquals(expectedResult, result);
    }
}

Step 4: Running Cucumber Tests

1. Create Test Runner

Create a Java class to act as a test runner, for example, TestRunner.java.

package cucumber;

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 = {"cucumber.steps"}
)
public class TestRunner {
}

Step 5: Running Tests

1. Run Tests

You can run your Cucumber tests either from your IDE by running TestRunner.java as a JUnit test, or from the command line using Maven:

mvn test

Complete Example

Here is how your project structure might look:

project
├── src
│   └── test
│       ├── java
│       │   └── cucumber
│       │       ├── steps
│       │       │   └── CalculatorSteps.java
│       │       └── TestRunner.java
│       └── resources
│           └── features
│               └── calculator.feature
└── pom.xml

Conclusion

You have now set up a basic Cucumber project with Java. This allows you to write BDD-style feature files and implement step definitions in Java. Cucumber helps in creating living documentation for your project, making it easier for stakeholders to understand the behavior of the system. You can extend this example by adding more scenarios, using data tables, hooks, and other advanced features offered by Cucumber.

Cucumber Documentation

Leave a Reply