Cucumber is a tool that supports Behaviour Driven Development (BDD). It enables one common language between IT and non-technical persons for creating up-front application feature specifications and desired behavior. In other words, it facilitates good cooperation between all project participants, such as developers, product owners, business analysts, and testers. There is a clear separation between natural language and test code, making it easier to write and read the specs for all, not just developers.
Cucumber is well-suited for large integrations or end-to-end tests involving many working components and microservices, as well as interactions between people with different skills and backgrounds. You can run so-called "platform tests" that check larger chunks of your business flows with many counterparts involved. Afterwards, a nice human-readable report will be automatically generated. If you are interested in more details, let's dive deeper.
Working with Cucumber consists of three steps:
A. Write feature test steps in Gherkin
B. Write "glue" code
C. Write application code.
Let's dive into each.
To start, you need to write the new feature specification in the DSL Gherkin. It should be a human-readable statement explaining the expected behavior of the new feature. Include a range of cases: optimistic, pessimistic, and corner cases at the end.
Gherkin's most common keywords are:
Feature, Rule, Example, Given, When, Then, And, But, Background, Scenario, Scenario Outline, Examples.
For a simple example, we could write a ".feature" file (".feature" is the file extension of Cucumber files) like this:
You can include as many Given/When/Then statements as you need, depending on your test requirements in a given context. Gherkin's format helps non-technical people understand the test file content, which is one of its biggest strengths.
Each step from the ".feature" file needs to know which code (application or test) should be run, which is why "glue" code is necessary. Luckily, you can automatically generate all "glue" method definitions using the IntelliJ plugin "Cucumber for Java." In the ".feature" file, point the cursor to the step and press "Alt + Insert" -> "Create step definition." You may also just ask Copilot to write the code for you.
Alternatively, you can run the feature test and copy all missing "glue" methods listed in the output. You only need to add the methods' implementation manually. Note that in the glue classes, we use private fields to exchange data between steps.
Finally, following the BDD methodology, we implement a simple domain service that we want to test.
Of course, you will need to add the necessary dependencies in the pom file. The most important ones are:
That's it. We just wrote the first Cucumber BDD test. Now run it (mvn test) and check the HTML report inside the target folder with test and step results summary.
Now we are ready to explore more Cucumber elements.
There is an option to write step definitions in a more concise form using Java 8 lambda functions. The glue code could then be as follows, with the rest remaining the same.
In case you need to run the test multiple times with different parameters, you can create a "Scenario Outline" with "Examples." See the example below:
To pass a list of values to a step definition, "Data Tables" are handy. In the glue code, you can then transform them into Lists and Maps of simple or Domain elements.
If you want to set this up before each test feature scenario, move the "Given" section into the "Feature: Background:" instead of the "Scenario."
Cucumber is a fantastic BDD tool for facilitating collaboration between technical and non-technical stakeholders. Based on a single specification file, each feature provides:
Using Cucumber does not guarantee non-technical colleagues will want to write specifications in Gherkin. In this case, you could consider BDD Spock, which requires less maintenance since the test specification and implementation are in the same file, not two separate ones.
Passing all Cucumber tests doesn't mean the application is entirely bug-free. Be careful not to fall into a false sense of security. The tool is an excellent choice for tests with a large scope, like end-to-end integrations, where you want to work actively on a technical-free document that outlines your system's flows with many modules underneath and involves stakeholders with various backgrounds.
Testing is a crucial step of the software development lifecycle. Contact us today to create high-quality Java applications together.