Register an instance of this class with {@link RunNotifier} to be notifiedof events that occur during a test run. All of the methods in this class are abstract and have no implementation; override one or more methods to receive events.
For example, suppose you have a Cowbell
class that you want to make a noise whenever a test fails. You could write:
public class RingingListener extends RunListener { public void testFailure(Failure failure) { Cowbell.ring(); } }
To invoke your listener, you need to run your tests through JUnitCore
.
public void main(String... args) { JUnitCore core= new JUnitCore(); core.addListener(new RingingListener()); core.run(MyTestClass.class); }
If a listener throws an exception for a test event, the other listeners will have their {@link RunListener#testFailure(Failure)} called with a {@code Description}of {@link Description#TEST_MECHANISM} to indicate the failure.
By default, JUnit will synchronize calls to your listener. If your listener is thread-safe and you want to allow JUnit to call your listener from multiple threads when tests are run in parallel, you can annotate your test class with {@link RunListener.ThreadSafe}.
Listener methods will be called from the same thread as is running the test, unless otherwise indicated by the method Javadoc
@see org.junit.runner.JUnitCore
@since 4.0