Reputation: 1708
I wan to implement a Junit 5 test into Gradle project. I tried this:
Gradle configuration:
plugins {
id 'org.springframework.boot' version '2.5.5'
id 'io.spring.dependency-management' version '1.0.11.RELEASE'
id 'java'
}
group = 'test'
version = '0.0.1'
sourceCompatibility = '17'
repositories {
mavenCentral()
}
ext {
set('springCloudVersion', "2020.0.4")
}
dependencies {
...............
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.8.2'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.8.2'
}
dependencyManagement {
imports {
mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
}
}
test {
useJUnitPlatform()
}
Junit test:
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.Timeout;
import java.util.concurrent.TimeUnit;
public class GeneratePdf {
@DisplayName("Test MessageService.get()")
@Test
@Timeout(value = 60, unit = TimeUnit.SECONDS)
public void generatePdfFileTes() throws InterruptedException
{
System.out.println("test!");
}
}
When I run the code I get error:
Internal Error occurred.
org.junit.platform.commons.JUnitException: TestEngine with ID 'junit-jupiter' failed to discover tests
at org.junit.platform.launcher.core.EngineDiscoveryOrchestrator.discoverEngineRoot(EngineDiscoveryOrchestrator.java:160)
at org.junit.platform.launcher.core.EngineDiscoveryOrchestrator.discoverSafely(EngineDiscoveryOrchestrator.java:134)
at org.junit.platform.launcher.core.EngineDiscoveryOrchestrator.discover(EngineDiscoveryOrchestrator.java:108)
at org.junit.platform.launcher.core.EngineDiscoveryOrchestrator.discover(EngineDiscoveryOrchestrator.java:80)
at org.junit.platform.launcher.core.DefaultLauncher.discover(DefaultLauncher.java:110)
at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:86)
at org.junit.platform.launcher.core.DefaultLauncherSession$DelegatingLauncher.execute(DefaultLauncherSession.java:86)
at org.junit.platform.launcher.core.SessionPerRequestLauncher.execute(SessionPerRequestLauncher.java:53)
at com.intellij.junit5.JUnit5IdeaTestRunner.startRunnerWithArgs(JUnit5IdeaTestRunner.java:71)
at com.intellij.rt.junit.IdeaTestRunner$Repeater$1.execute(IdeaTestRunner.java:38)
at com.intellij.rt.execution.junit.TestsRepeater.repeat(TestsRepeater.java:11)
at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:35)
at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:235)
at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:54)
Caused by: org.junit.platform.commons.JUnitException: MethodSelector [className = 'org.merchant.poc.GeneratePdf', methodName = 'generatePdfFileTes', methodParameterTypes = ''] resolution failed
at org.junit.platform.launcher.listeners.discovery.AbortOnFailureLauncherDiscoveryListener.selectorProcessed(AbortOnFailureLauncherDiscoveryListener.java:39)
at org.junit.platform.engine.support.discovery.EngineDiscoveryRequestResolution.resolveCompletely(EngineDiscoveryRequestResolution.java:102)
at org.junit.platform.engine.support.discovery.EngineDiscoveryRequestResolution.run(EngineDiscoveryRequestResolution.java:82)
at org.junit.platform.engine.support.discovery.EngineDiscoveryRequestResolver.resolve(EngineDiscoveryRequestResolver.java:113)
at org.junit.jupiter.engine.discovery.DiscoverySelectorResolver.resolveSelectors(DiscoverySelectorResolver.java:46)
at org.junit.jupiter.engine.JupiterTestEngine.discover(JupiterTestEngine.java:69)
at org.junit.platform.launcher.core.EngineDiscoveryOrchestrator.discoverEngineRoot(EngineDiscoveryOrchestrator.java:152)
... 13 more
Caused by: org.junit.platform.commons.PreconditionViolationException: Could not load class with name: org.merchant.poc.GeneratePdf
at org.junit.platform.engine.discovery.MethodSelector.lambda$lazyLoadJavaClass$0(MethodSelector.java:157)
at org.junit.platform.commons.function.Try$Failure.getOrThrow(Try.java:335)
at org.junit.platform.engine.discovery.MethodSelector.lazyLoadJavaClass(MethodSelector.java:156)
at org.junit.platform.engine.discovery.MethodSelector.getJavaClass(MethodSelector.java:135)
at org.junit.jupiter.engine.discovery.MethodSelectorResolver.resolve(MethodSelectorResolver.java:73)
at org.junit.platform.engine.support.discovery.EngineDiscoveryRequestResolution.lambda$resolve$2(EngineDiscoveryRequestResolution.java:146)
at java.base/java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:197)
at java.base/java.util.ArrayList$ArrayListSpliterator.tryAdvance(ArrayList.java:1602)
at java.base/java.util.stream.ReferencePipeline.forEachWithCancel(ReferencePipeline.java:129)
at java.base/java.util.stream.AbstractPipeline.copyIntoWithCancel(AbstractPipeline.java:527)
at java.base/java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:513)
at java.base/java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:499)
at java.base/java.util.stream.FindOps$FindOp.evaluateSequential(FindOps.java:150)
at java.base/java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
at java.base/java.util.stream.ReferencePipeline.findFirst(ReferencePipeline.java:647)
at org.junit.platform.engine.support.discovery.EngineDiscoveryRequestResolution.resolve(EngineDiscoveryRequestResolution.java:185)
at org.junit.platform.engine.support.discovery.EngineDiscoveryRequestResolution.resolve(EngineDiscoveryRequestResolution.java:125)
at org.junit.platform.engine.support.discovery.EngineDiscoveryRequestResolution.resolveCompletely(EngineDiscoveryRequestResolution.java:91)
... 18 more
Caused by: java.lang.ClassNotFoundException: org.merchant.poc.GeneratePdf
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:641)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:188)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:520)
at org.junit.platform.commons.util.ReflectionUtils.lambda$tryToLoadClass$9(ReflectionUtils.java:829)
at org.junit.platform.commons.function.Try.lambda$call$0(Try.java:57)
at org.junit.platform.commons.function.Try.of(Try.java:93)
at org.junit.platform.commons.function.Try.call(Try.java:57)
at org.junit.platform.commons.util.ReflectionUtils.tryToLoadClass(ReflectionUtils.java:792)
at org.junit.platform.commons.util.ReflectionUtils.tryToLoadClass(ReflectionUtils.java:748)
... 34 more
Do you know how this issue can be fixed?
EDIT:
import org.junit.jupiter.api.Test;
public class InvoiceTests {
@Test
public void generateTest()
{
System.out.println("test!");
}
}
I tried this but again it's not working.
Upvotes: 88
Views: 197644
Reputation: 172
Happend with junit-jupiter 5.12.0 and mockito-junit-jupiter 5.16.0 and 5.15.2.
Solution was to go back to junit-juper 5.11.4, because this seems to be the version that works with the mockito dependency.
In other projects without mockito, the junit-jupiter version 5.12.0 works without problems.
Upvotes: 0
Reputation: 31
It worked after adding Junit Jupiter (Aggregator).
Example :
For Maven, Add below dependency
<!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.11.0</version>
<scope>test</scope>
</dependency>
For Gradle,
// https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter
testImplementation 'org.junit.jupiter:junit-jupiter:5.11.0'
Upvotes: 0
Reputation: 81
Generally speaking the problem is in a conflict of different versions of junit5 dependencies in a project. To find this conflict you’d better generate a dependency tree:
mvn dependency:tree (in maven)
and check which transitive dependencies fetch the same junit5 dependencies (i.e. junit-jupiter-engine, junit-jupiter-api, etc) of different versions.
To fix the problem you would need in your pom.xml (in maven) to
Upvotes: 0
Reputation: 101
In my case, i had this error of tests not found followed by "caused by..ClassNotFoundException: CleanupMode"...
The reason was i only had junit-jupiter-api dependency and the second dependency: junit-jupiter-engine was missing from pom! And it was not obvious you need both
After adding the following:
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.8.2</version>
<scope>test</scope>
</dependency>
it resolved the issue
Upvotes: 3
Reputation: 19
Removing and adding the @Test annotation fixed the issue for me
I am using java 17 and intellij
Upvotes: 1
Reputation: 450
I was facing similar issue. I tried a couple of answers involving dependency upgrade given above but it didn't work.
Although it's defined correct in your code, but someone else might face this problem, hence sharing here In the below code the where test execution is triggered from, includeCategories was commented.
test {
minHeapSize = "128m"
maxHeapSize = "1024m"
jvmArgs "-XX:+UseG1GC", "-XX:MinHeapFreeRatio=30", "-XX:MaxHeapFreeRatio=50", "-XX:+UseStringDeduplication", "--add-opens=java.base/java.util.stream=ALL-UNNAMED", "--add-opens=java.base/java.util=ALL-UNNAMED", "--add-opens=java.base/java.text=ALL-UNNAMED", "--add-opens=java.base/java.util.regex=ALL-UNNAMED", "--add-opens=java.base/java.lang=ALL-UNNAMED", "--add-opens=java.base/jdk.internal.reflect=ALL-UNNAMED", "--add-opens=java.base/java.util.concurrent=ALL-UNNAMED", "--add-opens=java.base/jdk.internal.loader=ALL-UNNAMED", "--add-opens=java.base/java.lang.reflect=ALL-UNNAMED", "--add-opens=java.base/java.time=ALL-UNNAMED", "--add-opens=java.base/java.io=ALL-UNNAMED", "--add-opens=java.sql/java.sql=ALL-UNNAMED", "--add-opens=java.base/java.math=ALL-UNNAMED", "--add-opens=java.xml/jdk.xml.internal=ALL-UNNAMED"
useJUnitPlatform {
// includeCategories 'com.package.project.UnitTests'
ignoreFailures = true
}
Upvotes: 0
Reputation: 6872
My full error was:
[ERROR] TestEngine with ID 'junit-jupiter' failed to discover tests
...
...
[ERROR] Please refer to /Users/Shared/trex2/trex2-lib/target/surefire-reports for the individual test results.
[ERROR] Please refer to dump files (if any exist) [date].dump, [date]-jvmRun[N].dump and [date].dumpstream.
[ERROR] There was an error in the forked process
That suggests to look for *.dump
files. Searching the target
folder there were indeed surefire dump files:
% find . -name \*.dump
./target/surefire-reports/2024-04-07T08-33-37_487-jvmRun1.dump
Looking inside those showed:
# Created at 2024-04-07T08:33:38.346
org.junit.platform.commons.JUnitException: TestEngine with ID 'junit-jupiter' failed to discover tests
at org.junit.platform.launcher.core.EngineDiscoveryOrchestrator.discoverEngineRoot(EngineDiscoveryOrchestrator.java:160)
...
Caused by: org.junit.platform.commons.JUnitException: ClassSelector [className = 'com.github.trex_paxos.AlgorithmTests', classLoader = null] resolution failed
at org.junit.platform.launcher.listeners.discovery.AbortOnFailureLauncherDiscoveryListener.selectorProcessed(AbortOnFailureLauncherDiscoveryListener.java:39)
...
Caused by: java.lang.UnsupportedClassVersionError: Preview features are not enabled for com/github/trex_paxos/Progress (class file version 66.65535). Try running with '--enable-preview'
at java.base/java.lang.ClassLoader.defineClass1(Native Method)
at java.base/java.lang.ClassLoader.defineClass(ClassLoader.java:1023)
at java.base/java.security.SecureClassLoader.defineClass(SecureClassLoader.java:150)
A long way down in that log at the end of a line (scroll right to the end) that dump out suggests Try running with '--enable-preview'
.
I was trying out Java 22 preview features such as:
var msg = STR."unreachable progress=\{progress}, accept=\{accept}"
I had enabled --enable-preview
on the compiler plugin with:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.10.1</version>
<configuration>
<source>22</source>
<target>22</target>
<compilerArgs>--enable-preview</compilerArgs>
</configuration>
</plugin>
The use of preview features needs to be set at both compile time and runtime (to use preview versioned .class files).
I needed to add the same flag to the maven-surefire-plugin:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.2.5</version>
<configuration>
<argLine>--enable-preview</argLine>
</configuration>
</plugin>
Upvotes: 0
Reputation: 1454
While playing around with new features of Java 21 (openjdk-21) on intelliJ, I ran into this issue. Removing or commenting the plugin with --enable-preview
solved the problem.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>21</source>
<target>21</target>
<compilerArgs>--enable-preview</compilerArgs>
</configuration>
</plugin>
Also had to remove references to new features like STR
besides doing mvn clean verify
.
Note:
Answer from @AceneKASDI and this Github issue were useful
Upvotes: 0
Reputation: 1103
For me, the best solution was adding junit-bom
dependency in dependencyManagement
.
<dependencyManagement>
<dependencies>
...
<dependency>
<groupId>org.junit</groupId>
<artifactId>junit-bom</artifactId>
<version>5.10.1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
Then, you can just add regular dependency wherever you want in scope of dependencyManagement
declaration.
</dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
It will take all proper versions for all junit
modules (also jupiter
) from junit-bom
declared in dependencyManagement
.
Upvotes: 7
Reputation: 1394
I ran into this issue when I upgraded from Sprint Boot 3.1 to 3.2
I resolved by updating to the latest version of the junit-platform-launcher
<!-- https://mvnrepository.com/artifact/org.junit.platform/junit-platform-launcher -->
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-launcher</artifactId>
<version>1.10.2</version>
<scope>test</scope>
</dependency>
Upvotes: 0
Reputation: 95
Removing the junit-jupiter-engine dependency generated by inteliJ works.
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.8.2</version>
<scope>test</scope>
</dependency>
This will downgrade junit-jupiter-engine from 5.8.2 to 5.6.3:
Upvotes: 2
Reputation: 634
Was building a new Springboot 3.x app and saw this. I had this trouble because I was adding Junit Jupiter dependencies manually as opposed to using the Springboot 3.x testing starter dependency. Removing all of the manual JUnit Jupiter dependencies (api, engine, paramters etc) and adding :
// https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-test
testImplementation 'org.springframework.boot:spring-boot-starter-test:3.1.5'
resolved the issue for me. I was then able to execute gradle myproject:test
and the tests were discovered and run. This also allowed Eclipse to execute the test by right clicking on the test class and clicking "Run As" and then selecting Junit Test. Hope it saves someone time.
Upvotes: 1
Reputation: 41
If you didn't configure a database of your application, remove the @AutoConfigureTestDatabase
annotation from test classes as it can throw the exception.
As you remove it, you don't have to downgrade JUnit to version 5.7.0.
Upvotes: -1
Reputation: 355
I initially used mvn clean package
- it worked fine. But then, when I run it with mvn verify
- it failed.
Java 20 Junit jupiter api 5.9.2
The solutions is simple, I have renamed the file from ClassNameIntegrationTest.java to ClassNameIT.java it did the trick!
Upvotes: 0
Reputation: 139
If your IDE is IntelliJ, check if you've set the directory as a Test.
Here this directory isn't marked as a Test Source, so the icon color is gray, and the folders are fully filled.
This is the way to mark the directory as a Test Source.
Now, the directory is marked a Test Source, changing the color, and the icon for the other folders, and now the IDE can recognise the tests inside the directory :)
Upvotes: 7
Reputation: 1917
My case was different and might help someone.
I had converted a Java project to Spring boot project. Before doing this, I had imported junit-jupiter-engine
dependency in my pom file.
I had to remove the junit-jupiter-engine
dependency from the project and had to annotate my tests with:
@RunWith(SpringRunner.class)
@FixMethodOrder(value = MethodSorters.NAME_ASCENDING)
Upvotes: 0
Reputation: 351
testImplementation('org.junit.jupiter:junit-jupiter:5.9.0')
Upvotes: -1
Reputation: 5947
JUnit libraries versions you specify must match JUnit POMs.
I got this error during routine dependency upgrade. I upgraded junit-jupiter-api
and junit-jupiter-engine
to 5.8.2 but left junit-platform-engine
and junit-platform-commons
pinned to 1.7.2. After some experiments I opened JUnit POM and found out that 5.8.2 uses 1.8.2 dependency. That was the reason. Always check that related dependencies are in sync!
Upvotes: 8
Reputation: 461
For me, this problem happened for me when adding the junit-platform-suite-engine:1.9.1
dependency. By using gradle dependencies
, as shown below, you can see that the 1.9.1 version causes a conflict between the junit versions set by spring boot.
| +--- org.junit.jupiter:junit-jupiter:5.8.2
| | +--- org.junit:junit-bom:5.8.2 -> 5.9.1
| | | +--- org.junit.jupiter:junit-jupiter:5.9.1 -> 5.8.2 (c)
| | | +--- org.junit.jupiter:junit-jupiter-api:5.9.1 -> 5.8.2 (c)
| | | +--- org.junit.jupiter:junit-jupiter-engine:5.9.1 -> 5.8.2 (c)
| | | +--- org.junit.jupiter:junit-jupiter-params:5.9.1 -> 5.8.2 (c)
| | | +--- org.junit.platform:junit-platform-engine:1.9.1 -> 1.8.2 (c)
| | | +--- org.junit.platform:junit-platform-suite-api:1.9.1 -> 1.8.2 (c)
| | | +--- org.junit.platform:junit-platform-suite-commons:1.9.1 -> 1.8.2 (c)
| | | +--- org.junit.platform:junit-platform-suite-engine:1.9.1 (c)
| | | +--- org.junit.platform:junit-platform-launcher:1.9.1 -> 1.8.2 (c)
| | | \--- org.junit.platform:junit-platform-commons:1.9.1 -> 1.8.2 (c)
To solve this, either remove the version from the dependency like below
testImplementation("org.junit.platform:junit-platform-suite-engine")
Or use the same version as spring boot (i.e., 1.8.2 in my case)
testImplementation("org.junit.platform:junit-platform-suite-engine:1.8.2")
Upvotes: 0
Reputation: 107
In my case I have
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.9.0</version>
<scope>test</scope>
</dependency>
with
<properties>
<java.version>17</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>${maven.compiler.source}</maven.compiler.target>
<junit.jupiter.version>5.8.2</junit.jupiter.version>
<junit.platform.version>1.8.2</junit.platform.version>
</properties>
I simply removed
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.9.0</version>
<scope>test</scope>
</dependency>
And everything worked well!!
Upvotes: 5
Reputation: 1166
I had this issue when I upgraded the versions of JUnit from 5.7.0 to 5.8.2.
Try with version 5.7.0 for org.junit.jupiter:junit-jupiter-api
and org.junit.jupiter:junit-jupiter-engine
Upvotes: 54
Reputation: 11
junit-jupiter-engine is a part of junit-jupiter add junit-jupiter from enter link description here in your pom.xml
for Example:
<!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.9.1</version>
<scope>test</scope>
</dependency>
Upvotes: 0
Reputation:
I had to add this in order for it to play nice with spring-boot-starter-test
and JDK 17
ext['junit-jupiter.version'] = '5.9.0'
Note: this is in addition to normal test dependencies and test
task configuration.
Note2: AFAIK name requirements for test classes and test method and public
visibility don't apply anymore with junit5 (no public class MyClassTest
, public void mymethodTest()
)
Upvotes: 2
Reputation: 51
adding this dependency alone will fix this issue. I tried with 5.6.2, any version is fine
testImplementation('org.junit.jupiter:junit-jupiter:5.6.2')
Upvotes: 3
Reputation: 61
I was receiving such exception because had dependencies to both junit4 and junit5, removing junit4 solved the problem.
Upvotes: 0
Reputation: 384
What is the project structure you are following? I remember facing a similar issue when I started out, so basically what I did was just follow this structure:
project-dir
-- src
-- main
-- java
-- classes
-- test
-- java
-- test-classes
In addition to the above structure, make sure you are naming your test classes in a correct format as mentioned in the answer by @johanneslink
Upvotes: 9
Reputation: 452
Environment :
I have faced the same issue after upgrading my project from JDK 11 to JDK 17 and junit-jupiter-api from 5.7.0 to 5.8.2.
On my project, the working version is 5.7.2
Follow the issue here : Github issue
Upvotes: 23
Reputation: 1310
I got the same error because I'd named my integration test class as XyzIT, post renaming it to XyzIntegrationTest, it started working.
Upvotes: 1
Reputation: 5361
GeneratePdf
does not match the default name pattern for test classes. The default pattern is Test*|*Test|*Tests
.
You can change it in your Gradle file with
test {
useJUnitPlatform()
include '**/*Pdf'
}
Upvotes: 3