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.
annotation is added on the whole CustomerTest
Similarly, the @Tag
annotation is added on the whole CustomerRepositoryTest
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
. 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