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