All Collections
Behave Pro Classic
Build Tools
Cucumber-JVM - Maven (Java & Scala)
Cucumber-JVM - Maven (Java & Scala)

Export features and scenarios for test automation with Cucumber when using Maven as your build tool

Alan Parkinson avatar
Written by Alan Parkinson
Updated over a week ago

Behave Pro can export the created Feature and Scenarios to any gherkin compatible test automation tool. For maven based projects we have a plugin that will export the features directly from Jira during the correct maven life cycle.

Behave Maven plugin

The first step is to add the Hindsight Software Maven repository to your projects Maven pom file.

<pluginRepositories>
    <pluginRepository>
        <snapshots>
            <enabled>false</enabled>
        </snapshots>
        <id>hindsighttesting.release</id>
        <name>Hindsight Software Release Repository</name>
        <url>https://repo.hindsightsoftware.com/public-maven</url>
    </pluginRepository>
</pluginRepositories>

Next, the plugin declaration can be added to the pom file.

<plugin>
    <groupId>com.hindsighttesting.behave</groupId>
    <artifactId>behave-maven-plugin</artifactId>
    <version>1.0.5</version>
    <configuration>
        <server>https://behave.pro for Cloud OR Jira Server URL</server>
        <projectKey>10100</projectKey>
        <username>Cloud userId OR Jira Server Username</username>
        <password>Cloud API key OR Jira Server Password</password>
    </configuration>
    <executions>
        <execution>
            <goals>
                <goal>features</goal>
            </goals>
        </execution>
    </executions>
</plugin>

The plugin has 4 required parameters, server, projectKey, username and password. These 4 parameters are different depending on if you are using Jira Cloud or Server. You can retrieve the correct details the Jira project's admin area; 'Project Settings' > 'Behave Pro' > 'Generate config'. This article has more information on finding these configuration items

Optional proxy settings

If necessary, a proxy server can be added which will be used to download the features. If your proxy server requires no authentication, you can exclude the httpProxyUsername and httpProxyPassword parameters. 

<plugin>
    <groupId>com.hindsighttesting.behave</groupId>
    <artifactId>behave-maven-plugin</artifactId>
    <version>1.0.5</version>
    <configuration>
        <httpProxyURL>http://my-proxy-server:1234/</httpProxyURL>
        <httpProxyUsername>my-proxy-username</httpProxyUsername>
        <httpProxyPassword>my-proxy-password</httpProxyPassword>
        <server>...</server>
        <projectKey>...</projectKey>
        <username>...</username>
        <password>...</password>
    </configuration>
    ...
</plugin>

Configuring Cucumber

The maven plugin by default will export all the features to the folder 'target/generated-test-sources'. This default can be overridden by adding the 'outputDirectory' parameter to the maven plugin configuration. We always recommend using a folder within the 'target' directory so the exported files can be cleaned up by maven if required.

<outputDirectory>${project.build.directory}/generated-test-resources</outputDirectory>

Next, you will have to tell cucumber where to find the exported feature files using the 'features' configuration option. 

@RunWith(Cucumber.class)
@Cucumber.Options(features = { "target/generated-test-sources/" })
public class CucumberIT {

}

Configuring Cucumber from scratch 

If you don't have them already, the first step is to add the cucumber dependencies to the maven project. We will be using Cucumber’s JUnit runner to run our tests so JUnit will also be a dependency.

<dependencies>
    ...
    <dependency>
        <groupId>info.cukes</groupId>
        <artifactId>cucumber-picocontainer</artifactId>
        <version>1.0.10</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.picocontainer</groupId>
        <artifactId>picocontainer</artifactId>
        <version>2.14.1</version>
    </dependency>
    <dependency>
        <groupId>info.cukes</groupId>
        <artifactId>cucumber-junit</artifactId>
        <version>1.0.10</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.10</version>
        <scope>test</scope>
    </dependency>
    ...
</dependencies>

Create a java class called “CucumberIT” in the “src/test/java” folder of your maven project. Annotate this class with the following annotations.

import org.junit.runner.RunWith;
import cucumber.junit.Cucumber;

@RunWith(Cucumber.class)
@Cucumber.Options(format = { "json", "json:target/cucumber.json" }, features = { "target/generated-test-sources/" }, strict = true)
public class CucumberIT {

}

These annotations turn a standard JUnit test into a cucumber test which loads and executes the scenarios from Jira (The behave-maven-plugin generates these). To run this JUnit/Cucumber test during the Maven integration-test phase you need to add the maven failsafe plugin.

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-failsafe-plugin</artifactId>
    <version>2.12</version>
    <executions>
        <execution>
            <id>integration-test</id>
            <goals>
                <goal>integration-test</goal>
                <goal>verify</goal>
            </goals>
        </execution>
    </executions>
</plugin>

When you put this all together and run it using “mvn verify” the acceptance tests are extracted from Jira and run by cucumber. From the output of Maven you can see the tests failed.

They failed because Cucumber is trying to match each step in your Scenario with a method in Java and could find any matches. If it finds a matching method it will execute it or it fails the scenario (Test) and skip over the remaining steps in the scenario.

All that is left to do is implement the cucumber step definitions. When Cucumber can’t find a matching method to a step it will generate a code snippet for you to copy into your code and implement. In the console before the test results you will see code snippets for all the steps. 

All that is left to do is implement the cucumber step definitions by creating a new Java class in our project and copy ‘n’ paste the snippets

Did this answer your question?