Test Framework Refactoring – Cucumber Datatables in Java

Test Framework Refactoring – Cucumber Datatables in Java

Test Framework Refactoring

Writing tests in Behaviour Driven Development(BDD) mantra means ensuring everyone involved with application features being developed are on the same page. I am currently working on testing some APIs with tests already written in Java and I have been tasked with presenting a BDD Proof-Of-Concept (POC) to the team as a good way to tests the APIs. Here’s a summary of what I am using to accomplish that task:

  1.   Java
  2.   Ant
  3.   Junit/TestNG
  4.   Cucumber
  5.   Intellij IDEA Ultimate version
  6.   Jenkins
  7.   Git/Github

Project Tasks

The list of main tasks include:

  1.   Separate test data from tests
  2.   Make tests extensible
  3.   Clean-up existing tests – in short code re-factoring
  4.   Introduce BDD into existing and future tests
  5.   Categorize/Annotate tests for selective running purposes

The current development environment uses Ant and not Maven, which I would have preferred, but Ant is fast and it is already in the system – deep deep :).   The Junit/TestNG thing is a result of the test annotation and categorization task.   Currently using Junit but the extra features in TestNG has made it more appealing.   This task is going to be the last step, to be honest and I am not very bothered about it at the moment.

State of Play

The existing tests are sufficient in ensuring a quick feedback if anything breaks in the system.  The API provides services to many regions (US, Europe, the Middle East and Africa,  Asia and the Pacific) and it is extremely important that the tests/monitoring systems give alerts if any part of the system/region is not responding as expected.

In getting the user stories (in .feature files) into existing test, writing the features, scenario outline and examples was not difficult.  The challenge I am faced with is ensuring that data in the “Examples” section of the feature files, are used in step definitions taking advantage of existing test methods.

Searching online for a good tutorial on getting data from datatables (cucumber) into Java tests was not fruitful hence I had to find a solution.   found a few links but the most helpful was from cukes.info itself.

Sample code:

Feature: Car deals returned

Scenario Outline: Ensure that latest car finance deals are returned

Given the car deals api is available
When a valid request is made via the api
Then the api call should be successful with httpStatus OK
And the api response should have deals <response_key> and <response_value>
| response_key | response_value |
| location | |
| userGender | |
| selectCar | |
| selectFinance | |

| location | userGender    | selectCar | selectFinance |
| london   | female              | bmw           | racket |
| berlin     | male                  | mercedes | grapes |
| paris       | male                  | audi            | orange |