With JUnit 5 we can use the new Tag annotations to choose which test should be executed: tests can be tagged as “Simple”, “Slow”, “RequiringNetwork”, “RequiringDatabase”, or any other string we desire, and we can select which tag include or exclude in test execution.

screenshot-2018-10-08-2231-001.pngUsed technologies:

  • Java 8
  • JUnit 5.3
  • Maven 3
  • maven-surefire-plugin 2.22.0

The complete project could be found at GitHub: https://github.com/caladyon/BethelwhiteSamples/tree/master/junittrial

Project setup

I’ve created a simple Maven project that has a single Java class, called StringEcho, and few JUnit test classes:

  • UntaggedStringEchoTest, without any tag;
  • TaggedStringEchoTest, with the “OtherTest” tag;
  • FailingStringEchoTest, whose tests fail and is tagged as “WrongTest”;
  • Tagged2StringEchoTest, with both “OtherTest” and “WrongTest” tags (on the two methods, with different ordering)

The pom.xml file is quite straightforward:

  • a single dependency: junit-jupiter-engine
  • a single plugin: maven-surefire-plugin

I’ve added a number of profiles to help executing the tests with different maven-surefire-plugin’s configurations.

Tests

I ran the tests with various configuration and watched the executed tests.

mvn test

All tests were executed: tags are just ignored.

mvn test -P exclude

UntaggedStringEchoTest and TaggedStringEchoTest were executed: the other 2 suites have the excluded tag.

mvn test -P include

TaggedStringEchoTest and Tagged2StringEchoTest were executed: they are the only tests that have the included tags (the fact that Tagged2StringEchoTest has another tags doesn’t count).

mvn test -P include_exclude

TaggedStringEchoTest and Tagged2StringEchoTest were executed this time as well: it seems that the “groups” element has priority over the “excludedGroups” one.

WARNING: this behavior is the opposite of the ‘s one! In that case only TaggedStringEchoTest is executed: the exclude element seems to have priority over the include one.

mvn test -P exclude_include

The same result as above: the order of groups/excludedGroups elements in configuration doesn’t change the test behaviour.

A note

The Surefire plugin doesn’t need any extra dependency: starting with version 2.22.0, you don’t need junit-platform-surefire-provider anymore.

If this dependency remains, a warning message is printed:

WARNING:
The junit-platform-surefire-provider has been deprecated and is scheduled to
be removed in JUnit Platform 1.4. Please use the built-in support in Maven
Surefire >= 2.22.0 instead.
https://junit.org/junit5/docs/current/user-guide/#running-tests-build-maven

And remember that the configuration tags for test filtering are changed:

  • includeTags ==> groups
  • excludeTags ==> excludedGroups