Make Selenium Easy

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

Last updated on April 25th, 2020 at 12:48 am

*****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.

Step 1:- Setup 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.

Step 2 :- Create a Maven project and add required dependencies

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:-

You could see an extra <properties> 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.

Step 3:- Create a feature file

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:-

Step 4:- Generate Step definition

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:-

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:-

Step 5:- Run Feature file

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:-

Step 6:- Run Feature file using Runner class

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:-

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:-

Step 7:- Run Feature file using maven commands

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:-

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

You can find all Selenium related post here.
You can find all API manual and automation related posts here.
You can find frequently asked Java Programs here.

Author: Amod Mahajan

My name is Amod Mahajan and I am an IT employee with 6+ years of experience in Software testing and staying in Bengaluru. My area of interest is Automation testing. I started from basics and went through so many selenium tutorials. Thanks to Mukesh Otwani as his tutorials are easy and cover basics to advance. I have habit of exploring concepts by deep diving. I used to make notes. I thought of sharing my knowledge through posts and now I am here. #KeepLearning #ShareLearning

2 thoughts on “Cucumber Tutorial 1 – Setup a Basic Maven-Cucumber 5 -Junit Project in Eclipse

  1. Why getting below error while trying to run junit runner class with testng xml.
    Using runner its working but not with testng xml.

    un 11, 2020 6:00:31 PM io.cucumber.testng.TestNGCucumberRunner
    WARNING: By default Cucumber is running in –non-strict mode.
    This default will change to –strict and –non-strict will be removed.
    You can use –strict or @CucumberOptions(strict = true) to suppress this warning
    Jun 11, 2020 6:00:31 PM io.cucumber.core.runtime.FeaturePathFeatureSupplier get
    WARNING: No features found at classpath:/makroRunner

    0 Scenarios
    0 Steps
    0m0.010s

    Runner class:
    @CucumberOptions(
    features=”src/test/resources/features”,
    plugin = {“pretty”,”html:target/cucumber”,”json:target/cucumber/cucumber.json”,”com.aventstack.extentreports.cucumber.adapter.ExtentCucumberAdapter:”},
    glue=”makroStepDefinations”,
    monochrome = true,
    tags= {“@signIn”},
    strict = true

    )
    public class RunnerTest extends AbstractTestNGCucumberTests {
    @Override
    @DataProvider(parallel = true)
    public Object[][] scenarios() {
    return super.scenarios();

    }

  2. Very Productive Stuff.
    If I want to maintain all my feature file in Excel and and run my feature with Yes or No basis and all respective test data for each feature how can we do.

Leave a Reply

Please wait...

Subscribe to new posts to become automation expert

Want to be notified when my new post is published? Get my posts in your inbox.

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

by Amod Mahajan time to read: 6 min
2
%d bloggers like this: