Using JUnit 5 in IntelliJ IDEA

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.

This entry was posted in New Features and tagged , , , . Bookmark the permalink.

36 Responses to Using JUnit 5 in IntelliJ IDEA

  1. Dmitrij says:

    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

  2. Is there a way to include/exclude tagged Tests?

  3. josh gruenberg says:

    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:

      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:

        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:

    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. Srinidi says:

    Thank you for the update on JUnit 5. I was waiting for the article and wanted to learn more about JUnit 5.This article helped me a lot.

  6. Andre Greiner-Petter says:

    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

  7. says:

    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.

  8. James says:

    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.


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

  9. Michal Klasinski says:

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

    Inteij 2016.3.7743.44 says No tests were found :(((

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

  10. Michal Klasinski says:

    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(
    at com.intellij.junit5.JUnit5IdeaTestRunner.startRunnerWithArgs(
    at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(
    at com.intellij.rt.execution.junit.JUnitStarter.main(
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(
    at java.lang.reflect.Method.invoke(
    at com.intellij.rt.execution.application.AppMain.main(

  11. Michal Klasinski says:

    Adding this dep. seems to help out.


  12. Kevin Wittek says:

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

  13. Ernesto Maserati says:

    I have IntelliJ 2016.3 and tried to run in the IDE

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

  14. Chuck C says:

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

    • Andrey Cheptsov says:

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

      • Chuck C says:

        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:

          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.

  15. Neil says:

    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

    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. Nei says:

    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

  17. 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.

    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”.

  18. Tiakon says:

    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的类文件)

    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

Leave a Reply

Your email address will not be published. Required fields are marked *