I have a Cucumber Java Project in Eclipse. When I try to run maven, either within eclipse or from the command line, no tests are run. But When I run the test in eclipse with Junit (right click -> run with Junit), it runs the test. I've researched many articles with this exact question, but none of the proposed solutions have fixed my problem. Here's a description of my project structure: The source files are located in src/main/java/linkedinlearning/cucumbercourse/*
The feature file is located in 2 different places (I'll explain why in a moment)
- src/test/java/linkedinlearning/cucumbercourse/MenuManagement.feature
- src/test/resources/features/MenuManagement.feature
The step definition file is located in: src/test/java/stepdefinitions/MenuManagementSteps.java
The test runner file is located in src/test/java/testrunners/MenuManagementTest.java
I put the feature file in 2 different places because originally it was only in src/test/java/linkedinlearning/cucumbercourse/ but then I read online that it should go in src/test/resources/features, so I added it there as well.
Here is my pom file:
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>linkedinlearning</groupId>
<artifactId>cucumbercourse</artifactId>
<version>0.0.1</version>
<packaging>jar</packaging>
<name>Cucumbercourse</name>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>13</java.version>
<java.home>/Library/Java/JavaVirtualMachines/jdk-13.0.1.jdk/Contents/Home</java.home>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.junit</groupId>
<artifactId>junit-bom</artifactId>
<version>5.8.2</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-bom</artifactId>
<version>7.2.3</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-java</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-junit-platform-engine</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-suite</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.10.0</version>
<configuration>
<encoding>UTF-8</encoding>
<source>${java.version}</source>
<target>${java.version}</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M5</version>
<configuration>
<properties>
<!-- Work around. Surefire does not include enough
information to disambiguate between different
examples and scenarios. -->
<configurationParameters>
cucumber.junit-platform.naming-strategy=long
</configurationParameters>
</properties>
<includes>
<include>MenuManagementTest.java</include>
</includes>
<testFailureIgnore>true</testFailureIgnore>
</configuration>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
<!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
<plugin>
<artifactId>maven-site-plugin</artifactId>
<version>3.7.1</version>
</plugin>
<plugin>
<artifactId>maven-project-info-reports-plugin</artifactId>
<version>3.0.0</version>
</plugin>
</plugins>
</build>
</project>
Here is my testrunner file MenuManagementTest.java
package testrunners;
import org.junit.runner.RunWith;
import io.cucumber.junit.Cucumber;
import io.cucumber.junit.CucumberOptions;
@RunWith(Cucumber.class)
@CucumberOptions(
features="src/test/resources/features",
glue= "stepdefinitions",
plugin= {"pretty"})
public class MenuManagementTest {
}
I set $JAVA_HOME to point to the correct JDK (/Library/Java/JavaVirtualMachines/jdk-13.0.1.jdk/Contents/Home). Here is the result of running "mvn clean test" from the command line
jeffmartin@pc-jmartin ~/eclipse-workspace/cucumbercourse % mvn clean test
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------< linkedinlearning:cucumbercourse >-------------------
[INFO] Building Cucumbercourse 0.0.1
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ cucumbercourse ---
[INFO] Deleting /Users/jeffmartin/eclipse-workspace/cucumbercourse/target
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ cucumbercourse ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory /Users/jeffmartin/eclipse-workspace/cucumbercourse/src/main/resources
[INFO]
[INFO] --- maven-compiler-plugin:3.10.0:compile (default-compile) @ cucumbercourse ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 2 source files to /Users/jeffmartin/eclipse-workspace/cucumbercourse/target/classes
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ cucumbercourse ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 1 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.10.0:testCompile (default-testCompile) @ cucumbercourse ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 3 source files to /Users/jeffmartin/eclipse-workspace/cucumbercourse/target/test-classes
[INFO]
[INFO] --- maven-surefire-plugin:3.0.0-M5:test (default-test) @ cucumbercourse ---
[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: 2.661 s
[INFO] Finished at: 2022-02-17T15:47:32-05:00
[INFO] ------------------------------------------------------------------------
Is there something obvious causing the test to be ignored?
Thanks
CodePudding user response:
You are using JUnit 5 which is relatively new compared to Maven Surefire (which has a glacial development pace). So older versions of Surefire do not know how to run JUnit 5 tests yet.
However there are more puzzles. You're also using cucumber-junit
which uses JUnit 4. But you don't have junit-vintage
to run it with JUnit 5.
To avoid more confusion I would suggest deleting everything and starting over with https://github.com/cucumber/cucumber-java-skeleton.
Maven only copies files from src/**/resources
to target/classes
and taret/test-classes
. The .java
files from src/**/java
are compiled first, other files are mostly ignored.
So .feature
files anywhere else are not copied and should be removed. When in doubt look at the contents of these folders.
I'd also suggest putting everything in one package and following the conventions for naming packages https://docs.oracle.com/javase/tutorial/java/package/namingpkgs.html
Note that the package is anything after src/**/java
and src/**/resources
. Again when in doubt look at the target/classes
and taret/test-classes
folder.
The source files are located in src/main/java/linkedinlearning/cucumbercourse/*
You're the 4th person I've seen using that course and get stuck.
Have a look at https://cucumber.io/docs/guides/10-minute-tutorial/