Fork me on GitHub

Using Cucumber with JUnit

Configuring Cucumber JUnit

To get started with Cucumber, you need to add the required version of Cucumber JUnit to your project:

 <dependencies>
   [...]
     <dependency>
         <groupId>io.cucumber</groupId>
         <artifactId>cucumber-junit</artifactId>
         <version>${cucumber.version}</version>
         <scope>test</scope>
     </dependency>
   [...]
 </dependencies>

Then create an empty class that uses the Cucumber JUnit runner.

package org.sample.cucumber;

import org.junit.runner.RunWith;

import io.cucumber.junit.CucumberOptions;
import io.cucumber.junit.Cucumber;

@RunWith( Cucumber.class )
public class RunCucumberTest
{
  [...]
}

This will execute all scenarios in the package of the runner. By default a glue code is assumed to be in the same package. The @CucumberOptions annotation can be used to provide additional configuration of Cucumber.

Note that in this example the BDD scenarios are executed by the Surefire Plugin in the test phase of the build lifecycle.

mvn test

Using JUnit Rules

The Cucumber supports JUnit annotations @ClassRule, @BeforeClass and @AfterClass. These are invoked around the suite of features. Using these is not recommended as it limits the portability between different runners. Instead it is recommended to use Cucumbers `Before` and `After` hooks to setup scaffolding.

Using other JUnit features

The Cucumber runner acts like a suite of a JUnit tests. As such other JUnit features such as Categories, Custom JUnit Listeners and Reporters and re-running failed tests can all be expected to work.