Skip to content

Make Selenium Easy

And Keep It That Way

  • Home
  • Share
  • Toggle search form

Cucumber Tutorial 1 – Setup a Basic Maven-Cucumber 5 -Junit Project in Eclipse

Posted on 02/19/2025 By admin

*****Updated with latest version of Cucumber 5 *********

Cucumber 6 is on the way but many are still using Cucumber 1 ( info.cukes version). So in this post, I am going to explain An end to end basic Cucumber 5 maven project setup with Junit in Eclipse.

Eclipse is an integrated development environment which can be used to develop Java programs. I am not going to explain installation of Eclipse in this post. It is very straight forward and little google can help you much better. If you have any difficulty in installation , feel free to contact me though mail.

We need two plugins for Eclipse in order to work with Cucumber projects:-

  1. Natural
  2. Cucumber Eclipse Plugin

Natural plugin is helpful in editing and maintaining BDD/ATDD files. The currently supported languages are Cucumber (Gherkin syntax) and JBehave.

Cucumber Eclipse Plugin is helpful as content assistance for feature file , syntax highlighting, pretty formatting and step definition wizard.

To install above plugins, go to Eclipse Marketplace (Help -> Eclipse Marketplace) . Now search both plugins one by one and click “Install” button. Plugin installation requires restart of Eclipse. I will suggest you to install both plugins one by one and restart eclipse.

You just need two dependencies for Cucumber-Junit project”:-

  1. Cucumber-Java
  2. Cucumber-Junit

Note:- Keep version of Cucumber-Java and Cucumber-Junit same.

I believe creating a maven project in Eclipse will not be tough job for you. If you face any problem, refer this post.

Add above dependencies ( For this post, I am using Cucumber 4) in your pom.xml. Your pom.xml should look like as below:-

 4.0.0 MavenCucumber5JunitSampleProject MavenCucumber5JunitSampleProject 0.0.1-SNAPSHOT  UTF-8 1.8 1.8    io.cucumber cucumber-java 5.6.0   io.cucumber cucumber-junit 5.6.0 test   

You could see an extra tag in above example. That is to set Java version as 1.8 for this project. By default you see 1.5 Java version when you create a new maven project.

Now create a simple feature file “src/test/resources“. A feature file has a file extension as “.feature“. This feature file contains acceptance scenarios in Gherkin language.

I have created a very simple feature file as below:-

Feature: This is a demo feature one

Scenario: This is first scenario
    Given Precondition is given
    When Something is done
    Then Something is expected
    
    
    Scenario: This is second scenario
    Given Another precondition is given
    When Something is done again
    Then Something is expected again

Right click on above feature file and Select “Run As – > Cucumber Feature”. It will generate step definitions of above steps. You will get output in console as below:-

Jan 13, 2020 1:07:59 PM cucumber.api.cli.Main run
WARNING: You are using deprecated Main class. Please use io.cucumber.core.cli.Main
Feature: This is a demo feature one

  Scenario: This is first scenario # /C:/Users/amomahaj/eclipse-workspace/MavenCucumber4JunitSampleProject/src/test/resources/featureFiles/DemoFeature.feature:3
    Given Precondition is given    # null
    When Something is done         # null
    Then Something is expected     # null

  Scenario: This is second scenario     # /C:/Users/amomahaj/eclipse-workspace/MavenCucumber4JunitSampleProject/src/test/resources/featureFiles/DemoFeature.feature:9
    Given Another precondition is given # null
    When Something is done again        # null
    Then Something is expected again    # null

Undefined scenarios:
/C:/Users/amomahaj/eclipse-workspace/MavenCucumber4JunitSampleProject/src/test/resources/featureFiles/DemoFeature.feature:3 # This is first scenario
/C:/Users/amomahaj/eclipse-workspace/MavenCucumber4JunitSampleProject/src/test/resources/featureFiles/DemoFeature.feature:9 # This is second scenario

2 Scenarios (2 undefined)
6 Steps (6 undefined)
0m0.387s


You can implement missing steps with the snippets below:

@Given("Precondition is given")
public void precondition_is_given() {
    // Write code here that turns the phrase above into concrete actions
    throw new cucumber.api.PendingException();
}

@When("Something is done")
public void something_is_done() {
    // Write code here that turns the phrase above into concrete actions
    throw new cucumber.api.PendingException();
}

@Then("Something is expected")
public void something_is_expected() {
    // Write code here that turns the phrase above into concrete actions
    throw new cucumber.api.PendingException();
}

@Given("Another precondition is given")
public void another_precondition_is_given() {
    // Write code here that turns the phrase above into concrete actions
    throw new cucumber.api.PendingException();
}

@When("Something is done again")
public void something_is_done_again() {
    // Write code here that turns the phrase above into concrete actions
    throw new cucumber.api.PendingException();
}

@Then("Something is expected again")
public void something_is_expected_again() {
    // Write code here that turns the phrase above into concrete actions
    throw new cucumber.api.PendingException();
}


You see a line in above output as “You can implement missing steps with the snippets below:“. Copy everything after that line and paste in to Java class. Create a package say “stepDefintions” under “src/test/java” and create a Java class say “DemoFeatureStepDef.java“. Paste generated step definition in this class.

Since we need to provide our own definition to step, I added a normal print statement in each step definition methods as below:-

package stepDefinitions;

import io.cucumber.java.en.Given;
import io.cucumber.java.en.Then;
import io.cucumber.java.en.When;

public class DemoFeatureStepDef {

        
        @Given("Precondition is given")
        public void precondition_is_given() {
           System.out.println("Precondition is given");
        }

        @When("Something is done")
        public void something_is_done() {
            System.out.println("Something is done");
        }

        @Then("Something is expected")
        public void something_is_expected() {
            System.out.println("Something is expected");
        }

        @Given("Another precondition is given")
        public void another_precondition_is_given() {
           System.out.println("Another precondition is given");
        }

        @When("Something is done again")
        public void something_is_done_again() {
            System.out.println("Something is done again");
        }

        @Then("Something is expected again")
        public void something_is_expected_again() {
            System.out.println("Something is expected again");
        }


}

Now you are all set to run your test. Remember you need to run feature file not step definition file. You don’t need runner file as well. In fact, you don’t even require Junit as of now. Just right click on feature file and select “Run As – > Cucumber Feature“. You will see output as below:-

Jan 13, 2020 1:20:39 PM cucumber.api.cli.Main run
WARNING: You are using deprecated Main class. Please use io.cucumber.core.cli.Main
Feature: This is a demo feature one

  Scenario: This is first scenario # /C:/Users/amomahaj/eclipse-workspace/MavenCucumber4JunitSampleProject/src/test/resources/featureFiles/DemoFeature.feature:3
Precondition is given sdffdsfsf
    Given Precondition is given    # DemoFeatureStepDef.precondition_is_given()
Something is done
    When Something is done         # DemoFeatureStepDef.something_is_done()
Something is expected
    Then Something is expected     # DemoFeatureStepDef.something_is_expected()

  Scenario: This is second scenario     # /C:/Users/amomahaj/eclipse-workspace/MavenCucumber4JunitSampleProject/src/test/resources/featureFiles/DemoFeature.feature:9
Another precondition is given
    Given Another precondition is given # DemoFeatureStepDef.another_precondition_is_given()
Something is done again
    When Something is done again        # DemoFeatureStepDef.something_is_done_again()
Something is expected again
    Then Something is expected again    # DemoFeatureStepDef.something_is_expected_again()

2 Scenarios (2 passed)
6 Steps (6 passed)
0m0.192s

Runner class is useful to run multiple feature files and you can do a lot of configurations. We will see those in upcoming post. As of now we are covering only a basic setup.

Create a package named say “runnerFiles” under “src/test/java“. Create a runner java class now as “RunJunitTest.java“. You must create runner class name ending with word “Test” so that it can picked automatically while running through maven.

Content of Runner file will be as below:-

package runnerFiles;

import org.junit.runner.RunWith;

import io.cucumber.junit.Cucumber;
import io.cucumber.junit.CucumberOptions;


@RunWith(Cucumber.class)
@CucumberOptions(features = "src/test/resources/featureFiles",
glue = "stepDefinitions")
public class RunJunitTest {     
}

Annotation @RunWith is used to run as Junit using Cucumber. If you use serenity BDD, there you need to mention “SerenityRunner.class“. In @CucumberOptions annotation, feature attribute is used to pass path of feature file and glue attribute is to give path of step definitions. There are many if and but with this but stick to it as of now.

Now run above runner file as “Run As – > JUnit Test“. You should output as below:-

Precondition is given
Something is done
Something is expected
Another precondition is given
Something is done again
Something is expected again

Right click on Project or pom.xml and select Run As -> Maven test. It will pick Runner class ending with “Test” and execute feature file during test phase of maven life cycle. You will see output contains:-

Running runnerFiles.RunJunitTest
Precondition is given
Something is done
Something is expected
Another precondition is given
Something is done again
Something is expected again
Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.363 sec

Results :

Tests run: 2, Failures: 0, Errors: 0, Skipped: 0

You can clone sample project from here.

If you have any doubt, feel free to comment below.If you like my posts, please like, comment, share and subscribe.#ThanksForReading

#HappyLearning

Uncategorized

Post navigation

Previous Post: JSONPath and Query JSON using JSONPath
Next Post: Understand Severity And Priority Of A Defect In Software Testing

Related Posts

Selenium Interview Question 7 – How to Select Last Five Checkboxes Uncategorized
Make Selenium Easy | Make Selenium Easy – Selenium Tutorials : End To End Uncategorized
Interview Experience at Genpact Bangalore for Selenium Testing Profile – Dec – 2019 Uncategorized
Java Interview Question – Explain public static void main(String[] args) Uncategorized
Hierarchy of Selenium Classes and Interfaces – Make Selenium Easy Uncategorized
amod mahajan make selenium easy Uncategorized

Recent Posts

  • Getting Started with Selenium 4: What Is New and How to Upgrade from Selenium 3
  • Manual Testing
  • Baby Steps To Become Efficient Selenium-Java Automation Tester
  • Features of Selenium 4.0.0 Release – Java Binding
  • Part 1: Handling Drop-down Created Using SELECT Tag In Selenium

Recent Comments

No comments to show.

Archives

  • April 2026
  • April 2025
  • March 2025
  • February 2025
  • January 2025
  • December 2024
  • November 2024
  • October 2024
  • September 2024
  • August 2024
  • April 2024
  • March 2024
  • February 2024
  • December 2023
  • October 2023
  • August 2023
  • November 2022
  • September 2022
  • August 2022
  • July 2022
  • May 2022
  • March 2022
  • October 2021
  • April 2021
  • March 2021
  • January 2021
  • December 2020
  • October 2020
  • September 2020
  • August 2020
  • June 2020
  • May 2020
  • April 2020
  • March 2020
  • February 2020
  • January 2020
  • December 2019
  • November 2019
  • October 2019
  • September 2019
  • August 2019
  • May 2019
  • December 2018
  • November 2018
  • October 2018
  • September 2018
  • August 2018
  • July 2018
  • January 2018

Categories

  • Getting Started
  • Uncategorized

Copyright © 2026 Make Selenium Easy.

Powered by PressBook Masonry Dark