Back Home


  • Documentation of the system
    • Automated, executable acceptance tests

* Written in a human readable format

  • Business readable DSL
  • Describe software behavior without detailing implementation
  • Feature source files have .feature extension

Getting Started

Add to gradle

    compile 'info.cukes:cucumber-groovy:1.2.5'

In intelij create a test.feature file in src/test/groovy

Scenarios consist of steps

  • Given: Put the system in a known state before the user starts interacting with the system; pre-conditions
  • When: Describe the key action a user performs in this scenario
  • Then: Observe outcomes; observations should relate to business value of the feature

In Gherkin support Given/When/Then and table driven

Feature: Hello World
Scenario: Say hello

Given I have a hello app with the following names Howdy 
When I ask it to say hi
Then it should answer with "Howdy"
public class HelloWorldStepDef {
	private Hello hello;
	private String hi;
	@Given("^I have a hello app with \"([^\"]*)\"$")
	public void I_have_a_hello_app_with(String greeting) {
		hello = new Hello(greeting);
	@When("^I ask it to say hi$")
	public void I_ask_it_to_say_hi() { 
		hi = hello.sayHi();
	@Then("^it should answer with \"([^\"]*)\"$")
	public void it_should_answer_with(String expectedHi) {
		assertEquals(expectedHi, hi);

And run as a normal junit test class

import org.junit.runner.RunWith;
import cucumber.api.junit.Cucumber;
@Cucumber.Options(format = {"pretty", "html:target/cucumber"}, features = "src/test/resources/bskyb" )
public class RunCukesTest {

Table Driven Examples

Given I have the following foods
|name |healthy|
|Orange|Yes |
|Chips |No  |
When I count the number of healthy items
   Then I have 1 healthy item

In code the test data is regular expressioned out:

Given(~'I have the following foods') { table ->
   basket = new Basket()
  table.hashes().each {
   def item = new Food(
   name:, // it.get('name')
     healthy: it.healthy == 'Yes')
When(~'I count the healthy items') {
   numberOfHealthy = basket.numberOfHealthy
Then(~'I have (.+) healthy items') { int count ->
    assert numberOfHealthy == count

Table Driving Tests

Tables on the given provide a batch of data Scenario Outline can be used to iterate on the test

Scenario Outline: Eating
  Given there are <start> cucumbers
  When I eat <eat> cucumbers
  Then I should have <left> cucumbers

    | start | eat | left |
    |  12   |  5  |  7   |
    |  20   |  5  |  15  |


  • @Before Executes after the last step of each scenario
  • @Execute regardless if there are failing, undefined, pending or skipped steps
  • @AfterStep Executes after each step in a scenario
  • @database tag to load the database via DBUnit

Custom tag's can be used to configure the initialization of each test:

Before {
   // Initialise something
Before('@tagname') {
   // Initialise only for features/scenarios
   // tagged with @tagname
Before('~@tagname') {} // not tagged
After {
   // Clean up something

Clean-up Database Hook example:

After () {
   def sessionFactory = appCtx.getBean("sessionFactory")
   def dataSource = appCtx.getBean("dataSource")
   //clean fixtures
   println "Deleting the fixture..."
   def db = new Sql(dataSource)
   db.execute("DELETE FROM CANDIDATE;")

Cucumber JVM

  • Cucumber-JVM is a pure Java implementation of Cucumber

Hello World

Say hello and validate it was output


Feature: Hello World
  Scenario: Say hello
    Given I have a hello app with "Howdy"
    When I ask it to say hi
    Then it should answer with "Howdy World"
import cucumber.junit.Cucumber;
import org.junit.runner.RunWith;
@Cucumber.Options(format = {"pretty", "html:target/cucumber"})
public class RunCukesTest {

The Cucumber test can be run as a normal junit test

Cucumber-jvm Options

There are a number of options to manage the test execution:

  • Options can be configured to run a directory
  • To run individual features
@Cucumber.Options(features={"001-OQ_List.feature", "002-OQ_List.feature"})
@ExtendedCucumberOptions(jsonReport = "target/cucumber.json",
        retryCount = 3,
        detailedReport = true,
        detailedAggregatedReport = true,
        overviewReport = true,
        toPDF = true,
        outputFolder = "target")

A good list of the Options list

Using Tags

Tests can be tags and then executing accordingly

@Functional Feature: Your feature 1 here

@Integration Feature: Your feature 2 here in another file

Mule Cucumber Hooks

import javax.annotation.Resource;
import org.mule.api.MuleContext;
import org.mule.module.client.MuleClient;
import org.mule.tck.junit4.FunctionalTestCase;
import org.springframework.test.context.ContextConfiguration;
import com.bskyb.gonzales.TestContext;
public class Hooks {
	public TestContext testContext;
	BrokerService broker = null;
	private class MuleTest extends FunctionalTestCase {
		protected String getConfigResources() {
			return "rbsa-ingestor.xml";
		public MuleContext getMuleContext() {
			return muleContext;
	private MuleTest muleTest = null;
	public void startMule() throws Exception {
		muleTest = new MuleTest();		
		MuleClient client = new MuleClient(testContext.muleContext);
		testContext.muleContext = muleTest.getMuleContext();
	public void stopMule() throws Exception {
	public void startActiveMQ() throws Exception {
	public void stopActiveMQ() throws Exception  {

Cucumber Reports

Reports can be generated:

  • from junit code
  • from gradle build file

The report builder assumes that the tests have been configured to create a json file first. The report builder is the pointed to the the Json output file.

The following code sample generates results overview report based on Cucumber JSON report stored at ./src/test/resources/cucumber.json location. The output directory is target and the file prefix is cucumber-results. The code is:

CucumberResultsOverview results = new CucumberResultsOverview();

As the result of this code there would be target/cucumber-results-feature-overview.html file generated. Cucumber usage report works in the same fashion except it uses report produced as the part of usage Cucumber JVM plugin. The following example generates usage report into the target folder based on JSON usage report generated by Cucumber and located at ./src/test/resources/cucumber-usage.json path:

CucumberUsageReporting report = new CucumberUsageReporting(); report.setOutputDirectory(“target”); report.setJsonUsageFile(“./src/test/resources/cucumber-usage.json”); report.executeReport(); The output would be the HTML file located at target/cucumber-usage-report.html. This is how those reports are generated. Now let's take a look at what exactly is generated.

jvm_bdd.txt · Last modified: 2018/02/16 09:40 by root
RSS - 200 © CrosswireDigitialMedia Ltd