Newly-Introduced JUnit 5 Annotations and Classes. Tagged tests
The last article in our series on Newly-Introduced JUnit 5 Annotations and Classes. In this one we look at tagged tests.
4. Tagged tests
Tagged tests represent a replacement for the JUnit 4 Categories. The @Tag annotation can be used over classes and test methods. Tags can later be used to filter test discovery and execution. Listing 5 presents the CustomerTest tagged class, which tests the correct creation of the customers. A use case may be to group your tests into a few categories, based on the business logic and on those things you are effectively testing. Each tests category has its own particular tag. Then, you decide which tests to run, or alternate between running different categories, depending on the current needs.
The @Tag annotation is added on the whole CustomerTest class (1).
Similarly, the @Tag annotation is added on the whole CustomerRepositoryTest class (1).
In order to activate the usage of the tags, you’ve a few alternatives. One way to do it’s to work at the level of the pom.xml configuration file. In listing 7, it’s enough to uncomment the configuration node of the surfire plugin (1) and run mvn clean install.
Another alternative is from the IntelliJ IDEA IDE: you can activate the usage of the tags by going to Run -> Edit Configurations and choose Tags (JUnit 5) as test kind (figure 2).
Figure 2 Configuring the tagged tests from the IntelliJ IDEA IDE
JUnit 5 comes with a lot of new annotations that make the life of the programmer easier and nicer. We investigated the capabilities of a few of them: @DisplayName, @Disabled, @Nested and @Tag. The examples provided in this article will allow you to understand how to use them in your current work.
Interested in JUnit? Check out our trainings.
Java and Web Technologies Expert
Originally published at https://www.luxoft-training.com.