'Y'

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.

CustomerTest tagged class.JPG


The @Tag annotation is added on the whole CustomerTest class (1).

CustomerRepositoryTest tagged class.JPG


Similarly, the @Tag annotation is added on the whole CustomerRepositoryTest class (1).

pom.xml configuration file.JPG


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).

Configuring the tagged tests from the IntelliJ IDEA IDE.jpg

Figure 2 Configuring the tagged tests from the IntelliJ IDEA IDE

Conclusions

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.


Catalin Tudose
Java and Web Technologies Expert



Share the knowledge

Luxoft Warsaw - Warsaw Spire, plac Europejski 1, 00-844 Warszawa
Dimitrie Pompeiu nr 5-7 , building C, Et. 5, sect 2, Bucharest, 014459

Contact phone:

021 371 4858
Luxoft Poland Wroclaw - Silver Tower pl. Konstytucji 3-go Maja 3 50-048 Wroclaw
Aleja Generała Tadeusza Bora-Komorowskiego 25, Quattro Business Park Five, 31-476 Kraków, Poland

Contact phone:

+48 122110650
Success
Thank you.
Your request has been received.