'Y'

JUnit 5 features

The third and last article of our three part serieson JUnit. This time we look at its features.


6.Using the new JUnit 5 features

The JUnit 5 introduces new annotations, from which we are detailing a few ones.

@DisplayName
  • It can be used over class and test method.
  • It declares a custom display name for the annotated test class or test method.
  • It is typically used for test reporting in IDEs and build tools
  • It may contain spaces, special characters, and even emoji.

DisplayName_JUnit.JPG

The result of running this test should look like this:

DisplayName_JUnit_results.png

@Nested

Nested tests give the test writer more capabilities to express the relationship among several group of tests. It is a replacement for Enclosed rule, and part of an experimental API.

Inner classes can be:
  • Package private
  • Non-static
nested_JUnit.JPG
nested_JUnit_1.JPG
nested_JUnit_2.JPG


The result of running these tests is:

Nested_tests_JUnit.png

Conclusions

JUnit 5 comes with a new architecture and with new concepts and annotations. It has addressed the shortcomings of JUnit 4 and provided the developer the possibility to write more expressive and well organized hierarchy of tests. We have explored the most important news, including here the new architecture and the most important newly introduced annotations. The reader should have an overall image of the new capabilities and, from here, new steps can be made in the search of what JUnit 5 can provide. From what this article does not analyze, assertions, assumptions, dynamic and parameterized tests may be the next logical areas to investigate.

Interested in JUnit? Check out our trainings.


Catalin Tudose
Java and Web Technologies Expert

Share the knowledge

Still have questions?
Connect with us
Thank you.
Your request has been received.