Photo by Darius Soodmand on Unsplash

Using Cucumber DataTable For CRUD Operations

Arun B Chandrasekaran

--

Feature files are created using Gherkin keywords. These feature files support representing data in the form of rows and columns called DataTable. This article is to show you one of the patterns to create feature files using Cucumber DataTable for CRUD (Create, Read, Update & Delete) use cases.

DataTable is similar to a spreadsheet. If product owner like reading data in the form of spreadsheet, then using DataTable in feature file is a good option.

Assuming you have a fair understanding of what is BDD, Cucumber, Gherkin and DataTable, let’s start with a feature files for Create use case. Incase you want to learn these topics you may refer to Cucumber Documentation and Cucumber For Java Book.

Note: Feature files & sample code in this article will use Employee & Phone Object. One Employee may have many phones (OneToMany).

Data Model

Src: EmployeeEntity.java, PhoneEntity.java

1. Create Employee

Let us see the feature file, its step definitions and the html report for basic create employee use case.

1.1 Feature File — Create Employee Use Case (Basic Test)

Src: 100-create-employee.feature

You may be wondering why can’t we use First Name instead of firstName (Java field naming convention) in the DataTable header row.

First important step or convention to follow is to use field names in Java class as column header in DataTable so that it is easier to convert DataTable to Java object when creating step definition.

Source code shown below is build using below dependencies.

<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-java8</artifactId>
<version>4.2.0</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-spring</artifactId>
<version>4.2.0</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-junit</artifactId>
<version>4.2.0</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>net.masterthought</groupId>
<artifactId>cucumber-reporting</artifactId>
<version>4.2.3</version>
</dependency>

Src: pom.xml

1.2 Step Definitions — Create Employee Use Case

Src: EmployeeSteps.java, AbstractSteps.java

1.3 Cucumber Test Report — Create Employee Use Case (Basic Test)

Below report is generated using Cucumber Reporting library.

Cucumber report for basic test

Looking at the report you may have a fair idea on how the DataTable will be shown in the output.

1.4 Feature File — Create Employee Validation Use Cases

Below feature file is a sample to demonstrate how to create feature files to test validation use cases during create employee.

Src: 100-create-employee.feature

Note 1: DataTable columns ‘testCase’ and ‘expectedResult’ are being used to dynamically create scenario title.

Note 2: This feature file (having validation use cases) also uses the same step definitions that was implemented for basic test (the one stated above).

Note 3: If you look at the feature file stated above, you can see various validation use cases being tested under examples.

1.5 Cucumber Test Report — Create Employee (Validation Use Case)

For simplicity, below image is an example of one of the validation use case.

Cucumber report having all validation test cases

2. Get Employee

Now let us see how to create feature files for Get Use Case using DataTable.

2.1 Feature File — Get Employee By ID

Src: 200-get-employee.feature

Note: In this feature file, first an employee is setup in the system using ‘Background’, then Get By Employee Id use case is being tested to be successful and also the expected return value.

3. Update Employee

Update and create are more or less the same. Below is the feature file for update including the validations use cases being stated as examples.

3.1 Feature File — Update Employee

Src: 300-update-employee.feature

Note: If you look at the feature file stated above, you can see various validation use cases being tested under examples.

4. Delete Employee

Get and Delete feature files are more or less the same.

4.1 Feature File — Delete Employee By ID

Src: 400-delete-employee.feature

Final Thoughts

So far we saw how we can use DataTables to create feature files for CRUD Use Cases. Idea is to simplify the development effort required to create and maintain feature files and step definitions. Benefits of using Java field names as column headers for data table, following a consistent style or pattern in all Gherkin steps need to be made clear to business/product owner.

Note: Pretty print reports will not display DataTable, hence you may want to use Cucumber Reporting frameworks like the one shown in this article if you decide to use a lot of DataTable in your feature files.

Happy BDD!

Complete working source Code is available in GitHub.

References

https://www.amazon.com/Cucumber-Java-Book-Behaviour-Driven-Development/dp/1941222293

--

--

Arun B Chandrasekaran

Principal Software Engineer at World Fuel Services. My job is my hobby.