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

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

Related

Maven runs JUnit 5 tests but does not run TestFactory method

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.

Using GMavenPlus to run spock groovy tests alongside java junit tests from maven surefire [duplicate]

Env
java
$ java -version
java version "1.8.0_241"
Java(TM) SE Runtime Environment (build 1.8.0_241-b07)
Java HotSpot(TM) 64-Bit Server VM (build 25.241-b07, mixed mode)
maven
$ mvn -version
Apache Maven 3.6.3 (cecedd343002696d0abb50b32b541b8a6ba2883f)
Maven home: C:\Program Files\JetBrains\IntelliJ IDEA 2019.2.3\plugins\maven\lib\maven3
Java version: 1.8.0_231, vendor: Oracle Corporation, runtime: C:\Program Files\Java\jdk1.8.0_231\jre
Default locale: zh_CN, platform encoding: GBK
OS name: "windows 10", version: "10.0", arch: "amd64", family: "windows"
pom.xml -- Forked from spockframework/spock-example
Problem
I forked spockframework/spock-example add java test directory and HelloJUnitTest.java => sunzy/spock-exmaple
the Spock Tests can execute,but JUnit Tests can't
Snapshots
mvn clean test
only spock tests!
mvn clean test -Dtest=HelloJUnitTest
JUnit Test had generate in target/test-classes*
mvn clean test -Dtest=HelloSpocSpec
mvn -X include test-clasess
Okay, I have taken a look at your project. As you said, it is just the Spock sample project, upgraded to run Spock 2 tests. BTW, it still should be upgraded further, because in the current configuration compilation does not work with current Java versions, but that is off topic here, I am just mentioning it because I ran into a problem and then downgraded to Java 8 in order quickly reproduce your actual problem. The JUnit test's package name is not the problem either, even though the default package is always ugly, just like for the Spock tests.
Spock 1.x is based on JUnit 4, but Spock 2.x is based on JUnit 5 platform. This is also the one automatically found by Surefire when analysing the project dependencies. If you want Surefire to run multiple engines in parallel, you need to configure the corresponding providers as plugin dependencies, as mentioned here.
In your case, just add this to the POM:
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M4</version>
<configuration>
<useFile>false</useFile>
<includes>
<include>**/*Test.java</include>
<include>**/*Spec.java</include>
</includes>
</configuration>
<!-- Run Spock 2 and JUnit 4 tests in parallel -->
<dependencies>
<dependency>
<groupId>org.apache.maven.surefire</groupId>
<artifactId>surefire-junit47</artifactId>
<version>3.0.0-M4</version>
</dependency>
<dependency>
<groupId>org.apache.maven.surefire</groupId>
<artifactId>surefire-junit-platform</artifactId>
<version>3.0.0-M4</version>
</dependency>
</dependencies>
</plugin>
It also makes sense to explicitly add a test-scoped dependency to JUnit 4.12 or 4.13 to your POM because JUnit 4.12 is only a transitive dependency in your current POM. But later Spock 2 versions might remove that dependency because it is not really needed. I think that already happened with 2.0-M3. So be careful.
After this change, Maven says:
[INFO] --- maven-surefire-plugin:3.0.0-M4:test (default-test) # spock-example ---
[INFO]
[INFO] -------------------------------------------------------
[INFO] T E S T S
[INFO] -------------------------------------------------------
[INFO] Running HelloJUnitTest
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.059 s - in HelloJUnitTest
[INFO]
[INFO] Results:
[INFO]
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
[INFO]
[INFO]
[INFO] -------------------------------------------------------
[INFO] T E S T S
[INFO] -------------------------------------------------------
[INFO] Running DatabaseDrivenSpec
[INFO] Tests run: 3, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.171 s - in DatabaseDrivenSpec
(...)
[INFO]
[INFO] Results:
[INFO]
[INFO] Tests run: 39, Failures: 0, Errors: 0, Skipped: 0
If you decide to upgrade from JUnit 4 to 5, maybe configuration gets easier for you because then both Spock and JUnit use the same provider. In that case please add a JUnit 5 dependency so your tests can import the corresponding test annotations and assertion methods.
Update: After #khmarbaise commented on using vintage engine and me fully agreeing that it is the better solution, I want to show you how to do that instead of adding plugin dependencies to Surefire. (So you can delete those if you want to use this solution):
<dependency> <!-- only required if you want to run JUnit 4 tests alongside Spock 2 -->
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
<version>5.5.2</version>
<scope>test</scope>
</dependency>
Why version 5.5.2 and not e.g. 5.6.2? In order to avoid version conflicts and subsequent warnings about vintage engine not finding tests in the Groovy directory. This is just because the POM in this sample project still uses a Spock 2.0-M1 BOM. As I said, it ought to be updated. But with this version it just works because it depends on the same JUnit 5 platform version as Spock in this configuration.
BTW, now Maven executes the Spock tests first and then the JUnit 4 tests, so the log output for both would be in reverse order.

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)

Springboot test with coverage in Intellij

I have a spring boot 2 project with maven pom in Intellij
I thought maven uses different internal coverage tools compared to jacoco or something similar
If I click on - Run All Feature in Test with coverage
I get
Error: Could not find or load main class cucumber.api.cli.Main
If I add cucumber dependency (I dont have cucumber based tests or want it)
Exception in thread "main" cucumber.runtime.CucumberException:
No backends were found. Please make sure you have a
backend module on your CLASSPATH.
I just want to run simple spring boot rest based tests with coverage
What setup do I need?
Edit:
I had
Caused by: java.lang.ClassNotFoundException: org.jetbrains.plugins.cucumber.java.run.CucumberJvm3SMFormatter
I needed this in the pom
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-java</artifactId>
<version>1.2.2</version>
</dependency>
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-junit</artifactId>
<version>1.2.2</version>
</dependency>
and needed cucumber for java plugin installed
Now everything runs, no errors
BUT no code coverage, it is blank
It's a known issue that JUnit is not suggested from contex menu of project root: https://youtrack.jetbrains.com/issue/IDEA-198762
It appears you have the Cucumber integration installed in IDEA. This adds the option to run all feature files in the root of your project. If you want to run JUnit tests you have to drill down to src/main/test/java and select "Run All Tests".

How to run a mix of JUnit 4 and JUnit 5 tests in parallel using maven-failsafe-plugin

I am trying to run parallel integration tests in mix JUnit 4 and JUnit 5 using Junit Jupiter and Junit Vintage packages as well as adding a surefire plugin for junit 5 in dependencies for latest maven-failsafe-plugin.
<dependencies>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-surefire-provider</artifactId>
<version>1.0.1</version>
</dependency>
</dependencies>
All the test run, but not in parallel and take a lot of time. If I don't add this dependency it won't pick and run the Junit 5 tests but run the other tests in parallel, it is configured in the plug in.
There is no official documentation for failsafe plugin using Junit 5.
Is there a way to run the tests in parallel ?
Edit : logger write there is no use of threads (only main thread) when adding this dependency.

Resources