1 package org.junit.runner.notification; 2 3 import org.junit.internal.AssumptionViolatedException; 4 import org.junit.runner.Description; 5 import org.junit.runner.Result; 6 7 /** 8 * <p>If you need to respond to the events during a test run, extend <code>RunListener</code> 9 * and override the appropriate methods. If a listener throws an exception while processing a 10 * test event, it will be removed for the remainder of the test run.</p> 11 * 12 * <p>For example, suppose you have a <code>Cowbell</code> 13 * class that you want to make a noise whenever a test fails. You could write: 14 * <pre> 15 * public class RingingListener extends RunListener { 16 * public void testFailure(Failure failure) { 17 * Cowbell.ring(); 18 * } 19 * } 20 * </pre> 21 * </p> 22 * 23 * <p>To invoke your listener, you need to run your tests through <code>JUnitCore</code>. 24 * <pre> 25 * public void main(String... args) { 26 * JUnitCore core= new JUnitCore(); 27 * core.addListener(new RingingListener()); 28 * core.run(MyTestClass.class); 29 * } 30 * </pre> 31 * </p> 32 * @see org.junit.runner.JUnitCore 33 */ 34 public class RunListener { 35 36 /** 37 * Called before any tests have been run. 38 * @param description describes the tests to be run 39 */ testRunStarted(Description description)40 public void testRunStarted(Description description) throws Exception { 41 } 42 43 /** 44 * Called when all tests have finished 45 * @param result the summary of the test run, including all the tests that failed 46 */ testRunFinished(Result result)47 public void testRunFinished(Result result) throws Exception { 48 } 49 50 /** 51 * Called when an atomic test is about to be started. 52 * @param description the description of the test that is about to be run 53 * (generally a class and method name) 54 */ testStarted(Description description)55 public void testStarted(Description description) throws Exception { 56 } 57 58 /** 59 * Called when an atomic test has finished, whether the test succeeds or fails. 60 * @param description the description of the test that just ran 61 */ testFinished(Description description)62 public void testFinished(Description description) throws Exception { 63 } 64 65 /** 66 * Called when an atomic test fails. 67 * @param failure describes the test that failed and the exception that was thrown 68 */ testFailure(Failure failure)69 public void testFailure(Failure failure) throws Exception { 70 } 71 72 /** 73 * Called when an atomic test flags that it assumes a condition that is 74 * false 75 * 76 * @param failure 77 * describes the test that failed and the 78 * {@link AssumptionViolatedException} that was thrown 79 */ testAssumptionFailure(Failure failure)80 public void testAssumptionFailure(Failure failure) { 81 } 82 83 /** 84 * Called when a test will not be run, generally because a test method is annotated 85 * with {@link org.junit.Ignore}. 86 * 87 * @param description describes the test that will not be run 88 */ testIgnored(Description description)89 public void testIgnored(Description description) throws Exception { 90 } 91 } 92 93 94