Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the JUnit 5 way for RunListeners

Tags:

junit5

In our environment we are using JUnit 4 TestListeners to report test results to a remote server.

What is the JUnit 5 way doing that?

like image 937
mibutec Avatar asked Feb 03 '18 07:02

mibutec


People also ask

What is the use of runlistener In JUnit?

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.

What is a JUnit 5 test suite?

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.

How to listen to events of JUnit lifecycle?

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.

How do I run a JUnit test from a class?

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


1 Answers

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’s java.util.ServiceLoader facility are automatically registered with the DefaultLauncher.

"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

like image 71
Sormuras Avatar answered Sep 18 '22 06:09

Sormuras