How to run Cucumber tests with 'mvn test'

10,630

I have found the issue.

I've had to do mvn clean test in order to re-generate the compiled tests so that maven can find them with the *Test.class regex.

the most important attribute is the <testSourceDirectory>${project.basedir}/src/test/kotlin</testSourceDirectory> inside the <build> section to tell Maven to look into this directory in order to find the tests to execute.

Here's my two cleaned-up poms :

parent-pom.xml

    <properties>
        <kotlin.version>1.3.40</kotlin.version>
        <junit-jupiter.version>5.4.2</junit-jupiter.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>com.fasterxml.jackson.module</groupId>
            <artifactId>jackson-module-kotlin</artifactId>
        </dependency>
        <dependency>
            <groupId>org.jetbrains.kotlin</groupId>
            <artifactId>kotlin-reflect</artifactId>
            <version>${kotlin.version}</version>
        </dependency>
        <dependency>
            <groupId>org.jetbrains.kotlin</groupId>
            <artifactId>kotlin-stdlib-jdk8</artifactId>
            <version>${kotlin.version}</version>
        </dependency>

        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter</artifactId>
            <version>${junit-jupiter.version}</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.junit.vintage</groupId>
            <artifactId>junit-vintage-engine</artifactId>
            <version>${junit-jupiter.version}</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <sourceDirectory>${project.basedir}/src/main/kotlin</sourceDirectory>
        <testSourceDirectory>${project.basedir}/src/test/kotlin</testSourceDirectory>
        <plugins>
            <plugin>
                <groupId>org.jetbrains.kotlin</groupId>
                <artifactId>kotlin-maven-plugin</artifactId>
                <version>${kotlin.version}</version>
            </plugin>

            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <skip>true</skip>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

domain-pom.xml

    <properties>
        <cucumber.version>4.4.0</cucumber.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>io.cucumber</groupId>
            <artifactId>cucumber-java8</artifactId>
            <version>${cucumber.version}</version>
        </dependency>

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

logs :

[INFO] -------------------------------------------------------
[INFO]  T E S T S
[INFO] -------------------------------------------------------
[INFO] Running RunCukesTest
juin 25, 2019 12:01:59 AM cucumber.runtime.java.ObjectFactoryLoader loadSingleObjectFactory
AVERTISSEMENT: Use deprecated reflections to load ObjectFactory.
Feature: Is it friday yet

  Scenario: my scenario                # features/some-feature.feature:3
    Given today is Sunday              # StepDefs.today_is_Sunday()
    When I ask whether it's Friday yet # StepDefs.i_ask_whether_it_s_Friday_yet()
    Then I should be told "Nope"       # StepDefs.i_should_be_told(String)

1 Scenarios (1 passed)
3 Steps (3 passed)
0m0,245s

[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.251 s - in RunCukesTest
Share:
10,630

Related videos on Youtube

samasoulé
Author by

samasoulé

Updated on May 25, 2022

Comments

  • samasoulé
    samasoulé almost 2 years

    Note that I'm using Kotlin.
    Also note that I have a multi-modules application.

    I succeed in running Cucumber tests from the IDE by running the RunCukesTest.kt class.

    My problem is that when I run the command mvn test, The RunCukesTest.kt doesn't seems to run, and none .feature is tested.

    I'm not sure if I need to configure the maven-surefire-plugin in order to make it work.

    domain-pom.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <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">
        <parent>
            <artifactId>smart-notes</artifactId>
            <groupId>xxx</groupId>
            <version>0.0.1-SNAPSHOT</version>
        </parent>
        <modelVersion>4.0.0</modelVersion>
    
        <artifactId>domain</artifactId>
    
        <properties>
            <cucumber.version>4.4.0</cucumber.version>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
            <junit.version>4.12</junit.version>
        </properties>
    
        <dependencies>
            <dependency>
                <groupId>io.cucumber</groupId>
                <artifactId>cucumber-java8</artifactId>
                <version>${cucumber.version}</version>
            </dependency>
    
            <dependency>
                <groupId>io.cucumber</groupId>
                <artifactId>cucumber-junit</artifactId>
                <version>${cucumber.version}</version>
                <scope>test</scope>
            </dependency>
    
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>${junit.version}</version>
                <scope>test</scope>
            </dependency>
        </dependencies>
    </project>
    

    parent-pom.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <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>
        <packaging>pom</packaging>
        <modules>
            <module>domain</module>
            <module>infra</module>
        </modules>
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.1.6.RELEASE</version>
            <relativePath/> <!-- lookup parent from repository -->
        </parent>
        <groupId>xxx</groupId>
        <artifactId>smart-notes</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <name>smart-notes</name>
        <description>Take quick smart notes</description>
    
        <properties>
            <java.version>1.8</java.version>
            <kotlin.version>1.3.40</kotlin.version>
            <junit-jupiter.version>5.4.2</junit-jupiter.version>
        </properties>
    
        <dependencies>
            <dependency>
                <groupId>com.fasterxml.jackson.module</groupId>
                <artifactId>jackson-module-kotlin</artifactId>
            </dependency>
            <dependency>
                <groupId>org.jetbrains.kotlin</groupId>
                <artifactId>kotlin-reflect</artifactId>
                <version>${kotlin.version}</version>
            </dependency>
            <dependency>
                <groupId>org.jetbrains.kotlin</groupId>
                <artifactId>kotlin-stdlib-jdk8</artifactId>
                <version>${kotlin.version}</version>
            </dependency>
    
            <dependency>
                <groupId>org.junit.jupiter</groupId>
                <artifactId>junit-jupiter</artifactId>
                <version>${junit-jupiter.version}</version>
                <scope>test</scope>
            </dependency>
        </dependencies>
    
        <build>
            <sourceDirectory>${project.basedir}/src/main/kotlin</sourceDirectory>
            <testSourceDirectory>${project.basedir}/src/test/kotlin</testSourceDirectory>
            <plugins>
                <plugin>
                    <groupId>org.jetbrains.kotlin</groupId>
                    <artifactId>kotlin-maven-plugin</artifactId>
                    <version>${kotlin.version}</version>
    
                    <configuration>
                        <jvmTarget>1.8</jvmTarget>
                        <args>
                            <arg>-Xjsr305=strict</arg>
                        </args>
                        <compilerPlugins>
                            <plugin>spring</plugin>
                        </compilerPlugins>
                    </configuration>
    
                    <dependencies>
                        <dependency>
                            <groupId>org.jetbrains.kotlin</groupId>
                            <artifactId>kotlin-maven-allopen</artifactId>
                            <version>${kotlin.version}</version>
                        </dependency>
                    </dependencies>
                </plugin>
    
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                    <configuration>
                        <skip>true</skip>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    </project>
    

    RunCukesTest.kt

    import cucumber.api.CucumberOptions
    import cucumber.api.junit.Cucumber
    import org.junit.runner.RunWith
    
    @RunWith(Cucumber::class)
    @CucumberOptions(
            plugin = ["pretty"],
            features = ["classpath:features"],
            glue = ["classpath:acceptance.stepdefs"])
    class RunCukesTest
    
    

    some-feature.feature

    Feature: Is it friday yet
    
      Scenario: my scenario
        Given today is Sunday
        When I ask whether it's Friday yet
        Then I should be told "Nope"
    

    Maven build logs

    [INFO] 
    [INFO] --- maven-surefire-plugin:2.22.2:test (default-test) @ domain ---
    [INFO] 
    [INFO] -------------------------------------------------------
    [INFO]  T E S T S
    [INFO] -------------------------------------------------------
    [INFO] 
    [INFO] Results:
    [INFO] 
    [INFO] Tests run: 0, Failures: 0, Errors: 0, Skipped: 0
    [INFO] 
    [INFO] ------------------------------------------------------------------------
    [INFO] BUILD SUCCESS
    [INFO] ------------------------------------------------------------------------
    [INFO] Total time:  9.209 s
    [INFO] Finished at: 2019-06-23T16:46:35+02:00
    [INFO] ------------------------------------------------------------------------
    

    Architecture

    • src

      • test

        • kotlin

          • acceptance
            • RunCukesTest.kt
            • stepdefs
              • StepDefsTest.kt
        • resources

          • features
            • some-feature.feature

    Thank you for your time :)

    • Grasshopper
      Grasshopper almost 5 years
      Refer to this guide - cucumber.io/docs/guides/10-minute-tutorial. Switch to the kotlin tab at the top
    • samasoulé
      samasoulé almost 5 years
      Thank you, I've had already followed this tutorial, even after adding the kotlin-maven-plugin and maven-compiler-plugin + configurations, it still fails. I have done 'mvn clean test' (in order to regenerate compiled classes) and it works perfectly.