Tests not running through Maven? - java

When I run my test in Maven I get this:
[INFO] -------------------------------------------------------
[INFO] T E S T S
[INFO] -------------------------------------------------------
[INFO]
[INFO] Results:
[INFO]
[INFO] Tests run: 0, Failures: 0, Errors: 0, Skipped: 0
My test class, JsonReaderTest.class, is placed in src/test/java and follows the correct name convention as far as I know from maven-surefire-plugin.
Tests run fine when run outside of Maven.
I have this plugin included in my pom:
<!-- Executes tests -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.20.1</version>
</plugin>
and this in my dependencies:
<!-- Test -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.0.0</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.0.0</version>
</dependency>
and my test class:
package org.avalin.optaplanner.test.java;
import org.avalin.optaplanner.json.JsonReader;
import org.junit.jupiter.api.*;
import java.io.FileNotFoundException;
import java.nio.file.Paths;
import static org.junit.jupiter.api.Assertions.*;
public class JsonReaderTest
{
#Test
#DisplayName("Test: No such file at designated path")
void testloadFromJsonTest() throws Exception
{
Throwable exception = assertThrows(FileNotFoundException.class,
()-> JsonReader.loadFromJson(".json"));
assertEquals(".json (No such file or directory)",
exception.getMessage());
}
#Test
#DisplayName("Test: Load Shifts from JSON (String instead of number)")
void testLoadShiftsFromJson3()
{
Throwable exception = assertThrows(NumberFormatException.class, ()-> JsonReader.loadFromJson(Paths.get("src/main/resources/org/avalin/optaplanner/json/faultyShift-2.json").toAbsolutePath().toString()));
assertEquals("\nOne or more of your \"shift\" elements has a number format exception.\n" +
"Check for errors in your JSON-properties.\n" +
"(Did you insert a string instead of a number in id?)",
exception.getMessage());
}
#Test
#DisplayName("Test: JSON is correctly loaded")
void testJsonIsLoaded()
{
assertFalse(JsonReader.jsonIsLoaded());
}
#AfterEach
void cleanJsonReader()
{
JsonReader.cleanJsonReader();
}
}
When I tried googling this problem, it seemed the only thing that could be wrong would be naming convention (class had to end with or start with test, I tested both with no change) and that the test class should be put into the appropriate folder.
When I run: mvn -Dtest=JsonReaderTest test
I get following:
Failed to execute goal org.apache.maven.plugins:maven-surefire-
plugin:2.20.1:test (default-test) on project optaplanner: No tests were
executed!
The JsonReaderTest.class is also correctly generated inside target/test-classes
What could be the culprit here?

Using the Maven Surefire plugin and JUnit 5 together requires some tweaking ...
From the docs:
The JUnit team has developed a very basic provider for Maven Surefire that lets you run JUnit 4 and JUnit Jupiter tests via mvn test. The pom.xml file in the junit5-maven-consumer project demonstrates how to use it and can serve as a starting point.
Due to a memory leak in Surefire 2.20, the junit-platform-surefire-provider currently only works with Surefire 2.19.1.
...
<build>
<plugins>
...
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19</version>
<dependencies>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-surefire-provider</artifactId>
<version>1.0.0</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
...

This plugin worked for me:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M3</version>
<dependencies>
<dependency>
<groupId>org.apache.maven.surefire</groupId>
<artifactId>surefire-junit47</artifactId>
<version>3.0.0-M3</version>
</dependency>
</dependencies>
</plugin>
Taken from https://maven.apache.org/surefire/maven-surefire-plugin/examples/junit.html

the following pom configuration worked for me:
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.4.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.4.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-launcher</artifactId>
<version>1.4.0</version>
<scope>test</scope>
</dependency>
....
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19</version>
<dependencies>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-surefire-provider</artifactId>
<version>1.0.0</version>
</dependency>
</dependencies>
</plugin>
...
the plugin part as #glytching stated above

I was having the same problem. All the test classes were package private and JUnit 5 couldn't see them. The solution was to add this dependency to the pom file:
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.3.2</version>
<scope>test</scope>
</dependency>

I had the same issue, it took me days to find the solution!
First, make sure you end your class-name with ***Test.java, for example: MyGreatTest.java
Secondly, make your class and methods public! This is very important, otherwise mvn test will ignore your test class!
Thirdly, I added this peace of code in the pom.xml:
<build>
<plugins>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.0</version>
</plugin>
</plugins>
</build>

along with all junit dependencies like engine, api, launcher this one integrated all I guess
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-runner</artifactId>
<version>1.9.0-M1</version>
<scope>test</scope>
with latest version of surefire plugin
and it works solely as well for me without other junite dependencies

Related

Filter JUnit 5 test cases with the annotation #Tag("name_test") using Maven

I want to filter my Junit 5 test cases, I am using the annotation #Tag("type_test"). I run my test with maven with the command mvn -Dtests=a test but it runs all the cases. For example, I have two methods and I want run only the method with #Tag("a"), but the result in console is "Hello word 1" and "Hello word 2". See the example code.
static Properties properties = null;
#BeforeAll
public static void setUp() throws Throwable {
properties = CommonMethods.loadConfigPropertiesFile();
RestAssured.baseURI = properties.getProperty("BASE_HOST");
}
#Test
#Tag("a")
public void test1() {
System.out.println("hello word 1");
}
#Test
#Tag("b")
public void test2() {
System.out.println("hello word 2");
}
}
pom.xml
<build>
<plugins>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19</version>
<configuration>
<properties>
<includeTags>${tests}</includeTags>
</properties>
</configuration>
<dependencies>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-surefire-provider</artifactId>
<version>1.0.0-M2</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.0.0-M2</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.0.0-M2</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.0.0-M2</version>
</dependency>
</dependencies>
The versions and libraries you're using are outdated. Please retry with:
Maven Surefire 2.22.1 (better: 3.0.0-M3)
JUnit Jupiter 5.3.2 (better: 5.4.0-M1)
See this sample pom.xml file that also covers how to filter tags:
<build>
<plugins>
<!-- JUnit 5 requires Surefire version 2.22.1 or higher -->
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
<configuration>
<groups>a</groups>
<!-- excludedGroups>slow</excludedGroups -->
</configuration>
</plugin>
</plugins>
</build>
Source: https://github.com/junit-team/junit5-samples/blob/master/junit5-migration-maven/pom.xml
For more details on how to configure the JUnit Platform with Maven see the JUnit 5 User Guide https://junit.org/junit5/docs/current/user-guide/#running-tests-build-maven or the Maven Surefire documentation: https://maven.apache.org/surefire/maven-surefire-plugin/examples/junit-platform.html
No need to touch your POM file.
mvn surefire:test -D groups=a,b should also do the trick.
If you need to compile before running the tests, use:
mvn test -D groups=a,b
Note that the space put between -D and groups=a,b is optional and purely for cosmetic reasons.
I found a solution. It important verify and try with the latest version of each dependency. In this example:
maven-surefire-plugin (3.0.0-M3)
junit-platform-surefire-provider (1.3.0-M1)
junit-jupiter-engine (5.4.0-M1)
junit-jupiter-api (5.4.0-M1)
Solutions
Without profiles:
pom.xml
<build>
<plugins>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M3</version>
<configuration>
<properties>
<includeTags>${tests}</includeTags>
</properties>
</configuration>
<dependencies>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-surefire-provider</artifactId>
<version>1.3.0-M1</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.4.0-M1</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.4.0-M1</version>
</dependency>
</dependencies>
And you can use the command mvn test -Dtests=a to execute only the methods with the annotation #Tag("a")
With profiles:
Add this example code in the pom.xml
<profiles>
<profile>
<id>serverdevelop</id>
<properties>
<tests>develop</tests>
</properties>
</profile>
<profile>
<id>servertesting</id>
<properties>
<tests>testing</tests>
</properties>
</profile>
<profile>
<id>serverproduction</id>
<properties>
<tests>production</tests>
</properties>
</profile>
</profiles>
And for example you can use the command mvn test -Pserverdevelop to execute only the methods with the annotation #Tag("develop"). This is very useful for separating test cases by environments.

How do I run JUnit 5 integration tests with the Maven Failsafe plugin?

The Maven Failsafe plugin won't find my JUnit 5 integration tests when I'm running the command mvn clean failsafe:integration-test, although it can find the files.
I have the junit-jupiter-api and junit-jupiter-engine as test dependencies:
<properties>
<junit.jupiter.version>5.0.1</junit.jupiter.version>
</properties>
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>${junit.jupiter.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>${junit.jupiter.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
My integration tests are named correctly (following the **/*IT.java, **/IT*.java, or the **/*ITCase.java that included by default by Failsafe and excluded by default by Surefire).
Is there any way that I can use JUnit 5 tests with Failsafe?
Edit: This answer was correct before maven-failsafe-plugin:2.22.0. See davidxxx's answer for the ideal and most up to date solution.
The maven-failsafe-plugin currently doesn't support JUnit 5, out of the box.
However, like with maven-surefire-plugin, you can run JUnit 5 tests with the maven-failsafe-plugin by specifying the dependency on the org.junit.platform:junit-platform-surefire-provider:1.0.1 with the earlier version of the maven-failsafe-plugin:2.19.1.
It doesn't work with the current version 2.20 of the failsafe (in the same way that the surefire has the error) due to an OutOfMemory error.
See the below for an example of the configuration of the plugin:
<properties>
<junit.platform.version>1.0.1</junit.platform.version>
</properties>
<plugin>
<artifactId>maven-failsafe-plugin</artifactId>
<version>2.19.1</version>
<dependencies>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-surefire-provider</artifactId>
<version>${junit.platform.version}</version>
</dependency>
</dependencies>
</plugin>
You can find a full example of this working (and a failing one) on GitHub. To test that it works, you can run mvn clean failsafe:integration-test.
Note that from the JUnit 5 documentation : junit-platform-surefire-provider should be not used any longer :
Due to the release of Surefire 2.22.0, the
junit-platform-surefire-provider from the JUnit team has been
deprecated and will be discontinued in a subsequent release of the
JUnit Platform.
Additionally, you can also read in the maven-surefire-plugin documentation :
Using JUnit 5 Platform
To get started with JUnit Platform, you need to add at least a single
TestEngine implementation to your project. For example, if you want to
write tests with Jupiter, add the test artifact junit-jupiter-engine
to the dependencies in POM
So you have to specify this test dependency :
<properties>
<junit-jupiter.version>5.2.0</junit-jupiter.version>
</properties>
<dependencies>
[...]
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>${junit-jupiter.version}</version>
<scope>test</scope>
</dependency>
[...]
</dependencies>
And the maven-failsafe-plugin declaration could be as simple as :
<build>
<plugins>
<plugin>
<artifactId>maven-failsafe-plugin</artifactId>
<version>2.22.0</version>
</plugin>
</plugins>
</build>

Unable to execute Junit5 tests using Maven [duplicate]

This question already has answers here:
Surefire is not picking up Junit 5 tests
(19 answers)
Closed 6 years ago.
Maven execution
mvn clean test
I am trying to use junit5 for one of my maven projects but not able to execute the unit tests during the test phase using -
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.0.0-M3</version>
</dependency>
The output that I get is -
[INFO] --- maven-surefire-plugin:2.19.1:test (default-test) # utils ---
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Results :
Tests run: 0, Failures: 0, Errors: 0, Skipped: 0
Tried implementing the solution mentioned # Surefire is not picking up Junit 5 tests to update the dependencies to -
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.0.0-M3</version>
</dependency>
<dependency>
<groupId>org.junit</groupId>
<artifactId>junit5-api</artifactId>
<version>5.0.0-ALPHA</version><!--https://mvnrepository.com/artifact/org.junit/junit5-api -->
<scope>test</scope>
</dependency>
and updating the plugin to -
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19.1</version>
<dependencies>
<dependency>
<groupId>org.junit</groupId>
<artifactId>surefire-junit5</artifactId>
<version>5.0.0-ALPHA</version><!--couldn't find this on the central though-->
</dependency>
</dependencies>
</plugin>
but the output remains same.
Question - Is this custom provider no more supported or is there any solution to executing the tests using maven, junit5 and/or junit5-api currently?
Note - The test execution was working fine with JUnit-4.
You should configure the maven-surefire-plugin like this:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19.1</version>
<dependencies>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-surefire-provider</artifactId>
<version>1.0.0-M3</version>
</dependency>
</dependencies>
</plugin>
You only need to include the junit-jupiter-api artifact and only in test scope in your dependencies section:
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.0.0-M3</version>
<scope>test</scope>
</dependency>
Please see http://junit.org/junit5/docs/current/user-guide/#running-tests-build-maven for more information.
Edit - From the same docs.
In order to have Maven Surefire run any tests at all, a TestEngine
implementation must be added to the runtime classpath.
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19</version>
<dependencies>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-surefire-provider</artifactId>
<version>1.0.0-M3</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.0.0-M3</version>
</dependency>
</dependencies>
</plugin>

Cannot create Launcher without at least one TestEngine; consider adding an engine implementation JAR to the classpath in Junit 5

I got following execption when i tried to run test case in junit5:
Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.19:test (default-test) on project CRUD-App: Execution default-test of goal org.apache.maven.plugins:maven-surefire-plugin:2.19:test failed: There was an error in the forked process
org.junit.platform.commons.util.PreconditionViolationException: Cannot create Launcher without at least one TestEngine; consider adding an engine implementation JAR to the classpath
at org.junit.platform.commons.util.Preconditions.condition(Preconditions.java:161)
at org.junit.platform.launcher.core.DefaultLauncher.<init>(DefaultLauncher.java:52)
at org.junit.platform.launcher.core.LauncherFactory.create(LauncherFactory.java:42)
at org.junit.platform.surefire.provider.JUnitPlatformProvider.invoke(JUnitPlatformProvider.java:59)
at org.apache.maven.surefire.booter.ForkedBooter.invokeProviderInSameClassLoader(ForkedBooter.java:286)
at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:240)
at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:121)
pom.xml
<dependencies>
...
<dependency>
<groupId>org.junit</groupId>
<artifactId>junit5-api</artifactId>
<version>5.0.0-SNAPSHOT</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19</version>
<dependencies>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-surefire-provider</artifactId>
<version>1.0.0-M2</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
Test Class:
public class PersonServiceTest {
final Database database = Database.from("jdbc:h2:mem:" + App.DB_NAME);
final PersonService personService = new PersonService(database);
public PersonServiceTest() {
}
#Test
#DisplayName("#Person#insert()")
public void testInsert() {
personService.insert(new PersonBuilder()
.setId(1).setName("Bhuwan")
.setAddress("KTM")
.setContactNo("984849").createPerson()
);
}
}
Maven Goal: mvn test
Mixing ALPHA snapshot artifacts (i.e., org.junit:junit5-api:5.0.0-SNAPSHOT) with M2 artifacts (i.e., org.junit.platform:junit-platform-surefire-provider:1.0.0-M2), won't work.
The Maven section in the user guide suggests to check out the pom.xml from the junit5-maven-consumer project. If you follow that example, you will end up with something like the following.
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
<junit.jupiter.version>5.0.0-M2</junit.jupiter.version>
<junit.platform.version>1.0.0-M2</junit.platform.version>
</properties>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19</version>
<dependencies>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-surefire-provider</artifactId>
<version>${junit.platform.version}</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>${junit.jupiter.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>${junit.jupiter.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
To write your tests, you only need the junit-jupiter-api; however, in order to run your tests you must have a TestEngine on the classpath. For JUnit Jupiter you therefore need junit-jupiter-engine on the classpath as well.
As Nicolai Parlog pointed out, you could add junit-jupiter-engine as a dependency for the maven-surefire-plugin; however, that would not include the JupiterTestEngine in the classpath for your IDE.
If you're only running tests via Maven or with a recent beta version of IntelliJ 2016 (which has built-in support for JUnit 5), then you may not care if JupiterTestEngine is on the classpath in your IDE. But... if you're using Eclipse, NetBeans, or a non-beta version of IntelliJ, you'll definitely want the JupiterTestEngine on the classpath in the IDE as well.
Regards,
Sam (core JUnit 5 committer)
Ended up here since getting the same error using Gradle and JUnit 4. The solution in this case can be found in the docs:
The JUnit Platform can run JUnit 4 based tests as long as you
configure a testImplementation dependency on JUnit 4 and a
testRuntimeOnly dependency on the JUnit Vintage TestEngine
implementation similar to the following.
A test engine is also needed:
dependencies {
testImplementation("junit:junit:4.13.2")
testRuntimeOnly("org.junit.vintage:junit-vintage-engine:5.8.2")
}
The Maven Surefire plugin not only needs the JUnit 5 provider but also a TestEngine implementation to run tests with. To quote the JUnit 5 docs:
In order to have Maven Surefire run any tests at all, a TestEngine
implementation must be added to the runtime classpath.
In accordance with that the following works:
<build>
<plugins>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19</version>
<dependencies>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-surefire-provider</artifactId>
<version>1.0.0-M4</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.0.0-M4</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.0.0-M4</version>
<scope>test</scope>
</dependency>
</dependencies>
Note that this configuration makes the engine a dependency of the surefire plugin, not of your test code.
simply adding below dependency solved this prob.
testImplementation 'org.junit.jupiter:junit-jupiter-engine:5.8.2'
I solve it changing
classpath "org.springframework.boot:spring-boot-gradle-plugin:2.1.8.RELEASE"
to
classpath "org.springframework.boot:spring-boot-gradle-plugin:2.2.0.RELEASE"

Maven Surefire plugin did not rerun failed Cucumber tests

I am using the Java implementation of Selenium WebDriver (version 2.53.0) to run some automated tests against a web application. The tests are written in Behaviour Driven Testing format using the Java implementation of Cucumber (version 1.2.3). I use Maven (version 3.3.9) to import all my dependencies and also to build and run the tests. The tests are organised into different categories using Cucumber tags. For example, I can run one category of tests tagged with #JohnnyBravo from the command line using the following commands:
cd path_to_Maven_POM_file
mvn clean test -Dcucumber.options="--tags #JohnnyBravo"
After doing some research, I found out that you can use the Maven SureFire plugin to rerun failed tests by adding "-Dsurefire.rerunFailingTestsCount=2" to the Maven command above according to this link. I then tried to rerun that category of tests using the command below while ensuring that some of them will definitely fail so as to see if they will be rerun or not:
mvn clean test -Dcucumber.options="--tags #JohnnyBravo" -Dsurefire.rerunFailingTestsCount=2
Unfortunately, the failed tests did not rerun. What am I doing wrong here ?
My POM file is shown below:
<?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>
<groupId>com.company</groupId>
<artifactId>regression-test</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<cucumber.version>1.2.3</cucumber.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>2.8.2</version>
</dependency>
<dependency>
<groupId>net.sourceforge.jtds</groupId>
<artifactId>jtds</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-picocontainer</artifactId>
<version>${cucumber.version}</version>
</dependency>
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-java</artifactId>
<version>${cucumber.version}</version>
</dependency>
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-junit</artifactId>
<version>${cucumber.version}</version>
</dependency>
<dependency>
<groupId>com.vimalselvam</groupId>
<artifactId>cucumber-extentsreport</artifactId>
<version>1.1.0</version>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>2.53.0</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
<dependency>
<groupId>com.pojosontheweb</groupId>
<artifactId>monte-repack</artifactId>
<version>1.0.1</version>
</dependency>
</dependencies>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.18.1</version>
</plugin>
</plugins>
</pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.18.1</version>
<configuration>
<systemPropertyVariables>
<webdriver.chrome.driver>src/test/resources/chromedriver/chromedriver.exe</webdriver.chrome.driver>
</systemPropertyVariables>
<!--<testFailureIgnore>true</testFailureIgnore>-->
</configuration>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
The Cucumber JUnit test runner class is shown below:
import com.cucumber.listener.ExtentCucumberFormatter;
import cucumber.api.CucumberOptions;
import cucumber.api.junit.Cucumber;
import org.junit.BeforeClass;
import org.junit.runner.RunWith;
import java.io.File;
import java.util.HashMap;
#RunWith(Cucumber.class)
#CucumberOptions(
monochrome = false,
plugin = {
"pretty",
"html:C:/Test_Output_Data/Results/HTML/",
"json:C:/Test_Output_Data/Results/results.json",
"com.cucumber.listener.ExtentCucumberFormatter"
},
features = {"src/test/resources/"},
tags = {
"#JohnnyBravo",
//"#AgentUI", "#Smoke",
//"#GenUI", "#Smoke",
//"#GenUI", "#Regression",
}
)
public class GeneralRunnerTest {
#BeforeClass
public static void setup() {
ExtentCucumberFormatter.initiateExtentCucumberFormatter();
ExtentCucumberFormatter.loadConfig(new File("src/test/resources/extent-config.xml"));
ExtentCucumberFormatter.addSystemInfo("Browser Name", "Firefox");
ExtentCucumberFormatter.addSystemInfo("Browser version", "46.0.1");
ExtentCucumberFormatter.addSystemInfo("Selenium version", "2.53.0");
HashMap<String, String> systemInfo = new HashMap<>();
systemInfo.put("Cucumber Version", "1.2.3");
systemInfo.put("Extent Cucumber Reporter Version", "1.1.0");
ExtentCucumberFormatter.addSystemInfo(systemInfo);
}
}
Check if you are using the right version for your cucumber dependencies.
On the page for this feature on the surefire website, it says
Since of 2.21.0 the provider surefire-junit47 can rerun scenarios created by cucumber-jvm 2.0.0 and higher.
Also check if you're using the same junit provider that they've mentioned. I'm not sure which is the default provider.

Categories