Add Cucumber Features

After thinking about the requirements, it time to create the Cucumber feature files. And add them to the project.

To make it possible to run these Specification by Examples, Cucumber is used. The library Cucumber for Java Virtual Machine) contains the Java implementation.

Action Plan

  • Add the required Cucumber dependencies.
        <dependency>
            <groupId>info.cukes</groupId>
            <artifactId>cucumber-java</artifactId>
            <version>1.2.0</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>info.cukes</groupId>
            <artifactId>cucumber-junit</artifactId>
            <version>1.2.0</version>
            <scope>test</scope>
        </dependency>
  • Create directories for the Cucumber feature files and Cucumber step classes.

  • Create directory for Cucumber step classes
    mkdir src/it/java
  • Create directory for Cucumber feature file
    mkdir src/it/resources

  • Add the new directories as Eclipse source Folders
    Select both directories java and resources and use the mouse menu: Build Path > Use as Source Folder

  • Add Maven failsafe plug-in to the Maven Project Configuration pom.xml
    WARNING The Maven failsafe plug-in is a general Integration Test plug-in, by default is scans for test classes with specific patterns. The class RunCucumberTests does NOT fall in the pattern, so configuration is required. The Java test classes are not in a default Maven test classes directory src/test/java, so configration is needed.
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-failsafe-plugin</artifactId>
                <version>2.19.1</version>
                <executions>
                    <execution>
                        <id>run-integration-tests</id>
                        <goals>
                            <goal>integration-test</goal>
                            <goal>verify</goal>
                        </goals>
                        <configuration>
                            <includes>
                                <include>*RunCucumberTests.java</include>
                            </includes>
                            <testSourceDirectory>src/it/java</testSourceDirectory>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
  • Create a Java package with a name similar as the package name in src/main/java.
    The top level package name for the project is com.github.<github-user>.mrrs.
    Create a new package name in src/it/java for the integration tests com.github.<github-user>.mrrs.it.
    Inside this Java package the Integration Test related classes will be added.

  • Create a new class, in the package com.github.<github-user>.mrrs.it, that will run Cucumber feature files. Give this class a clear name like RunCucumberTests.

import org.junit.runner.RunWith;

import cucumber.api.CucumberOptions;
import cucumber.api.junit.Cucumber;

@RunWith(Cucumber.class)
@CucumberOptions(
        plugin = { "html:target/cucumber/report.html", "json:target/cucumber-report.json" }, 
        features = { "src/it/resources/" })
public class RunCucumberTests {
    // No implementation required
}

Install Cucumber for Eclipse feature

Use the Eclipse update site: http://cucumber.github.com/cucumber-eclipse/update-site

Create New Feature File

  • Create the Cucumber feature file room.feature, in the directory src/it/resources for the room features.
Feature: Rooms require a location and capacity. Optional they have a name and facilities.


Scenario: Room can be found by location
Given a room with name "Berlin", location "1.12" and capacity 12 
    And which has facility "whiteboard"
When searching for room with location "1.12"
Then the room with name "Berlin" should be returned
  • Create a new class RoomSteps place it in the package com.github.<github-user>.mrrs.it.cucumber.
    This class will be used to place the Given, Then and When methods in, generated by Cucumber, based on the feature file.
public class RoomSteps {

}

Check the Set Up

Run Cucumber through Eclipse Cucumber

  • Open the file room.feature.
  • Use the right mouse button in the file editor, or on the file room.feature, in the view Project Explorer / Package Explorer. Use the menu: Run As > Cucumber Feature
    This will show in the view Console, which Cucumber steps are still needed to be implemented.

    Eclipse Run Cucumber Examples Eclipse Run Cucumber Examples Eclipse Run Cucumber Examples

Run Cucumber through Eclipse JUnit

TIP To just only run the Cucumber Examples, select the RunCucumberTests class, from the view Package Explorer or Project Explorer and the use context menu: Run As -> JUnit Test
Eclipse Run Cucumber Examples with JUnit Eclipse Run Cucumber Examples with JUnit

Run Cucumber through Eclipse Maven

Written on May 10, 2016