First scenario in Cucumber using JUnit

In this tutorial, we will write our first test scenario using cucumber tool. The testing framework used in this tutorial is Junit.

After creating a project in eclipse add below jar files into the project:

– cucumber-junit

– cucumber-reporting

– junit

You can download these jar files from Maven website. (

If you have created a Maven project add below dependencies:

Create a feature file:

Create a Java project or Maven java project.

Create a folder with name feature.

Create a feature file say with name Login.feature. Right-click project name >> new folder with name feature >> new feature file with name Login.feature.

What is a feature file?

Feature file is a plain text file with .feature extension. We write test cases in the feature file using Gherkin language.

Let us see an example of a feature file and then we will understand each statement:

In the above code, you can see that I have written a test case in plain English language. The code starts with Feature keyword which tells about what feature this file consists of and the next statement has a description of the feature.

Next, we have is Scenario keyword which is like my first test case. The scenario tag tells you about the scenario we are going to write.

A feature can have multiple scenarios

After that, we write the steps to execute in the scenario using tags like Given, When, And, Then But etc.

Keywords or syntax used in Gherkins:

  1. Feature
  2. Background
  3. Scenario
  4. Given
  5. When
  6. Then
  7. And
  8. But
  9. Scenario outline
  10. Examples
  11. Scenario Templates

When you will create the feature files it will show you a warning with each step stating – Step <step> does not have a matching glue code. See the below screenshot.

How does Cucumber work?

– Cucumber first reads the feature files and identify all the steps in that feature file.
– Then Cucumber maps these steps with the step-definition (using TestRunner class).
– Then Cucumber executes these steps of step definition on the Application.

Create a Test Runner:

Next, we will write a code which will execute this code written above.

  • Create a package with name testRunner.
  • Create a class with name
    • In class add an annotation @RunWith(Cucumber.class) where @RunWith is from org.junit.runner.RunWith class and Cucumber.class is from cucumber.api.junit.Cucumber class.
    • Add @CucumberOptions annotation – this informs test engine with some properties
      • informs the test engine where are the feature files.
      • glue – It is to bind the feature files with step definition

See the below code:

Create a Step Definition class:

Next step is to create a Step definition class in steps package. Right-click steps package >> new >> class >>

Right click on class >> Run As >> JUnit Test cases. It will execute the code and will show you some message in console. like below:

Copy all the steps from above and paste them into class and edit each step. You can also change the name of the methods. See the below code:

Wherever there is (.*) symbol it is a special character and is used to pass a parameter in a test step. Like in above code email Id and password are passed as an argument in the test step.

We will learn about these special characters in detail in upcoming tutorials.

Run the code again and you will get below output:

In the next tutorial, we will learn about the passing multiple arguments using Data Tables in a step.

Saurabh Dhingra

About the Author

Saurabh Dhingra

Follow Saurabh Dhingra: