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.

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

    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?

  9. Michal Klasinski says:

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

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

  11. Michal Klasinski says:

    Adding this dep. seems to help out.

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

  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 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. :(

  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

    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

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

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

    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

  19. Alex says:

    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?

  20. laercio says:

    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

  21. Pawel says:

    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.

Leave a Reply

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