How to use JUnit 5 in IntelliJ IDEA

There is an interesting update in the recent IntelliJ IDEA 2016.2: support for the new JUnit 5 testing framework. Almost all Java developers use JUnit at some point, so it's exciting to see a new version that brings a familiar set of features similar to other frameworks.

IntelliJ IDEA supports running tests written in JUnit 5 without the need for additional libraries (such as Gradle or Maven plugins), the only thing you have to do is add the JUnit 5 dependencies. Here are our  dependencies for adding Gradle in JUnit 5 M2 version:

After doing the above, you can write the following test using the new annotation:

Running these tests yields a result similar to the run window in IntelliJ IDEA:

You will be able to see that JUnit Jupiter becomes the engine in the new JUnit test, through which you can run both old and new tests.

At first glance, the new JUnit 5 is no different from JUnit 4, except that the annotations are imported from different packages and the result is a different type of runner.

But in fact JUnit 5 also added some new features, such as the ability to add a readable name or description to the test, so after using @DisplayName , you no longer need to rely on the class function name to describe.

You can now make group assertions, thus giving you more realistic visibility into failures whether there is one or more failures.

You can also annotate with @Nested , so you can write BDD-style tests or group tests with different dimensions in the same class:

JUnit 5还有一种处理预期异常的新方法,通过对抛出的特定异常进行断言:

你甚至可以获取所抛出异常的更深入细节:

这里只是在JUnit 5使用的部分特性,而IntelliJ对于这个新的框架的支持也在提升。

如果你对JUnit 5感兴趣,可以下载IntelliJ IDEA 2016.2并看一看以下的资源:

 更多关于IntelliJ IDEA 2016.2特性的详细介绍>>

 

本文翻译自dzone.com

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326683121&siteId=291194637