In our environment we are using JUnit 4 TestListener
s to report test results to a remote server.
What is the JUnit 5 way doing that?
This listener can be used for various purposes from improved logging to test specific logic. 1. JUnit RunListener Example 1.1. JUnit test classes We are writing two test classes below for example only.
Using JUnit 5 test suites, you can run tests spread into multiple test classes and different packages. JUnit 5 provides two annotations: @SelectPackages and @SelectClasses to create test suites. Additionally, you can use other annotations for filtering test packages, classes or even test methods.
This JUnit Listener can listen to events of JUnit lifecycle. We can add this listener by creating a custom Runner. Then we use this runner with the @RunWith annotation which will register our JUnit Listener to the test case. We can extend the org.junit.runner.notification.RunListener class and override the listener methods to implement.
Just add the @Suite annotation of a class and start including or excluding the test classes and methods into the suite. When we want to run the suite, simply run it as a normal JUnit test class and it will execute all the included tests in the suite. 3.2. @SuiteDisplayName
You may use a TestExecutionListener
:
http://junit.org/junit5/docs/current/user-guide/#launcher-api-listeners-custom
7.1.4. Plugging in Your Own Test Execution Listeners
In addition to the public Launcher API method for registering test execution listeners programmatically, custom
TestExecutionListener
implementations discovered at runtime via Java’sjava.util.ServiceLoader
facility are automatically registered with theDefaultLauncher
.
"JUnit 5", read the JUnit Platform, ships some example listeners. You can view their source here:
https://github.com/junit-team/junit5/tree/HEAD/junit-platform-launcher/src/main/java/org/junit/platform/launcher/listeners
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With