Cucumber data table – convert a two column table to a Map

Cucumber is a tool for running automated acceptance tests written in a behavior-driven development (BDD) style. Cucumber is written in the Ruby programming language. Cucumber projects are available for other platforms beyond Ruby.

Cucumber works with Ruby, Java, .NET, Flex or web applications written in any language. It has been translated to over 40 spoken languages. – http://cukes.info/

The language that Cucumber understands is called Gherkin.

While Cucumber can be thought of as a “testing” tool, the intent of the tool is to support BDD. This means that the “tests” (plain text feature descriptions with scenarios) are typically written before anything else and verified by business analysts, domain experts, etc. non technical stakeholders. The production code is then written outside-in, to make the stories pass.

Cucumber itself is written in Ruby, but it can be used to “test” code written in Ruby or other languages including but not limited to Java, C# and Python.

In this example I will show you how we can use cucumber’s nice feature that helps us to use tables in our scenarios. The table can easily be converted to a list or map that we can use in our step.Prerequisites

Now we will see the below steps how to create a maven based project in Eclipse

Step 1. Create a standalone maven project in Eclipse

Go to File -> New -> Other. On popup window under Maven select Maven Project. Then click on Next. Select the workspace location – either default or browse the location. Click on Next. Now in next window select the row as highlighted from the below list of archtypes and click on Next button.

Step 3. If you see JRE System Library[J2SE-1.4] then change the version by below process

Do right-click on the project and go to Build -> Configure build path, under Libraries tab click on JRE System Library[J2SE-1.4], click on Edit button and select the appropriate jdk 1.8 from the next window. Click on Finish then Ok.

Step 4. Create src/test/resources folder, if it already does not exist, for putting the resource files.

Do right-click on the project and go New -> Source Folder. Give Folder name: as src/test/resources and click on Finish button.

Step 5. Create a feature files called employeelist.feature under src/test/resources/cuke/flow/feature with the below content

Feature: An organization recruits few more employees
"""
The organization got more projects from existing or new clients, so it has decided
to recruit more employees to provide services to its customers
"""
Scenario: An organization recruits employees based on designations
Given an organization has employees
| PM | 5 |
| Sr. Dev | 200 |
| Jr. Dev | 5000 |
When it recruits 5 more Sr. Dev and 150 more Jr. Dev employees
Then the organization will have 205 Sr. Dev and 5150 Jr. Dev

features – location of the feature fileglue – the package where the step definition class will be writtenmonochrome – whether we want the output in console in human readable formatformat – in what format and where we want the generated output file

Step 7. Once we run the above class the following steps are generated in the console

@Given("^an organization has employees$")
public void an_organization_has_employees(DataTable arg1) throws Throwable {
// Express the Regexp above with the code you wish you had
// For automatic conversion, change DataTable to List<YourType>
throw new PendingException();
}
@When("^it recruits (\\d+) more Sr. Dev and (\\d+) more Jr. Dev employees$")
public void it_recruits_more_Sr_Dev_and_more_Jr_Dev_employees(int arg1, int arg2) throws Throwable {
// Express the Regexp above with the code you wish you had
throw new PendingException();
}
@Then("^the organization will have (\\d+) Sr. Dev and (\\d+) Jr. Dev$")
public void the_organization_will_have_Sr_Dev_and_Jr_Dev(int arg1, int arg2) throws Throwable {
// Express the Regexp above with the code you wish you had
throw new PendingException();
}

Note: The parameter DataTable in step @Given can be converted to any one of List<YourType>, List<List<E>>, List<Map<K,V>> or Map<K,V>, where E, K, V must be scalar, i.e., String, Integer, Date, enum, Double etc.

Step 8.Create a step definition class that will hold all generated steps with implementations. Modify the generated steps according to our needs

Step 9. Run the cucumber test runner class at step 6. You will see the below output in the console.

Feature: An organization recruits few more employees
"""
The organization got more projects from existing or new clients, so it has decided
to recruit more employees to provide services to its customers
"""
Scenario: An organization recruits employees based on designations # cuke/flow/feature/employeelist.feature:8
Given an organization has employees # EmployeeListSteps.an_organization_has_employees(DataTable)
When it recruits 5 more Sr. Dev and 150 more Jr. Dev employees # EmployeeListSteps.it_recruits_more_Sr_Dev_and_more_Jr_Dev_employees(int,String,int,String)
Then the organization will have 205 Sr. Dev and 5150 Jr. Dev # EmployeeListSteps.the_organization_will_have_Sr_Dev_and_Jr_Dev(int,int)
1 Scenarios (1 passed)
3 Steps (3 passed)
0m0.098s