How to write automated tests using Cucumber (2/7)

Artur Mishustin
5 min readOct 22, 2020


In this article we will write simple automated tests with the Cucumber library.

Photo by Natalie Rhea Riggs — Unsplash

The code from this article is located here.

Branches represent tool on different steps of readiness, according to the articles list.

Further screenshots are taken in the IntelijIdea IDE.

Software requirements

We will use Java 8 programming language provided via Amazon Corretto and Maven 3.6.3.

Cucumber plugin for the IntelijIdea

My recommendation, is install this plugin first.

It makes work with the Cucumber *.feature files and gluecode much easier.

Project setup

Create new maven project:

Select menu New — Project
Select Maven and press Next

Dependencies configuration

Then we need to add Cucumber dependencies to the project. Open pom.xml file located on your project folder:

And add code below:

Current Cucumber version is 6.4.0. Check maven repository for the latest one. Then add plugin for the code compilation. Add code below to the pom.xml:

With the all provided changes, pom.xml file should be like this:

After that, press the right button on the pom.xml file and click Reimport
IDE will download all dependencies:

Cick Reimport button

Package structure

The are no rules how packages structure should looks like. We will create two folders, features and steps. Inside this folders you can create any subfolders
to structure your code (we will do it later for the API and Web tests).

Created packages

TestNg runner

We will implement testNg runner for the Cucumber tests. This file will be executed by maven, provide configuration to the Cucumber and start execute feature files.

Create CucumberRunnerTest class with the code below:

Feature and Glue parameters describes where Cucumber will be looking
for a features and step files. Junit plugin will generate Junit XML test report which used for the test results importing into various applications
and services.

Catalog with the test configuration

Most of the time automated tests will be executed on systems placed across different environments. We will create a folder with the configuration files for the different environments and with the test run commands will provide
a parameter to choose configuration file to read.

Create env_config folder:

Create 3 files:,,

Add the next dummy variables to the files:

host=" to the;
host= to the;
host= to the;

Now it’s time to implement configuration reader for created properties files.

Test Configuration class and Run configuration

We need a class which will be read and store all parameters from
the configuration file, depends on the “environment” parameter.

Create class TestConfiguration:

Add two variables env and host:

Add method readProperties, which safely read configuration file and returns Properties object. And method readConfig for the reading configuration file and assigning values from it to the variables.

Parameter environment will be placed via run command in the console, but when test executed via IDE, this parameter is missing. With time, variables list will be grown: test scope, credentials, keys and others. For the local test development purposes, these parameters will be read from a local file.
Also, from a configuration files these parameters are easy to read and edit.

Create file and IMPORTANT: this file should be immediately added to the .gitignore. It should not be ever committed to the repository
or somehow published. Use only secured and encrypted ways of communication to share it.

TestConfiguration will check, does require parameters are in memory.
If not — read them from the file.

Let’s implement readRunProperties method:

At the end, TestConfiguration class should be like this:

Test Initialization

Before the first test will be executed, we need to perform test initialization actions, like reading configuration file and initializing HTML report file.

Create class TestInitialization with the one static method called init:

Method init contains all initialization actions. At the current moment,
it obtains run properties (env variable) and based on this variable read appropriate configuration file.

First feature file

Finally, we are ready for the writing the first Cucumber feature file. For the Gherkin and Cucumber tutorials I suggest official Cucumber documentation.

Create system.feature file:

These tests shows current time, date, OS name and configuration details to be sure that project setup performed well.

Add code below to the system.feature file:

Add glue code

Create class SystemInfoSteps:

We need to implement methods for each step defined in a feature file.

Steps from the feature file will be mapped with the attribute value on the top of a method. We will implement real tests with the assertions on a next article. For now, test methods just prints some system information:

Execute the tests

The final version of the execution tool after all manipulation stored here.

Let’s finally execute our tests! For the first time, via IDE. Choose system.feature file and click Run.

Console output will be:

Then, execute via maven and pass env parameter into the run command.

Execute commend below in a console: mvn clean test -Denv=qa

Great job! Check Article 3 and find how to implement Web automated tests using Selenium library!



Artur Mishustin

Test automation engineer, writer