50

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.

1
  • 1
    Judging from the stacktrace "java.lang.ClassNotFoundException: org.merchant.poc.GeneratePdf" something in your setup seems to be wrong. Might be conflicting versions or source paths or a few other things. If a clean build doesn't help, I suggest you create a minimum reproducing repo and link it here, so that others can recreate the effect. Dec 23, 2021 at 9:53

22 Answers 22

36

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

4
  • 5
    Same. Do we know what the difference in 5.8.2 is that's causing this? Mar 30, 2022 at 12:02
  • 3
    Thanks this helped. This was the solution to my problem when I upgraded from spring boot 2.5.10 to 2.6.5 Apr 4, 2022 at 3:56
  • yes, It was working for me also, Eclipse version Version: 2022-03 (4.23.0) Build id: 20220310-1457, Changed to 5.7.0 Thanks. Aug 18, 2022 at 4:54
  • Thanks! Also worked for me with via Intellij runner (Intellij Ultimate 2022.2.3) and also with Apache Ant 1.10.10
    – Max M
    Oct 21, 2022 at 14:37
15

Environment :

  • IntelliJ
  • Java 17

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

9

In my case, a mvn clean compile helped.

0
8

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

1
  • Certainly. Exactly.
    – 007
    Nov 28 at 15:16
3

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'
}
1
  • I tried to change the names(see the post) but again it's not working. Dec 22, 2021 at 21:54
3

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')
1
  • This worked for me with Kotlin 1.7.10 and JDK 17.0.4. But these other jupiter versions did not work: 5.6.3, 5.7.0, 5.7.2, 5.8.2,5.9.1
    – devdanke
    Oct 17, 2022 at 12:03
2

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())

Reference here

1
  • This solved my problem as some (but not all) of the junit dependencies was downgraded by spring.
    – Semaphor
    Nov 21 at 12:44
1

I got the same error because I'd named my integration test class as XyzIT, post renaming it to XyzIntegrationTest, it started working.

2
  • 3
    Maven uses two different plugins. For integrationtests (ending in IT) maven uses maven-failsafe-plugin and for usual unit tests maven uses maven-surefire-plugin Mar 23 at 10:28
  • @ValerijDobler ok, so how else could I have fixed my error for ITs?
    – ADJ
    Mar 28 at 15:00
1

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!

1
  • Thanks - This got me onto the solution - not sure how but my junit-platform-engine dependency (coming in transiently) was at a higher version than junit-jupiter-api. Making sure those were the same major/minor solved my issue.
    – djchapm
    Aug 29 at 1:59
1

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.

enter image description here

This is the way to mark the directory as a Test Source.

enter image description here

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 :)

enter image description here

0

I was receiving such exception because had dependencies to both junit4 and junit5, removing junit4 solved the problem.

0

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!!

0

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")

0

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)
0

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!

0

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.

-1

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>
-1

testImplementation('org.junit.jupiter:junit-jupiter:5.9.0')

1
  • 2
    It would be nice to give a brief explanation of how this works / how it solves the problem, and how it's different than existing answers.
    – starball
    Dec 26, 2022 at 22:35
-1

Incorrect package path was my reason for this issue. Fixing the package path resolved the issue.

[package path present on the top of the java class] package <>

-1

All of such issues had magically gone away after I did the following:

1- open the "Maven" tool window in IntelliJ IDEA. 2- execute "test". 3- then try again to run your unit tests.

It started to work for me.

-1

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.

-1

End up getting java.lang.NoSuchMethodError error so found this link to solve the issue.

1
  • Please don't leave link-only answers -- include the relevant details within your post, so that if (when) the link goes down the answer doesn't become useless. Oct 7 at 0:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.