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.

28 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?

Leave a Reply

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