Getting started with Cucumber using Spring and JUnit

Cucumber is a very nice testing framework written in the Rubby programming language, which follows the BDD (behavior-driven development) methodology.

Cucumber adds an extra layer of abstraction to our testing paradigm. This layer of abstration is the highest abstraction there is - the English language. Cucucmber converts a feature written in English to executable code, enabling every person, regardless of their coding expertise, to understand exactly what the feature tests and the expected output.

Since this framework is written in Rubby, it doesn’t have a straight forward integration with Java. Here, I’ll show you one way to start using “Cucumber-Jvm”, a Cucumber implementation for the most popular JVM languages, using JUnit as a runner class. I will also show an integration of Cucumber with Spring.

Cucumner basics

As we mentioned, Cucumber converts features written in English to executable code. So, a feature can be thought of as any stateful “black box” in your application that receives input and generates output. Given the “black box” has a state, for each input, we expect to see a specfic output.

For example:

Feature: File permissions


  • Given a file with root permissions
  • When user is root
  • Then access is granted


  • Given a file with root permissions
  • When user is guy
  • Then access is denied

Given, When, and Then are keywords in Gherkin, Cucumber’s plain text DSL.

Before starting to code, it is considered a good practice to write down the entire feature file. This gives us a general outline of how the code behind the feature should look like.

Let’s get started

Enough with high-level theory, let’s see to the underlying details.

A full Cucumber flow is comprised of 3 components: feature file, steps file, and runner file.

Feature file:

An example of a feature file with one scenario looks like this:

Feature File

Steps file:

Cucumber knows how to glue these English sentences to java methods using regexes. The file used to run these sentences is called a “Steps” files, because it runs the feature “step by step”. Each step in the feature is matched against a regex, owned by a specific method in the Steps file. Arguments can be passed to the method via the regex.

An example of a Steps file matching the above “.feature” file looks like this:

Steps File Header

Steps File Footer

Note: in the image above, the Steps file is a Spring bean (as you can see we can wire to it other beans).

Runner file:

The last member when running a cucumber test is the “JUnit-Runner” class, an empty Junit class that is used as a boot class.

An example of a runner class looks like this:

Runner File

Here, we tell JUnit to run the test using Cucumber - @RunWith(Cucumber.class), and tell “Cucumber-Jvm” which configurations to run - @CucucmberOptions. The location of the “.feature” file as well as the location of the directory containing the Steps file is mandatory.

Thats it! you can now start implementing Cucumber features in Java.

Package explorer:

Package Explorer



Advanced features

Cucumber extends far beyond this basic example. If you want to dive deeper into cucumber, you can read more about the following topics:

  • Running “Scenario Oulines” - same as running regular scenarios, but you can add paramters and run the same scenario with different arguments.
  • Integration with Jenkins - Cucumber can export the test results to Json format, which in turn can be read by Jenkins and shown in a nice flash format.
  • Cucucmber Tags - each feature/scenario can have a user defined tag. When running a full flow, you can tell Cucumber-Jvm to run only scenrios with specific tags (e.g. @Sanity), or on the contrary you can tell it to run all scenarios without a specific tag (e.g. @Ignore).