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:
Once you’ve done this, you can start writing tests that use the new annotations:
Running these tests will give you familiar-looking results in the IntelliJ IDEA run window:
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
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:
And you can use the
@Nested annotation, so you can write BDD-style tests or group the tests in a class by some dimension:
JUnit 5 also has a new way of handling expected Exceptions, by letting you assert specific Exceptions are thrown
And you can even get the thrown Exception to check for further details:
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:
- The JUnit 5 User Guide, for reference and examples
- JUnit 5 – An Early Test Drive for a walkthrough of the new features
- The vJUG session on JUnit 5.
And, of course, you can check out more of the features available in IntelliJ IDEA 2016.2.
Subscribe to Blog updates
Thanks, we've got you!
Import Postman Collections to the HTTP Client
We know that many of you use Postman collections and want to stay in the flow while working with them. This means staying inside IntelliJ IDEA and not switching to a different app. Great news! You can now effortlessly transform Postman collections into .http and use them in the HTTP Client …
What’s New in IntelliJ IDEA 2023.1 for Spring Developers
The IntelliJ IDEA 2023.1 release came packed with updates for Spring. This blog post will highlight the most noticeable changes, including full support for Lombok, a reworked Spring tool window, navigation for Spring Security rules, Spring Security 6 support, and more. Remember that these featur…
‘Quick’ Features in IntelliJ IDEA
A few IntelliJ IDEA features or actions include the term ‘quick’. Do they speed up certain processes? Let’s find out. In this blog post, I’ll cover what these features are, why you need them, and how you can use them. 1. Quick Lists If you have actions that you use regularly but don’t have shortcut…
5 Best Features for Profiling Java Code In IntelliJ IDEA
When it comes to apps functioning efficiently, there is always room for improvement. Fast, lag-free, smooth operation – what else can a Java developer dream of? However, when you aim for this level of perfection in your app, you will need a comprehensive toolset to monitor performance and identif…