Setting up a Cucumber-JVM project in IntelliJ

 

Getting set up with Cucumber-JVM for the first time with an IDE can be a tricky thing to get right. There are a number of dependencies and plugins to install and configure which might require a fair amount of searching StackOverflow and Cucumber repositories. All of this can be time-consuming and frustrating. In this post, we are going to go through step-by-step what it is required to get a Cucumber-JVM project set up to the point where you can run it inside your IntelliJ IDE, as well as run via Maven.

You can either follow this post as you set up your project, or if you are looking to get started straight away, you can download a project template from our repository. At the time of writing, we will be using Java 1.8, IntelliJ CE 2017.3 and Cucumber-JVM 2.3.1. So let’s get started!


Set up your maven project

Our project will be managed via Maven, so our first port of call is to create a new Maven project in IntelliJ. Simply create a new project by opening the IntelliJ project wizard and choosing a Maven project:

 New maven project screenshot

Click on ‘Next’ and you will be asked to fill in your Maven project details:

 New project, fill in Maven project details screenshot

Once you have entered your project details, set where you want to store your project locally and click 'Finish' to create your project:

 Finish project screenshot

Set up your dependencies

With your Maven project set up, IntelliJ will have generated a pom.xml file to which we will add our dependencies, to install and run Cucumber-JVM:

<dependencies>
    <dependency>
        <groupId>io.cucumber</groupId>
        <artifactId>cucumber-java-</artifactId>
        <version>2.3.1</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>io.cucumber</groupId>
        <artifactId>cucumber-junit-</artifactId>
        <version>2.3.1</version>
        <scope>test</scope>
    </dependency>
</dependencies>

Once complete, your pom.xml will look similar to the following:

 cucumber jvm template screenshot

The cucumber-java dependency is the core Cucumber library we will be using to run our feature files. The cucumber-junit dependency provides support for us to run our feature files as part of the Maven build cycle.


Create your First feature File

Now we have Cucumber-JVM installed, we want to create a feature file to run. Before we create our feature file we need to create a package named resource within src/test. This means you end up with a path of src/test/resource.

 create package screenshot

Once you have your resource package, create your feature file inside the package and add content like the example below:

Feature: Demo feature

Scenario: Demonstrating how Cucumber-JVM works
    Given I have a configured Cucumber-JVM project 
    When I run it within my IDE 
    Then I will be able to run connected step definitions

The resulting feature file should look something like this:

 demo.feature screenshot


Install and conFIgure cucumber plugin

With our feature file in place, our next step will be to run the file and let it locate matching step definitions to execute. Currently, we don’t have any means to run our feature file so we need to install the Cucumber plugin for IntelliJ. Head to Preferences > Plugins and click on 'Install JetBrains plugin...':

 Android support plugins screenshot.

In the search box, search for 'cucumber for java' and when it appears as a result, click 'Install'. You will have to restart IntelliJ once complete.

 cucumber for java screenshot

With the necessary plugin installed we are now able to create a run configuration that we can use to run our feature file. So let’s create a default run configuration to run our feature file with. Head to Run > Edit Configurations… and open up your defaults list and select Cucumber java:

 Run/Debug configurations screenshot

For the default setting you will want to set the following settings:

Main class - cucumber.api.cli.main

Glue - stepdefs

Use classpath of module - The name of your Maven module (cucumber-jvm-template)

 cucumber java config screenshot

With the defaults set, we head over to our feature file and right click onthe first line of the feature and select Run 'Feature: demo':

 Run feature demo screenshot

When you click on menu option, the feature will run and you will get an output like this:

 Test results screenshot


Running step definitions in intelliJ

Our feature file is now running inside IntelliJ but is looking for step definitions to execute that don’t currently exist, so let’s create them. We can copy the step definition code from the output of the previous run to drop into a file, but first we need to create a new package named stepdefs in src/test/java.

 src. test. java, stepdefs screenshot

Notice how the package for our step definitions is the same name as the package we set in the Glue setting for the run configuration. This is so that the run configuration knows where to look for the step definitions when a feature file is run. Inside our package, we create a new class called DemoDefinitions and copy the step definitions from the previous run's output:

 Package stepdefs screenshot

Now, when we run our feature file again, we will get a different output:

 0 of 3 tests failed screenshots

Our step definitions are now set up to be filled in with code to run automation against a product, and we can run our feature files inside IntelliJ.


Running via Maven

Finally, we want to ensure that our Cucumber scenarios can be run as part of a Maven build process. Currently, when we run mvn test, it tell us there are no tests to run. We need to add a final class in to allow Maven to hook into Cucumber. So, create a new class inside src/test/java named RunCukesTest.

 Run Cukes Test screenshot

With the new class created you need to add the following above the class declaration:

@RunWith(Cucumber.class)
@CucumberOptions(<dependency>
        features = {"src/test/resource"}
)
 RunCukesTest java screenshot

The Cucumber options provided point to the folder where our feature files are stored, so that when we run Maven again we can see that the scenarios have been run

 TESTS terminal screenshot

Wrapping up

At this point, we now have a Cucumber-JVM project set up to start developing on. We can add multiple new feature files within our resource package, and add matching step definitions inside the stepdefs package.

We also have the option to run our feature files inside IntelliJ when we are actively developing them, and the option to run them via Maven when building our project.


You may also like…