If you’re writing a program in Java and using Maven, you may want to automate the calls for running your feature files in Maven whenever you try to build your code. Here’s how to run Cucumber Test in Java automatically whenever you try to build your Maven Project.
In this example, we will use our existing code for String Palindrome Cucumber Test and Sign Up Cucumber Test. Previously, if we want to run our Gherkin features, we either right click the feature file and run or create a run configurations. But how about if we have a lot of feature files that we need to run? Or we have Jenkins that automate our builds? In that case we can create a Class that defines the runner configurations which then able to run cucumber test in java. Here’s how:
First, create a Class that ends with Test in name. For example, CucumberTest.java. I noticed that if your class name doesn’t end with Test, maven doesn’t call that Class.
Next, add a @RunWith annotation in your class and put Cucumber.class inside. It defines how JUnit can run your class. Add a new annotation @CucumberOptions that holds the options or arguments that will be passed inside our runner. For example, here’s our CucumberTest class:
package com.javapointers; import cucumber.api.CucumberOptions; import cucumber.api.junit.Cucumber; import org.junit.runner.RunWith; @RunWith(Cucumber.class) @CucumberOptions( features = "src/test/resources", glue = "com.javapointers" ) public class CucumberTest { }
CucumberOptions interface contains different parameters that can be defined. The most basic are:
Then, run Maven test in your project either by command or by Maven Tab in your IDE:
You should see output in your IDE that it is running your features.
Earlier, I have mentioned that you can define tags to limit features or scenarios you want to run. Here’s an example on how to do that.
For example, I only want to run my Sign Up Feature and not my String Palindrome Feature. In order to do that, I will need to add a tag in my Sign Up Feature.
@SignUpFeature Feature: Sign Up Feature Scenario: Valid Registration Form Information Given User submits a valid registration form | John | Doe | jdoe | testPass1 | [email protected] | Then System proceeds with registration Scenario: Valid Registration Form Information with Header Given User submits a valid registration form header | FirstName | LastName | Username | Password | Email | | John | Doe | jdoe | testPass1 | [email protected] | Then System proceeds with registration Scenario: Valid Registration Form Information with Header and Multiple Rows Given User submits valid registration forms | FirstName | LastName | Username | Password | Email | | John | Doe | jdoe | testPass1 | [email protected] | | Anne | Smith | asmith | testPass2 | [email protected] | | Mike | Stewart | mstewart | testPass3 | [email protected] | Then All Forms will proceed with registration Scenario: Invalid Registration Forms Given User submits invalid registration forms | FirstName | LastName | Username | Password | Email | | | Doe | jdoe | testPass1 | [email protected] | | Anne | | asmith | testPass2 | [email protected] | | Mike | Stewart | mstewart | testPass3 | invalidEmail!@ | Then All Forms will be declined and not registered
Notice that I have added @SignUpFeature at the top of my feature. Next, we will define it in our CucumberTest class inside CucumberOptions annotation.
package com.javapointers; import cucumber.api.CucumberOptions; import cucumber.api.junit.Cucumber; import org.junit.runner.RunWith; @RunWith(Cucumber.class) @CucumberOptions( features = "src/test/resources", glue = "com.javapointers", tags = "@SignUpFeature" ) public class CucumberTest { }
Then we can run our Maven Test or Install that will only run Scenarios under Sign Up Feature.
Both of our examples doesn’t need any plugin or maven pom configurations. Just add the required dependency and you’re good to go. Here’s our complete pom.xml in case you need it:
4.0.0 com.javapointers cucumber-data-tables 1.0-SNAPSHOT io.cucumber cucumber-java 4.4.0 test io.cucumber cucumber-junit 4.4.0 test org.apache.commons commons-lang3 3.8.1 commons-validator commons-validator 1.6
That’s it. In summary, we just need to define a Runner Class that contains the Cucumber Options. You can download the source code using this link.