Run JAVA+Cucumber+JUnit Maven project in command line

24,003

Solution 1

Go to the path where your Pom.xml is situated. Then execute the command below.

Command to run maven, cucumber tests from command prompt.

mvn clean test -Dcucumber.options="path/to/feature-files --tags @Your_tag"

Solution 2

I have solved the problem. I remove the surefire plugin, update my project and run: After I have restructured the project:

-- Features file should be on /src/resource/feature. -- Java file should be on /src/test/java and should have the name Steps.java. $ mvn clean install $ mvn -Dtest=RunnerTest test

Thanks for all.

Solution 3

Please note that the below is deprecated. refer here

mvn clean test -Dcucumber.options="path/to/feature-files --tags @Your_tag"

The updated equivalent for above is

mvn clean test -Dcucumber.feature="path/to/feature-files" -Dcucumber.filter.tags="@Your_tag"
  • Note: The path/to/feature-files can also contain sub directories with .feature files.

Please refer here to explore more on CLI options

Share:
24,003
dsbonafe
Author by

dsbonafe

Updated on July 09, 2022

Comments

  • dsbonafe
    dsbonafe almost 2 years

    I am with a small problem here: run this project using MAVEN and Cucumber.

    I'm with the following structure in my MAVEN project:

    ```
    br.com.home.cucumberProjectWithWS
                    |--- src/tests/java
                    |                             |--- com.home.Model (secret)
                    |                             |--- com.home.Control (secret)
                    |                             |--- com.home.View
                                                   |                             |--- ... (secret)
                                                   |                             |--- Runner.java
                    |                             |--- com.home.javaStepsFolder
                    |                                                             |--- MyTestsSteps.java
                    |--- src/main/java
                    |--- src/main/resources
                    |--- src/tests/resources
                    |--- featuresFolder
                    |                             |--- FirstFeature.feature
                    |                             |--- SecondFeature.feature
                    |                             |--- ThirdFeature.feature
                    |--- pom.xml
    ```
    

    The Runner.java class is the following:

    ```
    package br.com.home.runner;
    
    import org.junit.runner.RunWith;
    
    import cucumber.api.CucumberOptions;
    import cucumber.api.junit.Cucumber;
    
    @RunWith(Cucumber.class)
    @CucumberOptions(monochrome = false, plugin = { "html:target/cucumber-html-report", "json:target/cucumber.json",
                                   "pretty:target/cucumber-pretty.txt", "usage:target/cucumber-usage.json",
                                   "junit:target/cucumber-results.xml" }, features = "featuresFolder", glue = { "br.com.home.javaStepsFolder" })
    public class Runner {
    
    }
    ```
    

    The class MyTestsSteps.java is something like the following:

    ```
    package br.com.home.runner;
    
    import cucumber.api.*;
    
    class MyTestsSteps{
    
                    Scenario scenario;
                    Controller controller = new Control();
    
                    @Before
                    public void doItBefore(Scenario scenario){
                                   this.scenario = scenario;
                    }
    
                    @When("^we do something$")
                    public void doSomething(){
                                   controller.doSomething();
                    }
    
                    @When("^we do something else$")
                    public void doSomethingElse(){
                                   controller.doSomethingElse();
                    }
    
                    @Then("^we expect \"([^\"]*)$")
                    public void weExpectSomeResult(String result){
                                   assertTrue(controller.getResultExpected().equals(result));
                    }
    }
    ```
    
    And my `pom.xml` is the following:
    
    ```
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
                    <modelVersion>4.0.0</modelVersion>
                    <groupId>com.home.cucumberProjectWithWS</groupId>
                    <artifactId>com.home.cucumberProjectWithWS</artifactId>
                    <version>0.0.1-SNAPSHOT</version>
    
                    <dependencies>
                                   <!-- CUCUMBER -->
    
                                   <!-- CUCUMBER: Java -->
    
                                   <dependency>
                                                   <groupId>info.cukes</groupId>
                                                   <artifactId>cucumber-java</artifactId>
                                                   <version>1.2.4</version>
                                                   <scope>test</scope>
                                   </dependency>
    
                                   <!-- CUCUMBER: Core -->
                                   <dependency>
                                                   <groupId>info.cukes</groupId>
                                                   <artifactId>cucumber-core</artifactId>
                                                   <version>1.2.4</version>
                                   </dependency>
    
                                   <!-- CUCUMBER: JUnit -->
                                   <dependency>
                                                   <groupId>info.cukes</groupId>
                                                   <artifactId>cucumber-junit</artifactId>
                                                   <version>1.2.4</version>
                                                   <scope>test</scope>
                                   </dependency>
    
                                   <!-- CUCUMBER: JVM Deps -->
                                   <dependency>
                                                   <groupId>info.cukes</groupId>
                                                   <artifactId>cucumber-jvm-deps</artifactId>
                                                   <version>1.0.5</version>
                                   </dependency>
    
                                   <!-- CUCUMBER: Reports -->
                                   <dependency>
                                                   <groupId>net.masterthought</groupId>
                                                   <artifactId>cucumber-reporting</artifactId>
                                                   <version>2.5.0</version>
                                   </dependency>
    
                                   <!-- CUCUMBER: Gherkin -->
                                   <dependency>
                                                   <groupId>info.cukes</groupId>
                                                   <artifactId>gherkin</artifactId>
                                                   <version>2.12.2</version>
                                   </dependency>
    
                                   <!-- MOCKITO: All -->
                                   <dependency>
                                                   <groupId>org.mockito</groupId>
                                                   <artifactId>mockito-all</artifactId>
                                                   <version>1.10.19</version>
                                   </dependency>
    
                                   <!-- JUNIT -->
                                   <dependency>
                                                   <groupId>junit</groupId>
                                                   <artifactId>junit</artifactId>
                                                   <version>4.11</version>
                                                   <scope>test</scope>
                                   </dependency>
                    </dependencies>
    
    
                    <build>
                                   <sourceDirectory>src/main/java</sourceDirectory>
                                   <plugins>
                                                   <plugin>
                                                                   <artifactId>maven-compiler-plugin</artifactId>
                                                                   <version>3.3</version>
                                                                   <configuration>
                                                                                   <source>1.8</source>
                                                                                   <target>1.8</target>
                                                                                   <encoding>UTF-8</encoding>
                                                                   </configuration>
                                                   </plugin>
    
                                                   <plugin>
                                                                   <groupId>org.apache.maven.plugins</groupId>
                                                                   <artifactId>maven-surefire-plugin</artifactId>
                                                                   <version>2.19.1</version>
                                                                   <configuration>
                                                                                   <properties>
                                                                                                  <property>
                                                                                                                  <name>junit</name>
                                                                                                                  <value>true</value>
                                                                                                  </property>
                                                                                   </properties>
                                                                                   <includes>
                                                                                                  <include>**/*Runner.java</include>
                                                                                   </includes>
                                                                   </configuration>
                                                   </plugin>
    
                                   </plugins>
                    </build>
    
    </project>
    ```
    

    I try to run:

    ```
    mvn clean test
    ```
    

    And it does not works.

    I want to run those tests using Maven and know if is possible set the sequence of execution of Cucumber Tests.

    I have tried to define in @CucumberOptions features parameter, but it did not work!

    ```
    features = "{featuresFolder/FirstFeature.feature, featuresFolder/SecondFeature.feature}"
    ```
    

    and

    ```
    features = {
                    "featuresFolder/FirstFeature.feature", 
                    "featuresFolder/SecondFeature.feature"
    }
    ```
    

    And tries to do (as recommended in other post here):

    ```
    <includes>
                    <exclude>**/*Runner.java</exclude>
    </includes>
    ```
    

    in pom.xml surefire-plugin configuration.

    But it did not work too.

    Someone could help me to run this project using MAVEN command line and Cucumber if possible.

    I am using Windows 8.1! And the project will run in Linux in future.

    Thanks.

  • A user
    A user about 7 years
    thank you @dsbonafe for accepting the answer. Hope your issue resolved.
  • Rohit Babu
    Rohit Babu almost 3 years
    NOTE: THE ABOVE -Dcucumber.options USAGE IS DEPRECATED. Please find the updated one here - stackoverflow.com/a/68437296/10804821