Can't find tests (JUnit 5)

Answered

When I attempt to run an entire folder of unit tests, I am told that "No tests were found". I am told that "Tests were not found in module [OMITTED MODULE NAME]. Use module [OTHER OMITTED MODULE NAME] instead." I have changed and applied the classpath to the specified module under Run -> Debug Configurations, but I every time I run the folder again it reverts to the prior classpath.

When I attempt to run a single unit test file, I get the following stack trace:

Feb 06, 2018 2:01:43 PM org.junit.platform.launcher.core.DefaultLauncher handleThrowable
WARNING: TestEngine with ID 'junit-jupiter' failed to discover tests
org.junit.platform.commons.util.PreconditionViolationException: Could not load class with name: [OMITTED JAVA FILE NAME]
at org.junit.platform.engine.discovery.ClassSelector.lambda$getJavaClass$0(ClassSelector.java:71)
at java.util.Optional.orElseThrow(Optional.java:290)
at org.junit.platform.engine.discovery.ClassSelector.getJavaClass(ClassSelector.java:70)
at org.junit.jupiter.engine.discovery.DiscoverySelectorResolver.lambda$resolveSelectors$2(DiscoverySelectorResolver.java:59)
at java.util.ArrayList.forEach(ArrayList.java:1257)
at org.junit.jupiter.engine.discovery.DiscoverySelectorResolver.resolveSelectors(DiscoverySelectorResolver.java:58)
at org.junit.jupiter.engine.JupiterTestEngine.resolveDiscoveryRequest(JupiterTestEngine.java:68)
at org.junit.jupiter.engine.JupiterTestEngine.discover(JupiterTestEngine.java:61)
at org.junit.platform.launcher.core.DefaultLauncher.discoverEngineRoot(DefaultLauncher.java:130)
at org.junit.platform.launcher.core.DefaultLauncher.discoverRoot(DefaultLauncher.java:117)
at org.junit.platform.launcher.core.DefaultLauncher.discover(DefaultLauncher.java:82)
at com.intellij.junit5.JUnit5IdeaTestRunner.startRunnerWithArgs(JUnit5IdeaTestRunner.java:48)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)

 

As far as I can tell, my setup is identical to that of my coworkers, who are not having any issues.

 

IntelliJ IDEA version: 3.4

OS: Mac OS X

Relevant Gradle dependencies:

testCompile 'org.junit.jupiter:junit-jupiter-api:5.0.2'
testRuntime 'org.junit.jupiter:junit-jupiter-engine:5.0.2'
5
18 comments

Please try File | Invalidate Caches | Invalidate and Restart, then rebuild the project.

If the issue persists, please provide the sample project to reproduce.

4

I resolved the issue excluded the junit-jupiter-engine

<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>
<exclusion>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
4

You probably don't need this dependency:

testRuntime 'org.junit.jupiter:junit-jupiter-engine:5.0.2

since IntelliJ IDEA 2017.3.4 comes with the up to date JUnit version bundled.

See http://junit.org/junit5/docs/current/user-guide/#running-tests-ide-intellij-idea:

> // Only needed to run tests in a version of IntelliJ IDEA that bundles an older version

Fix the dependencies, delete .idea directory, reimport the project from Gradle.

3

I have similar issues with Java 11 (if that is, in fact, the root cause).

2

Please create an issue at YouTrack: https://youtrack.jetbrains.com/issues with project sample attached. Thank you.

1

Found reason o https://junit.org/junit5/docs/current/user-guide/#running-tests-ide-intellij-idea

Idea will download specific versions of JUnit platform dependencies specified  in mvane/gradle.

 

 

1

I've tried your suggestion, to no avail. Unfortunately, I'm not legally allowed to provide the project in question. Do you have any other ideas?

0

It turns out I was using a different JDK version from my teammates. I downgraded to what they were using and it seems to work now.

0

I have same problem. It's seems it doesn't work with JDK 10.

0

This problem appeared in a project that stored its junit-dependencies explicitly (no maven or whatsoever), and somehow the module dependencies were removed and replaced by the IDE's bundled JUnit 5. I removed that one and restored the dependencies on the project's JARs of these.

0

Why would I want to exclude `junit-jupiter-engine`, which is required for my maven build to run, so that I can run tests directly in the IDE?

This is fundamentally broken.

0

Hello Dave,

Please share the sample project where you experience the issue at YouTrack:

https://youtrack.jetbrains.com/issues/IDEA

Thank you 

0

Hi Olga,

Unfortunately I can't - it is proprietary and protected by NDA. And I don't have time to experiment with creating an MRE.

Honestly though, the details of my project should not matter. Running a correctly defined JUnit test from the IDE invoker should just work regardless of what build framework I use or what test dependencies I have for that framework.

0

Dave, please clarify what version of Junit framework is used in your case? I've checked the sample project with Juni5 tests with the dependencies that you've provided and the tests are successfully run. 

Here is the code sample:

package pack;

public class Sample {
public String foo() {
return "aaa";
}
}

package pack;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

class SampleTest {
@Test
public void fooTest() {
Assertions.assertEquals(new Sample().foo(), "aaa");
}
}

 

The version of IntelliJ IDEA used: https://www.jetbrains.com/idea/nextversion.

If possible can you please check the same one. 

Thank you 

 

0

Hi Olga,

I am using Junit5. I didn’t mention my dependencies, but the project is built using maven and i am using the Junit5 Jupiter framework. IntelliJ IDEA version is the latest Ultimate GA build.

Did you try running tests for the entire test class or just the fooTest method?

If i try to run a single test method it works fine. But if I try to run tests for a test class it fails to find any tests.

This is annoying for test classes with dozens of test methods.

The same tests are detected and run by the maven runner. But running the entire maven build, which takes minutes, is not practical for just testing one class i have changed in a TDD loop.

0

Hello,

The test is run for me when I run it from any level: package, module, class. Please see the issue created: https://youtrack.jetbrains.com/issue/IDEA-289774

my sample project is attached to the issue. Is the problem reproduced for you with it? Thank you

0

Just a note to anyone coming across this more recently that the reason Yurii Ch's solution above may work for them is if their project actually has older (Junit 4) dependencies in it, like mine. I upgraded my project to Java 11 and ran into the same problem, because another dependency was pulling in junit-jupiter-api and I think IntelliJ may be interpreting that to mean that my project should build tests with Junit 5. Excluding junit-jupiter-api fixed it.

0

I got it fixed in Gradle 8 by providing suite configuration with JUnitJupiter, however, Idea (IntelliJ IDEA 2023.2.2 (Ultimate Edition)) still see it as a gradle task, not Junit.

dependencies {
  testImplementation("org.junit.jupiter:junit-jupiter-api:5.6.0" }
}

testing {
    suites {
        val test by getting(JvmTestSuite::class) {
            useJUnitJupiter()
        }
    }
}

 

0

Please sign in to leave a comment.