Using JUnit 5 in IntelliJ IDEA

Posted on by Trisha Gee

One of the new features in IntelliJ IDEA 2016.2 is support for the new JUnit 5 testing framework.  Almost all Java developers will have used JUnit at some point, so it’s pretty exciting to find the newest version has evolved with the times and provides a number of new features, some of which may be familiar to those who have used other frameworks.

IntelliJ IDEA supports the ability to actually run tests written for JUnit 5 – there’s no need to use the additional libraries (like the Gradle or Maven plugins for example), all you need is to include the JUnit 5 dependency. Here we’re using Gradle to include the dependency to the M2 version:

Setting up dependencies

Once you’ve done this, you can start writing tests that use the new annotations:

Simple JUnit 5 test

Running these tests will give you familiar-looking results in the IntelliJ IDEA run window:

JUnit 5 test results

You’ll notice JUnit Jupiter is the test engine for the new JUnit tests, and that you can run both new and old tests.

At first glance, the new JUnit 5 tests are no different to JUnit 4 ones, other than the annotations being imported from a different package, and showing two types of runner on the results.

But JUnit 5 comes with some new features, such as the ability to add a readable name or description for the test, so you no longer have to rely on a method name, using @DisplayName:

Using @DisplayName

You can group assertions so that all assertions are run, regardless of whether one (or more) fails, giving you better visibility over the true state of a failure:

Grouping assertions

And you can use the @Nested annotation, so you can write BDD-style tests or group the tests in a class by some dimension:

Running @Nested tests

JUnit 5 also has a new way of handling expected Exceptions, by letting you assert specific Exceptions are thrown

JUnit 5 can expect exceptions

And you can even get the thrown Exception to check for further details:

Inspect Exception

These are just some of the features available to use in JUnit 5, and IntelliJ’s support for the new framework is also evolving.

If you think JUnit 5 looks interesting, download IntelliJ IDEA 2016.2 and take a look at:

And, of course, you can check out more of the features available in IntelliJ IDEA 2016.2.

Comments below can no longer be edited.

59 Responses to Using JUnit 5 in IntelliJ IDEA

  1. Dmitrij says:

    August 3, 2016

    Getting the following error when trying to run test with only the junit-jupiter-api dependency above:
    Exception in thread “main” java.lang.NoClassDefFoundError: junit/textui/ResultPrinter

    Running IntelliJ IDEA 2016.2.1

    • Andrey Cheptsov says:

      August 3, 2016

      Thanks for your feedback. This bug will be fixed in 2016.2.2. Learn more: https://youtrack.jetbrains.com/issue/IDEA-158667

    • Mark Allison says:

      August 13, 2016

      A hacky work-around is to add JUnit 4 as a dependency as well:

      dependencies {
      testRuntime ‘junit:junit:4.12’
      testCompile ‘org.junit.jupiter:junit-jupiter-api:5.0.0-M2’
      testRuntime(“org.junit.jupiter:junit-jupiter-engine:5.0.0-M2”)
      }

  2. Jens Schauder says:

    September 9, 2016

    Is there a way to include/exclude tagged Tests?

    • Trisha Gee says:

      September 12, 2016

      You mean the tags described here? These can be used via Gradle, so you could run the tests in IntelliJ IDEA using the Gradle runner.

      I’m also investigating if there is a way to provide these tags as arguments to the IntelliJ IDEA test runner, I’ll get back to you when I find out.

      • Trisha Gee says:

        September 12, 2016

        IntelliJ IDEA doesn’t provide explicit support for including/excluding tests with the @Tag annotation, but it is on the roadmap.

        • Marcin Kunert says:

          May 25, 2018

          How is the support for running tagged tests looking right now. Is it already in the IDE?

  3. josh gruenberg says:

    September 28, 2016

    FYI, it appears IntelliJ doesn’t correctly handle tests generated via a @TestFactory method: the “DynamicTests” run, but failures are not reported in the console; instead, the TestFactory method is listed as “Empty test suite.”

    • josh gruenberg says:

      September 28, 2016

      Sorry, more accurately: if the @TestFactory method ITSELF throws an exception, this is treated as an empty test suite, with no indication of the problem.

      • josh gruenberg says:

        September 28, 2016

        Heh, actually, that isn’t quite accurate, either. I’ll post a new comment with a correct description of the problem…

  4. josh gruenberg says:

    September 28, 2016

    FYI, if a @TestFactory method returns a Stream, and traversing the Stream throws an exception, it appears IntelliJ doesn’t correctly handle this: the failure is not reported in the console; instead, the TestFactory method is listed as “Empty test suite.”

  5. Andre Greiner-Petter says:

    November 3, 2016

    I have a class with a bunch of @TestFactory methods. It runs smoothly in IntelliJ but IntelliJ don’t see @TestFactory as a test-class. (Warning: isn’t a test class).
    I know it’s a minor issue but it confused me a bit.
    IntelliJ IDEA 2016.2.5
    Build #IC-162.2228.15, built on October 14, 2016
    JRE: 1.8.0_112-b15 amd64
    JVM: Java HotSpot(TM) 64-Bit Server VM by Oracle Corporation

  6. says:

    November 15, 2016

    It seems I can’t run single tests only… or @Nested classes. While the latter would be really nice, the former actually prevents me from switching to Jupiter: I often set a breakpoint in my code and then start only a single test.

    • says:

      November 16, 2016

      I also have a Run Config using the pattern .*Test to run only my unit- but not my integration tests (that end with IT). IDEA doesn’t find _any_ tests, when I run it with the Jupiter runner on the classpath. This pretty much forces me to stick to JUnit 4 🙁

    • Anna Kozlova says:

      November 17, 2016

      This was fixed by junit team but the fix is not released yet.
      Anna

  7. James says:

    December 6, 2016

    With 2016.3, if my Maven projects uses version M3, it has throws NoSuchMethod errors due to a conflict with IntelliJ M2 jars. If I switch the version to M2 the tests run fine.

    org.junit.jupiter
    junit-jupiter-api
    5.0.0-M3
    test

    Is there a way to update 2016.3 to M3, and eventually the release version?

    • Andrey Cheptsov says:

      December 13, 2016

      2016.3.1 with support for M3 will be out within a couple of hours.

  8. Michal Klasinski says:

    December 9, 2016

    When you try to run maven project with basic junit5 tests from here:

    https://github.com/junit-team/junit5-samples/tree/master/junit5-maven-consumer

    Inteij 2016.3.7743.44 says No tests were found :(((

    With maven clean install it works fine, so it is clearly ide problem 🙁

  9. Michal Klasinski says:

    December 9, 2016

    Exception in thread “main” java.lang.NoSuchMethodError: org.junit.platform.commons.util.Preconditions.notNull([Ljava/lang/Object;Ljava/lang/String;)[Ljava/lang/Object;
    at org.junit.platform.launcher.core.DefaultLauncher.registerTestExecutionListeners(DefaultLauncher.java:71)
    at com.intellij.junit5.JUnit5IdeaTestRunner.startRunnerWithArgs(JUnit5IdeaTestRunner.java:44)
    at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:51)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:237)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)

    • Ivan says:

      June 13, 2017

      Could you solve it? Have the same issue…

      • TWiStErRob says:

        June 25, 2017

        Same here, I replaced SNAPSHOT/M5+ dependencies with M4 in my build.gradle and it worked:
        junitVintage : ‘4.12.0-M4’,
        junitPlatform: ‘1.0.0-M4’,
        junitJupiter : ‘5.0.0-M4’,

  10. Michal Klasinski says:

    December 9, 2016

    Adding this dep. seems to help out.

    org.junit.platform
    junit-platform-launcher
    1.0.0-M3

  11. Kevin Wittek says:

    December 9, 2016

    Are there any plans to support for the Gradle Test Runner when using JUnit5?

  12. Ernesto Maserati says:

    December 13, 2016

    I have IntelliJ 2016.3 and tried to run https://github.com/junit-team/junit5-samples/tree/r5.0.0-M3/junit5-maven-consumer in the IDE

    IntelliJ test runner plugin said “No tests found”, it did not work. 🙁

    • Mihai Anghel says:

      December 14, 2016

      Hi Ernesto. Try version 2016.3.1. It has been released yesterday and it does the trick

  13. Chuck C says:

    March 20, 2017

    I am using the latest EAP and it doesn’t seem to recognize @BeforeEach or @BeforeAll. Should this be working?

    • Andrey Cheptsov says:

      March 21, 2017

      A few questions. What EAP? How do you expect it to “recognize” them?

      • Chuck C says:

        June 5, 2017

        It looks like the Jupiter engine isn’t being called so methods with those annotations don’t get called before running tests. I am always using the latest EAP’s and it has never behaved like this blog where it calls out the Jupiter engine. I guess it still processes everything using the old Junit 4 facade.

        • Andrey Cheptsov says:

          June 6, 2017

          Could you please share the command line used by the IDE for running tests. You can find it inside the Run tool window. This might help us better understand the problem.

    • Jerome says:

      September 30, 2017

      Hi
      It wans’t working for me either until i changed this:
      import org.junit.Test;
      to:
      import org.junit.jupiter.api.Test;
      Maybe this helps someone googling for it.

      • Tom says:

        May 25, 2018

        Thank you!! It did!

  14. Neil says:

    April 4, 2017

    I am getting compilation warning
    Warning:java: unknown enum constant org.junit.platform.commons.meta.API.Usage.Stable
    reason: class file for org.junit.platform.commons.meta.API$Usage not found

    using
    IntelliJ IDEA 2017.1
    Build #IC-171.3780.107, built on March 22, 2017
    JRE: 1.8.0_112-release-736-b13 amd64
    JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
    Windows 7 6.1

  15. Nei says:

    April 5, 2017

    I am getting

    Warning:java: unknown enum constant org.junit.platform.commons.meta.API.Usage.Stable
    reason: class file for org.junit.platform.commons.meta.API$Usage not found

    IntelliJ IDEA 2017.1
    Build #IC-171.3780.107, built on March 22, 2017
    JRE: 1.8.0_112-release-736-b13 amd64
    JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
    Windows 7 6.1

  16. Morgan Kobeissi says:

    April 29, 2017

    The moment I read MoodAnalyzer I said, did Trisha write this? Username checks out 🙂

    Btw, for those using the latest JUnit release as of today 5.0.0-M4, make sure to upgrade to your IDE to 2017.1.2. https://blog.jetbrains.com/idea/2017/04/intellij-idea-2017-1-2-update-is-available/

    Support has just been added, if you don’t upgrade you’ll get the weird red “-” circular icon on your tests and an error in the event log that says “Failed to start: 0 passed, X not started”.

  17. Tiakon says:

    May 9, 2017

    I sometime run JUnit5 test, and I can see idea print the exception:
    Warning:java: unknown enumeration constant org.junit.platform.commons.meta.API.Usage.Stable reason: cannot find org.junit.platform.commons.meta.API$Usage class file( original: Warning:java: 未知的枚举常量 org.junit.platform.commons.meta.API.Usage.Stable 原因: 找不到org.junit.platform.commons.meta.API$Usage的类文件)

    using
    IntelliJ IDEA 2017.2
    java version “1.8.0_73”
    Java(TM) SE Runtime Environment (build 1.8.0_73-b02)
    Java HotSpot(TM) 64-Bit Server VM (build 25.73-b02, mixed mode)
    Windows 7

  18. Alex says:

    June 26, 2017

    Currently I am testing to use JUnit 5 Test suites and run them with IntelliJ (they work when run with Gradle).

    My test suite looks like:

    @RunWith(JUnitPlatform.class)
    @SelectClasses({LoggerExtensionTest.class, AnotherLoggerExtensionTest.class})
    public class LoggerExtensionTestSuite {
    }

    And my IDEA is:

    IntelliJ IDEA 2017.1.4
    Build #IC-171.4694.23, built on June 6, 2017
    JRE: 1.8.0_112-release-736-b21 x86_64
    JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
    Mac OS X 10.12.5

    But when I run the suite test I got:

    Jun 26, 2017 12:22:10 PM org.junit.vintage.engine.discovery.DefensiveAllDefaultPossibilitiesBuilder$DefensiveAnnotatedBuilder buildRunner
    WARNING: Ignoring test class using JUnitPlatform runner: LoggerExtensionTestSuite
    Jun 26, 2017 12:22:10 PM org.junit.vintage.engine.discovery.DefensiveAllDefaultPossibilitiesBuilder$DefensiveAnnotatedBuilder buildRunner
    WARNING: Ignoring test class using JUnitPlatform runner: LoggerExtensionTestSuite

    i have read that there were some problems with that in previous versions, but now I think I have a pretty new class.

    Any idea?

  19. laercio says:

    July 9, 2017

    I’m trying to run maven project with basic junit5 tests from:
    https://github.com/junit-team/junit5-samples/tree/master/junit5-maven-consumer

    IntelliJ IDEA 2017.1.5
    Build #IC-171.4694.70, built on July 4, 2017
    JRE: 1.8.0_112-release-736-b21 amd64
    JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
    Linux 4.4.0-78-generic

    And i get the follow error:


    Exception in thread “main” java.lang.NoSuchMethodError: org.junit.platform.commons.util.ReflectionUtils.getDefaultClassLoader()Ljava/lang/ClassLoader;
    at org.junit.platform.launcher.core.ServiceLoaderTestEngineRegistry.loadTestEngines(ServiceLoaderTestEngineRegistry.java:30)
    at org.junit.platform.launcher.core.LauncherFactory.create(LauncherFactory.java:53)
    at com.intellij.junit5.JUnit5IdeaTestRunner.createListeners(JUnit5IdeaTestRunner.java:39)
    at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:49)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)

    Process finished with exit code 1
    Empty test suite.

    With maven test in the command line it works fine

  20. Pawel says:

    August 8, 2017

    I’m trying to use @ParameterizedTest but it seems Intellij can’t find the annotation for some reason. I’ve chosen “Add Junit5 to the classpath” it seems that the only jupiter-api and opentest have been added. These don’t contain the @ParameterizedTest but that seems to be the case of all the jars in the //$APPLICATION_HOME_DIR$/plugins/junit/lib/ folder
    Any ideas how to work around the problem? Did I miss something? It’s not really that important but it’s quite annoying.
    Any help will be appreciated.

  21. Steven says:

    October 27, 2017

    Hello,
    is JUnit 5 running under IntelliJ 14?

    • Anna Kozlova says:

      October 27, 2017

      No, cause IDEA 14 was released in 2014 and junit 5 project started in late 2015.

  22. jiuhua.xu says:

    November 21, 2017

    I’m trying to run maven project with basic junit5 tests from:
    https://github.com/junit-team/junit5-samples.git

    IntelliJ IDEA 2017.1
    Build #IU-171.3780.170, built on March 22, 2017
    JRE: 1.8.0_112-release-736-b21 amd64
    JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
    Window10

    And i get the follow error:

    Exception in thread “main” java.lang.NoSuchMethodError: org.junit.platform.launcher.Launcher.execute(Lorg/junit/platform/launcher/LauncherDiscoveryRequest;)V
    at com.intellij.junit5.JUnit5IdeaTestRunner.startRunnerWithArgs(JUnit5IdeaTestRunner.java:61)
    at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:51)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:237)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)

  23. jiuhua xu says:

    November 22, 2017

    I update idea to 2017.2.6, it works well.

  24. Marcel Overdijk says:

    November 30, 2017

    When running my junit5 tests from Intellij 2017.3 I see a lot of warnings like:

    Nov 30, 2017 10:07:58 PM org.junit.platform.commons.util.ClasspathScanner logGenericFileProcessingException
    WARNING: Failed to load java.lang.Class for path [/com/intellij/rt/execution/junit/FileComparisonFailure.class] during classpath scanning.
    java.lang.NoClassDefFoundError: junit/framework/ComparisonFailure
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClass(ClassLoader.java:763)
    at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
    at java.net.URLClassLoader.defineClass(URLClassLoader.java:467)
    at java.net.URLClassLoader.access$100(URLClassLoader.java:73)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:368)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:362)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:361)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:335)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    at org.junit.platform.commons.util.ReflectionUtils.loadClass(ReflectionUtils.java:460)
    at org.junit.platform.commons.util.ClasspathScanner.processClassFileSafely(ClasspathScanner.java:141)
    at org.junit.platform.commons.util.ClasspathScanner.lambda$findClassesForPath$3(ClasspathScanner.java:126)
    at org.junit.platform.commons.util.ClassFileVisitor.visitFile(ClassFileVisitor.java:45)
    at org.junit.platform.commons.util.ClassFileVisitor.visitFile(ClassFileVisitor.java:28)
    at java.nio.file.Files.walkFileTree(Files.java:2670)
    at java.nio.file.Files.walkFileTree(Files.java:2742)
    at org.junit.platform.commons.util.ClasspathScanner.findClassesForPath(ClasspathScanner.java:126)
    at org.junit.platform.commons.util.ClasspathScanner.findClassesForUri(ClasspathScanner.java:110)
    at org.junit.platform.commons.util.ClasspathScanner.lambda$findClassesForUris$0(ClasspathScanner.java:99)
    at java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:193)
    at java.util.ArrayList$ArrayListSpliterator.forEachRemaining(ArrayList.java:1374)
    at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:481)
    at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:471)
    at java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:708)
    at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
    at java.util.stream.ReferencePipeline.collect(ReferencePipeline.java:499)
    at org.junit.platform.commons.util.ClasspathScanner.findClassesForUris(ClasspathScanner.java:102)
    at org.junit.platform.commons.util.ClasspathScanner.scanForClassesInPackage(ClasspathScanner.java:78)
    at org.junit.platform.commons.util.ReflectionUtils.findAllClassesInPackage(ReflectionUtils.java:640)
    at org.junit.jupiter.engine.discovery.DiscoverySelectorResolver.lambda$resolveSelectors$1(DiscoverySelectorResolver.java:55)
    at java.util.ArrayList.forEach(ArrayList.java:1249)
    at org.junit.jupiter.engine.discovery.DiscoverySelectorResolver.resolveSelectors(DiscoverySelectorResolver.java:54)
    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)
    Caused by: java.lang.ClassNotFoundException: junit.framework.ComparisonFailure
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:335)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    … 44 more

    But the tests are green.

    All these warnings are very annoying and I’m wondering if I mis-configured something.

  25. Someone says:

    April 18, 2018

    If you are not using gradle how do you add junit as a dependency? It tried to add it in the dependencies tab of my project but no luck…

  26. Andrei says:

    July 16, 2018

    IntelliJ IDEA Ultimate 2018.1.6
    Warning for Junit5 tests: “JUnit test method contains no assertions”.
    https://i.imgur.com/v7BL17Z.png

  27. moomin says:

    October 17, 2018

    What the hell makes you write the configuration text in a image that I CAN’T copy the text from!!!!

    • Trisha Gee says:

      October 18, 2018

      Apologies, I agree that’s not useful at all.

      Was it the dependency code that you wanted? That’s currently:

      testImplementation (‘org.junit.jupiter:junit-jupiter-api:5.3.1’)

  28. crizzis says:

    July 22, 2019

    Just a small issue: the Show/Hide Ignored button in the Run tab doesn’t work when @Disabled is used together with @ParameterizedTest (the test is marked as ‘successful’ rather than ‘ignored’)

Subscribe

Subscribe for updates