Maven runs JUnit 5 tests but does not run TestFactory method - maven

I have a number of tests in a project which I am able to run using Maven (mvn test), all of these are JUnit 5 using the org.junit.jupiter.api.Test annotation.
I have one other class which has a method using the org.junit.jupiter.api.TestFactory annotation. When I run the tests in IDEA all the tests run and the TestFactory is run as expected (it is used as a factory to generate a number of tests, each of which runs and then the results are produced).
When I run in Maven it says it is running the class but it doesn't run any methods within it and the test factory method code isn't run.
Here's some example code showing the import and how it is being used on the method.
import org.junit.jupiter.api.TestFactory;
...class definition here etc...
#TestFactory
public Collection<DynamicTest> runTests() throws Exception {
System.out.println("[Test] TestFactory test running");
... other code here...
}
Maven will say it is running the class but the printout above will not occur.
I've tried adding another empty test method which uses the #Test annotation but this also doesn't run.
If I have maven specifically run this test using -Dtest=xxx Maven will even print out that it is running the test but then show that no tests have been run:
Running tests.FactoryTest
Tests run: 0, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.001 sec
Update: changing the name of a method to start with 'test' means that Maven does run the method, and if I change that method to start with something else (e.g. 'run') maven does not run it. This is also a difference between IDEA and Maven it seems (IDEA will run whatever is marked as a #Test but Maven seems to require some sort of naming convention). But that said, altering the name of the #TestFactory method still doesn't get Maven to run it.
Is there a simple way to get maven to handle the depencies etc when running the tests but just use the same mechanism as IDEA or just use a more standard JUnit way to run the tests rather than applying its own interpretations?
Pom file as it relates to JUnit:
<properties>
<junit-platform.version>5.9.1</junit-platform.version>
</properties>
...
<dependency>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M7</version>
<type>maven-plugin</type>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.9.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<version>5.9.1</version>
<scope>test</scope>
</dependency>
mvn -v output:
Apache Maven 3.8.5 (3599d3414f046de2324203b78ddcf9b5e4388aa0)
Maven home: /usr/local/Cellar/maven/3.8.5/libexec
Java version: 1.8.0_161, vendor: Oracle Corporation, runtime: /Library/Java/JavaVirtualMachines/jdk1.8.0_161.jdk/Contents/Home/jre
Default locale: en_GB, platform encoding: UTF-8
OS name: "mac os x", version: "10.16", arch: "x86_64", family: "mac"

You have not configured Maven Surefire correctly.
Thus, I assume that your tests are "running" using POJO Tests.
See the official JUnit 5 - Maven sample application for a working example with JUnit Jupiter and Maven Surefire.

Related

Spring Boot 2.5+ with JDK 17 - maven surefire doesn't execute existing tests

When changing the Spring Boot 2.2 application to 2.5.5 with JDK 17, the Surefire test plugin does not start any existing tests. This is the message:
[INFO] T E S T S
[INFO] -------------------------------------------------------
[INFO]
[INFO] Results:
[INFO]
[INFO] Tests run: 0, Failures: 0, Errors: 0, Skipped: 0
Why does it not start any existing tests? The names of the tests are *Test.java.
I saw similar behavior when explicitly adding the Surefire plugin with a version higher then 2.19. Spring boot test starter will have a newer surefire plugin.
Maven version is 3.6.3.
On the path is jdk17.1.0.
<java.version>17</java.version>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
I looked at similar questions and saw that the path of the test folder should be below 'src'. Yes, it is.
In the folder it is the same:
Below both folders is a 'java' folder.
The Surefire is not explicitly in the pom.xml because it is in the spring-boot-test starter.
what kind of test exist in you project ? integration test or/and unit test? Can you give more detail on your context?
Anyway, from version 2.4.0 of spring-boot, JUnit 5’s Vintage Engine Removed from spring-boot-starter-test.
It is said in the 2.4.0 release note page https://github.com/spring-projects/spring-boot/wiki/Spring-Boot-2.4-Release-Notes :
If you upgrade to Spring Boot 2.4 and see test compilation errors for JUnit
classes such as org.junit.Test, this may be because JUnit 5’s vintage engine has been
removed from spring-boot-starter-test. The vintage engine allows tests written with
JUnit 4 to be run by JUnit 5. If you do not want to migrate your tests to JUnit 5 and
wish to continue using JUnit 4, add a dependency on the Vintage Engine, as shown in
the following example for Maven:
<dependency>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-core</artifactId>
</exclusion>
</exclusions>
</dependency>
Other situations what you will see no tests run:
Main and test are not in the same folder
Test file names do not end in *Test.java
There is an older surefire plugin, like 2.14.x. Just use a newer version, like
org.apache.maven.plugins
maven-surefire-plugin
3.0.0-M5

Spring Boot 2.3.1, JUnit 5, Maven 3.6.3 - Maven lifecycle "test" does not run test suite

pom.xml:
...
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-runner</artifactId>
<version>1.7.0-M1</version>
<scope>test</scope>
</dependency>
...
In <build> only is used:
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
Not in use in <build> are the plugins maven-surefire-plugin and maven-compiler-plugin
There are a few test classes (MyTest1Test, MyTest2Test, etc.), testing REST controllers. Some test methods are tagged with "myTag", to be included in a test suite, like:
#SpringBootTest
#AutoConfigureMockMvc
public class MyTest1Test {
...
#Test
#WithMockUser
public void test1() {...}
#Test
#WithMockUser
#Tag("myTag")
public void test2() {...}
...
}
Also there is a test suite, defined like:
#RunWith(JUnitPlatform.class)
#SelectClasses({MyTest1Test.class, MyTest3Test.class})
#IncludeTags({"myTag"})
public class MyTestSuiteTest {
...
}
When I select Maven > test, all test classes are run, only the test suite is not run. Why not? What kind of configuration has to be done?
When I run the test suite itself by right-click > Run 'MyTestSuiteTest' within the test class, it is run correctly.
Trying replacing #RunWith(JUnitPlatform.class with #ExtendWith(SpringExtension.class) doesn't solve it. Maven > "test" doesn't run the test suite, and worth, trying to run the test suite with IDE (right-click on the class > Run 'MyTestSuiteTest") shows and error:
org.junit.runners.model.InvalidTestClassError: Invalid test class '...MyTestSuiteTest':
1. No runnable methods
at org.junit.runners.ParentRunner.validate(ParentRunner.java:525)
at org.junit.runners.ParentRunner.<init>(ParentRunner.java:102)
at org.junit.runners.BlockJUnit4ClassRunner.<init>(BlockJUnit4ClassRunner.java:84)
at org.junit.runners.JUnit4.<init>(JUnit4.java:23)
at org.junit.internal.builders.JUnit4Builder.runnerForClass(JUnit4Builder.java:10)
at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:70)
at org.junit.internal.builders.AllDefaultPossibilitiesBuilder.runnerForClass(AllDefaultPossibilitiesBuilder.java:37)
at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:70)
at org.junit.internal.requests.ClassRequest.createRunner(ClassRequest.java:28)
at org.junit.internal.requests.MemoizingRequest.getRunner(MemoizingRequest.java:19)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:49)
at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:33)
at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:230)
at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:58)
The IDE tries to run the test suite with JUnit 4, but why? Because of #SelectClasses, #IncludeTags? I have to include the dependency
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-runner</artifactId>
<version>1.7.0-M1</version>
<scope>test</scope>
</dependency>
to be able to use #SelectClasses and #IncludeTags, but also I excluded the vintage engine. Btw, do I have to remove the exclusion of vintage engine, if running a test suite with #RunWith?
Observation:
Including vintage engine:
Using #RunWith, Maven > "test", shows the following in the console output:
org.junit.vintage.engine.discovery.DefensiveAllDefaultPossibilitiesBuilder$DefensiveAnnotatedBuilder buildRunner
WARNING: Ignoring test class using JUnitPlatform runner: ....MyTestSuiteTest
Running the test suite directly with right-click > Run 'MyTestSuiteTest" via IDE shows an additional entry in the unit test view:
MyTestSuiteTest
JUnit Jupiter
MyTest1Test
test2
MyTest3Test
test1
JUnit Vintage
<no name>
Although there are just two annotated methods to run with the test suite (one from MyTest1Test class, and one from MyTest3Test class), it says 3 test were run successfully. JUnit Vintage didn't show up before when vintage engine was exluded.
Using #ExtendWith with vintage engine included:
Maven > "test" doesn't run the test suite, no warning or similar in the console output. "right-click > Run 'MyTestSuiteTest" gives the reported error.
Excluding vintage engine:
#RunWith behaves as described initially in the question.
#ExtendWith Maven > "test" doesn't run the test suite, no warning or similar in the console output. "right-click > Run 'MyTestSuiteTest" gives the reported error.

Issue running Kogito DMN TrafficViolationTest in my junit

I imported dmn-quarkus-example project into eclipse. the project compiles and shows no errors. when I run mvn clean quarkus:dev, I was able to test the rest endpoint through postman and it works.
but my junit in my eclipse fails with 404.
I read few blogs and updated my application.properties file with the following, still no luck
quarkus.http.port=9090
%dev.quarkus.http.port=9191
quarkus.http.test-port=8181
the other thing I had to do to get my junit working is added the following dependencies in the pom.xml
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-launcher</artifactId>
<scope>test</scope>
</dependency>
it working still not able to understand why?
it started working after
mvn clean install and tried maven update on eclipse.....
it random....
This sounds like Eclipse is not configured properly, in general; based on the provided information not possible to even say if it's something related to Quarkus, or just a general Eclipse configuration issue.
Moreover, tested locally with Eclipse 2019-12 and no special configuration and no special plugin, running the JUnit test works out of the box correctly, in the example screenshot below running the unit test of the REST interface for Kogito on Quarkus example:
Please ensure you have no Maven exclusion in your Eclipse configuration, and that Eclipse is allowing to run the necessary Maven phases with M2Eclipse (like running any Maven project with Eclipse)

How to fix: Error message in TestNG java.lang.AbstractMethodError: org.openqa.selenium.MutableCapabilities.is(Ljava/lang/String;)Z

How to solve the following error message
java.lang.AbstractMethodError:
org.openqa.selenium.MutableCapabilities.is(Ljava/lang/String;)Z in
automation project with Java, Selenium Server (having the
configuration : client-combined-3.7.1-sources, client-combined-3.7.1,
commo ns-lang-2.6, org.eclipse.jgit_4.5.0.201609210915-r,
selenium-firefox-driver-3.0.0-beta1, selenium-server-standalone-3.7.1
firefox v. 44)
I have tried many combinations of selenium server & selenium Firefox driver but I was receiving other errors
<dependencies>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.8.8</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>3.7.1</version>
</dependency>
</dependencies>
Only these lines of code I am using:
System.setProperty("webdriver.gecko.driver", "C:\\Users\\Aila\\Downloads\\geckodriver-v0.19.1-win32\\geck‌​odriver.exe");
driver = new FirefoxDriver();
There are two solution for this isssue:
Adding correct version in Selenium project coz different version don't have same solution for my issue selenium-server-standalone-2.42.2.jar is correct.
This one is optional just try, if any of solution of this issue is not resolve.
Open Windows Command Prompt and type the command below:
java -Dwebdriver.gecko.driver="C:\NewAutomationCICD\resource\chromedriver.exe" -jar C:\NewAutomationCICD\libs\selenium-server-standalone-2.42.2.jar
The error says it all:
java.lang.AbstractMethodError: org.openqa.selenium.MutableCapabilities.is(Ljava/lang/String;)Z in automation project with Java, Selenium Server (having the configuration : client-combined-3.7.1-sources, client-combined-3.7.1, commo ns-lang-2.6, org.eclipse.jgit_4.5.0.201609210915-r, selenium-firefox-driver-3.0.0-beta1, selenium-server-standalone-3.7.1
First of all ensure that you are using all the binaries and jars of latest version (Selenium, GeckoDriver, Firefox Browser).
If you want to use Maven :
Remove all the Selenium related jars which you have added manually from your project.
Use Selenium and TestNG related jars only through Maven Dependencies.
Clean the Project
Execute mvn clean install test
If you want to use Selenium and TestNG without Maven:
Remove all the Selenium related jars once and add only the selenium-server-standalone-3.7.1 within your project.
Clean the Project
Execute your #Test as TestNG Test or TestNG Suite

How to run Junit Test in Maven when a test file is not in mavenise structure

I have so many java files and those java files test.java is not in src/test/java structure.
for ex: i have java file named abc.java in src/java/abc but the the test file of this java file named as abcTest.java is in src/java/junit/abc.
so how i will do the junit testing of this java file through maven pom.xml as maven wants normal java file in src/main/java and test files in src/test/java so how i will do the test through maven?
I have added the junit dependency and surefire plugin in my pom.xml .
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
and the surefire plugin is
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.10</version>
<configuration>
<testSourceDirectory>${basedir}/../src/java/junit/*.java</testSourceDirectory>
<testClassesDirectory>${project.build.directory}/classe‌​s/</testClassesDirectory>
<includes>
<include>abcTest.java</include>
</includes>
</configuration>
</plugin>
i have given mvn test command but still it is giving zero tests in log. Can anyone help me on this how to run junit test.java files through maven?
i have added these two below lines in my pom.xml after basedirectory tag and junit test cases ran successfully . and now i am running mvn install and all my testcases in test.java are running although i don't have maven like structure.
<testSourceDirectory>${basedir}/../src/java/junit</testSourceDirectory>
<testOutputDirectory>${basedir}/target/test-classes</testOutputDirectory>
and after that i have added surefire plugin and thats all my junit testcases are running.
Assuming you can't really the structure of the project, you'll have to customize the Surefire plugin.
Adding a Junit dependency won't be sufficient - its only allow to compile your test classes.
So, you're in the right direction - you'll have to customize a Surefire plugin of Maven responsible for running the tests.
Now, in order to make Surefire plugin running the tests that don't end with *Test.java you'll have to change inclusions. Namely, you'll have to specify regular expressions that will reflect the names of tests you wish to run.
This page describes just this.

Resources