BDD tests for Hadoop with Cucumber. Part II
In the previous part, we have created the application to process the data and Docker container to isolate the testing environment.
In this part, we will create tests using Cucumber for Java and Gherkin.
Acceptance tests, usually, check the normal behavior of application — we want to be sure that the app does what is designed to do.
Gherkin is the great way to create acceptance tests — because it allows to concentrate on what we are going to do instead of how we do it, and, as a side effect, we get document features and scenarios in a human-readable format which is very helpful, especially for new team members.
Our application should sort the data to the different folders regarding caller countries, so let’s check it. To do that we need to create folders for cucumber features:
src -> test -> resources -> features -> callstream
Let's create a new file folder_structure.feature
for the feature inside callstream
folder.
At this point, we don’t check the output data files format, only the folders structure. To clarify that we should create an understandable and clear feature description in terms of Gherkin:
Now we can write the first ‘happy case’ scenario:
Scenario: processing data from one user from UA
Assume we have an input data file with one caller from Ukraine:
I don’t include other data fields, because we will use default ones. Then we should run the processing job.
When I run the CallStream job
And check that job create the correct folder structure:
And, of course, it should contain a file with processed data:
And, in summary:
It’s important to write these steps before implementation, so we will not limit our mind with details of our tests implementation. After the creation of the scenario, we should create an implementation of each step of the scenario. Implementation for steps lives in src -> java -> com -> steps
folder. For our callstream feature, we should create callstream
folder and add FolderStructureSteps.java
into it. Implementation of steps looks like that (I will not provide whole class here, it’s too large, and it’s not necessary to provide it here. You can take a look in article repository for more) :
So, it is pretty simple — we match step string to the Java regular expression in Give/When/Then annotations and wrote step implementation. For data fixture generation we use simple template generator Chunk Templates
. It uses themes
folder for templates, so we have to create a folder in themes
test resources and add calls_log.chtml
template with content:
As you can see we use default values for fields, which keep our scenarios in Gherkin clean and readable. Now we have a basic test structure for acceptance tests and everyone can extend the test suite and include output content checks or other important things.
Full code of the project you can find here:
https://github.com/savvyclutch/bdd_test_hadoop
Originally published at www.savvyclutch.com on December 21, 2016.