Issue 39

BDD by Cucumber

Sergiu Indrie
Software Engineer@iQuest



It seems a new test framework is gaining traction on the internet (especially in the Java area) reviving the concept of BDD. Cucumber is a software tool for running automated acceptance tests in a business-readable domain-specific language. BDD or Behavior-Driven Design, however, has been around for a while, the first articles and projects (JBehave [5] and RSpec [6]) appearing in 2007. As mentioned on the site of the project, Cucumber is a rewriting of the previous two frameworks.



The most important concept to grasp in this article is the concept of BDD. Behaviour-Driven Design is not just about choosing a relevant test framework and start writing tests, it's about really understanding the idea behind this development process (See articles [7] and [8]). Only then can we expect to obtain value from it. BDD was created out of a need for better collaboration between business people, developers and testers. If we want to profit from using this process we need to start with the idea of a joint effort between all stakeholders.

Cucumber Test

A Cucumber test requires the following 3 items

  1. a feature file
  2. step definitions
  3. business code

Feature File

A feature file is a plain text file written in Gherkin language, a business-readable domain-specific language that allows the description of the application's behavior in free text.

Below is a sample feature file:

Feature: Refund item

Scenario: Jeff returns a faulty microwave

Given Jeff has boght a microwave for $100

And he has a receipt

When he returns the microwave

Then Jeff should be refunded $100

The feature file is composed of at least one scenario (which is essentially a test) which is in turn composed of at least one step. Feature files can also have different supporting constructs like descriptions, comments, tags, a before hook and different ways to parameterize scenario steps.

Step Definitions

As we saw above, a scenario is composed of multiple steps like “Jeff has bought a microwave for $100” and “he has a receipt”. All those steps are plain text abstractions of supporting code also called glue code. In order to support the first step, we need to define a Java method like the following

@Given("^Jeff has bought a microwave for \\$(\\d+)$")
public void Jeff_has_bought_a_microwave_for_$(int amountOfMoney) {

The Gherkin step is matched to the Java method by using regular expressions. In this case the regex "^Jeff has bought a microwave for \$(\d+)$" will match “Jeff has bought a microwave for $100”. Notice that the microwave price is extracted as a regex group. This is the mechanism that Cucumber uses to extract variables from the free text specification.

The entire Java step definition file can be found below

public class RefundSteps {
   private Business business = new Business();
   @Given("^Jeff has bought a microwave for \\$(\\d+)$")
   public void Jeff_has_bought_a_microwave_for_$(int amountOfMoney) {
   @And("^he has a receipt$")
   public void he_has_a_receipt() {
   @When("^he returns the microwave$")
   public void he_returns_the_microwave() {
   @Then("^Jeff should be refunded \\$(\\d+)$")
   public void Jeff_should_be_refunded_$(int amountOfMoney) {

Step definition Java files can also use @Before and @After hooks similar to JUnit and a custom reporting mechanism by injecting a Scenario object where steps can output text or even image resources.

Business Code

Last, the step definition code needs to work with the application business code.


From the language point of view, Cucumber is very flexible, supporting all of the major programming languages like Java, .NET, C++, PHP and others. As for the Java side of things, there are 2 artifacts available: one for Java 8 and one for previous versions.

Some of the main supported frameworks are Spring, Selenium and Ruby on Rails.

Even though the framework is not extremely popular, it has been supported by the main Java IDEs for a while now (Intellij IDEA 12 and up; Eclipse starting with January 2014). With features such as syntax highlighting, step autocompletion, step navigation, step usage, step stub generation and test run, Cucumber development is fairly easy.

Maven integration is straightforward for basic usage (provided you use an empty test class annotated to run with Cucumber and place all artifacts in the same package) but more advanced use cases could benefit from a dedicated pure-Java Maven plugin (see [12]). (It may also be the case that your hierarchical feature file structure is more high level and thus different from your step definition package structure)

A Java test class that will run all features in the same package

package com.tsm.bdd;
import cucumber.api.junit.Cucumber;
import org.junit.runner.RunWith;
public class RefundTest {

The approaches to running Cucumber tests with a more complex setup using Maven are

Using the exec-maven-plugin


Using the maven-surefire-plugin

               --plugin pretty
               --plugin html:..\logs\report\html-report-java
               --plugin json:..\logs\report\json-reports\${json.name.java}
               --plugin junit:..\logs\report\junit-java.xml
               --tags ~@skip

It is worth noting that Cucumber can also be integrated with Jenkins with the help of plugins.


From a BDD perspective, Cucumber doesn’t add any major contribution especially when compared to JBehave (see [11]) which takes a very similar approach.

Overall, Cucumber seems to serve its purpose and provide easy collaboration between the involved parties with the use of the Gherkin language, and as a side-effect enhance the readability of developer written tests. However, from a programmer point of view, the tool feels not quite as mature with small gaps on test specification and writing (see [9]), unfinished documentation (see [10]) and complexity in configuring a unified way to run acceptance tests (Maven vs IDE).


  1. https://en.wikipedia.org/wiki/Behavior-driven_development
  2. http://dannorth.net/introducing-bdd/
  3. https://cucumber.io/
  4. https://github.com/cucumber/cucumber/wiki/Gherkin
  5. http://jbehave.org/reference/stable/index.html
  6. http://rspec.info/
  7. http://www.javacodegeeks.com/2015/04/pitfalls-of-cucumber-adoption.html
  8. http://www.javacodegeeks.com/2013/12/acceptance-testing-blaming-the-tools.html
  9. https://www.coveros.com/background-and-hooks-for-cucumber-jvm/
  10. https://cucumber.io/docs/reference#doc-string-argument
  11. http://mkolisnyk.blogspot.ro/2013/03/jbehave-vs-cucumber-jvm-comparison.html
  12. https://github.com/cucumber/cuke4duke/wiki/maven




  • Accenture
  • BT Code Crafters
  • Accesa
  • Bosch
  • Betfair
  • MHP
  • BoatyardX
  • .msg systems
  • Yardi
  • P3 group
  • Ing Hubs
  • Colors in projects


Sergiu Indrie wrote also