Skip to content

Commit 2cc8260

Browse files
committed
add suite events to listener
1 parent 387e851 commit 2cc8260

File tree

6 files changed

+145
-4
lines changed

6 files changed

+145
-4
lines changed

Diff for: src/main/java/org/junit/internal/runners/model/EachTestNotifier.java

+18
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,22 @@ public void fireTestStarted() {
4545
public void fireTestIgnored() {
4646
notifier.fireTestIgnored(description);
4747
}
48+
49+
/**
50+
* Notify {@link #notifier} that a test suite is about to start.
51+
* @see RunNotifier#fireTestSuiteStarted(Description)
52+
* @since 4.13
53+
*/
54+
public void fireTestSuiteStarted() {
55+
notifier.fireTestSuiteStarted(description);
56+
}
57+
58+
/**
59+
* Notify {@link #notifier} that a test suite is about to finish.
60+
* @see RunNotifier#fireTestSuiteFinished(Description)
61+
* @since 4.13
62+
*/
63+
public void fireTestSuiteFinished() {
64+
notifier.fireTestSuiteFinished(description);
65+
}
4866
}

Diff for: src/main/java/org/junit/runner/notification/RunListener.java

+19
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,25 @@ public void testRunStarted(Description description) throws Exception {
6969
public void testRunFinished(Result result) throws Exception {
7070
}
7171

72+
/**
73+
* Called when test suite is about to be started.
74+
*
75+
* @param description the description of the test suite that is about to be run
76+
* (generally a class name)
77+
* @since 4.13
78+
*/
79+
public void testSuiteStarted(Description description) throws Exception {
80+
}
81+
82+
/**
83+
* Called when test suite has finished, whether the test suite succeeds or fails.
84+
*
85+
* @param description the description of the test suite that just ran
86+
* @since 4.13
87+
*/
88+
public void testSuiteFinished(Description description) throws Exception {
89+
}
90+
7291
/**
7392
* Called when an atomic test is about to be started.
7493
*

Diff for: src/main/java/org/junit/runner/notification/RunNotifier.java

+32
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,38 @@ protected void notifyListener(RunListener each) throws Exception {
105105
}.run();
106106
}
107107

108+
/**
109+
* Invoke to tell listeners that a test suite is about to start.
110+
*
111+
* @param description the description of the suite test (generally a class name)
112+
* @since 4.13
113+
*/
114+
public void fireTestSuiteStarted(final Description description) {
115+
new SafeNotifier() {
116+
@Override
117+
protected void notifyListener(RunListener each) throws Exception {
118+
each.testSuiteStarted(description);
119+
}
120+
}.run();
121+
}
122+
123+
/**
124+
* Invoke to tell listeners that a test suite is about to finish. Always invoke
125+
* this method if you invoke {@link #fireTestSuiteStarted(Description)}
126+
* as listeners are likely to expect them to come in pairs.
127+
*
128+
* @param description the description of the suite test (generally a class name)
129+
* @since 4.13
130+
*/
131+
public void fireTestSuiteFinished(final Description description) {
132+
new SafeNotifier() {
133+
@Override
134+
protected void notifyListener(RunListener each) throws Exception {
135+
each.testSuiteFinished(description);
136+
}
137+
}.run();
138+
}
139+
108140
/**
109141
* Invoke to tell listeners that an atomic test is about to start.
110142
*

Diff for: src/main/java/org/junit/runner/notification/SynchronizedRunListener.java

+31
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,37 @@ public void testRunFinished(Result result) throws Exception {
4343
}
4444
}
4545

46+
/**
47+
* Synchronized decorator for {@link RunListener#testSuiteStarted(Description)}.
48+
* @param description the description of the test suite that is about to be run
49+
* (generally a class name)
50+
* @throws Exception if any occurs.
51+
* @since 4.13
52+
*
53+
* @see RunListener#testSuiteStarted(Description)
54+
*/
55+
@Override
56+
public void testSuiteStarted(Description description) throws Exception {
57+
synchronized (monitor) {
58+
listener.testSuiteStarted(description);
59+
}
60+
}
61+
62+
/**
63+
* Synchronized decorator for {@link RunListener#testSuiteFinished(Description)}.
64+
* @param description the description of the test suite that just ran
65+
* @throws Exception
66+
* @since 4.13
67+
*
68+
* @see RunListener#testSuiteFinished(Description)
69+
*/
70+
@Override
71+
public void testSuiteFinished(Description description) throws Exception {
72+
synchronized (monitor) {
73+
listener.testSuiteFinished(description);
74+
}
75+
}
76+
4677
@Override
4778
public void testStarted(Description description) throws Exception {
4879
synchronized (monitor) {

Diff for: src/main/java/org/junit/runners/ParentRunner.java

+3
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,7 @@ public Description getDescription() {
358358
public void run(final RunNotifier notifier) {
359359
EachTestNotifier testNotifier = new EachTestNotifier(notifier,
360360
getDescription());
361+
testNotifier.fireTestSuiteStarted();
361362
try {
362363
Statement statement = classBlock(notifier);
363364
statement.evaluate();
@@ -367,6 +368,8 @@ public void run(final RunNotifier notifier) {
367368
throw e;
368369
} catch (Throwable e) {
369370
testNotifier.addFailure(e);
371+
} finally {
372+
testNotifier.fireTestSuiteFinished();
370373
}
371374
}
372375

Diff for: src/test/java/org/junit/tests/running/classes/ParentRunnerTest.java

+42-4
Original file line numberDiff line numberDiff line change
@@ -167,9 +167,14 @@ public void test() {}
167167
@Test
168168
public void assertionErrorAtParentLevelTest() throws InitializationError {
169169
CountingRunListener countingRunListener = runTestWithParentRunner(AssertionErrorAtParentLevelTest.class);
170+
Assert.assertEquals(1, countingRunListener.testSuiteStarted);
171+
Assert.assertEquals(1, countingRunListener.testSuiteFinished);
172+
Assert.assertEquals(1, countingRunListener.testSuiteFailure);
173+
Assert.assertEquals(0, countingRunListener.testSuiteAssumptionFailure);
174+
170175
Assert.assertEquals(0, countingRunListener.testStarted);
171176
Assert.assertEquals(0, countingRunListener.testFinished);
172-
Assert.assertEquals(1, countingRunListener.testFailure);
177+
Assert.assertEquals(0, countingRunListener.testFailure);
173178
Assert.assertEquals(0, countingRunListener.testAssumptionFailure);
174179
Assert.assertEquals(0, countingRunListener.testIgnored);
175180
}
@@ -188,10 +193,15 @@ public void test() {}
188193
@Test
189194
public void assumptionViolatedAtParentLevel() throws InitializationError {
190195
CountingRunListener countingRunListener = runTestWithParentRunner(AssumptionViolatedAtParentLevelTest.class);
196+
Assert.assertEquals(1, countingRunListener.testSuiteStarted);
197+
Assert.assertEquals(1, countingRunListener.testSuiteFinished);
198+
Assert.assertEquals(0, countingRunListener.testSuiteFailure);
199+
Assert.assertEquals(1, countingRunListener.testSuiteAssumptionFailure);
200+
191201
Assert.assertEquals(0, countingRunListener.testStarted);
192202
Assert.assertEquals(0, countingRunListener.testFinished);
193203
Assert.assertEquals(0, countingRunListener.testFailure);
194-
Assert.assertEquals(1, countingRunListener.testAssumptionFailure);
204+
Assert.assertEquals(0, countingRunListener.testAssumptionFailure);
195205
Assert.assertEquals(0, countingRunListener.testIgnored);
196206
}
197207

@@ -218,6 +228,11 @@ public void assumptionFail() {
218228
@Test
219229
public void parentRunnerTestMethods() throws InitializationError {
220230
CountingRunListener countingRunListener = runTestWithParentRunner(TestTest.class);
231+
Assert.assertEquals(1, countingRunListener.testSuiteStarted);
232+
Assert.assertEquals(1, countingRunListener.testSuiteFinished);
233+
Assert.assertEquals(0, countingRunListener.testSuiteFailure);
234+
Assert.assertEquals(0, countingRunListener.testSuiteAssumptionFailure);
235+
221236
Assert.assertEquals(3, countingRunListener.testStarted);
222237
Assert.assertEquals(3, countingRunListener.testFinished);
223238
Assert.assertEquals(1, countingRunListener.testFailure);
@@ -235,12 +250,27 @@ private CountingRunListener runTestWithParentRunner(Class<?> testClass) throws I
235250
}
236251

237252
private static class CountingRunListener extends RunListener {
253+
private int testSuiteStarted = 0;
254+
private int testSuiteFinished = 0;
255+
private int testSuiteFailure = 0;
256+
private int testSuiteAssumptionFailure = 0;
257+
238258
private int testStarted = 0;
239259
private int testFinished = 0;
240260
private int testFailure = 0;
241261
private int testAssumptionFailure = 0;
242262
private int testIgnored = 0;
243263

264+
@Override
265+
public void testSuiteStarted(Description description) throws Exception {
266+
testSuiteStarted++;
267+
}
268+
269+
@Override
270+
public void testSuiteFinished(Description description) throws Exception {
271+
testSuiteFinished++;
272+
}
273+
244274
@Override
245275
public void testStarted(Description description) throws Exception {
246276
testStarted++;
@@ -253,12 +283,20 @@ public void testFinished(Description description) throws Exception {
253283

254284
@Override
255285
public void testFailure(Failure failure) throws Exception {
256-
testFailure++;
286+
if (failure.getDescription().isSuite()) {
287+
testSuiteFailure++;
288+
} else {
289+
testFailure++;
290+
}
257291
}
258292

259293
@Override
260294
public void testAssumptionFailure(Failure failure) {
261-
testAssumptionFailure++;
295+
if (failure.getDescription().isSuite()) {
296+
testSuiteAssumptionFailure++;
297+
} else {
298+
testAssumptionFailure++;
299+
}
262300
}
263301

264302
@Override

0 commit comments

Comments
 (0)