Skip to content

Commit e1315c7

Browse files
committed
Merge pull request #928 from kcooney/test-watcher-internal-assumption-violation
Add overload of TestWatcher.skipped() that uses the external AssumptionViolationException
2 parents 0e64f5c + e12bb77 commit e1315c7

File tree

5 files changed

+68
-9
lines changed

5 files changed

+68
-9
lines changed

src/main/java/org/junit/rules/Stopwatch.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package org.junit.rules;
22

3-
import org.junit.internal.AssumptionViolatedException;
3+
import org.junit.AssumptionViolatedException;
44
import org.junit.runner.Description;
55

66
import java.util.concurrent.TimeUnit;

src/main/java/org/junit/rules/TestWatcher.java

+24-4
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import java.util.ArrayList;
44
import java.util.List;
55

6-
import org.junit.internal.AssumptionViolatedException;
6+
import org.junit.AssumptionViolatedException;
77
import org.junit.runner.Description;
88
import org.junit.runners.model.MultipleFailureException;
99
import org.junit.runners.model.Statement;
@@ -54,7 +54,7 @@ public void evaluate() throws Throwable {
5454
try {
5555
base.evaluate();
5656
succeededQuietly(description, errors);
57-
} catch (AssumptionViolatedException e) {
57+
} catch (@SuppressWarnings("deprecation") org.junit.internal.AssumptionViolatedException e) {
5858
errors.add(e);
5959
skippedQuietly(e, description, errors);
6060
} catch (Throwable e) {
@@ -87,10 +87,16 @@ private void failedQuietly(Throwable e, Description description,
8787
}
8888
}
8989

90-
private void skippedQuietly(AssumptionViolatedException e, Description description,
90+
@SuppressWarnings("deprecation")
91+
private void skippedQuietly(
92+
org.junit.internal.AssumptionViolatedException e, Description description,
9193
List<Throwable> errors) {
9294
try {
93-
skipped(e, description);
95+
if (e instanceof AssumptionViolatedException) {
96+
skipped((AssumptionViolatedException) e, description);
97+
} else {
98+
skipped(e, description);
99+
}
94100
} catch (Throwable e1) {
95101
errors.add(e1);
96102
}
@@ -129,7 +135,21 @@ protected void failed(Throwable e, Description description) {
129135
/**
130136
* Invoked when a test is skipped due to a failed assumption.
131137
*/
138+
@SuppressWarnings("deprecation")
132139
protected void skipped(AssumptionViolatedException e, Description description) {
140+
// For backwards compatibility with JUnit 4.11 and earlier, call the legacy version
141+
org.junit.internal.AssumptionViolatedException asInternalException = e;
142+
skipped(asInternalException, description);
143+
}
144+
145+
/**
146+
* Invoked when a test is skipped due to a failed assumption.
147+
*
148+
* @deprecated use {@link #skipped(AssumptionViolatedException, Description)}
149+
*/
150+
@Deprecated
151+
protected void skipped(
152+
org.junit.internal.AssumptionViolatedException e, Description description) {
133153
}
134154

135155
/**

src/test/java/org/junit/tests/experimental/rules/LoggingTestWatcher.java

+6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package org.junit.tests.experimental.rules;
22

3+
import org.junit.AssumptionViolatedException;
34
import org.junit.rules.TestWatcher;
45
import org.junit.runner.Description;
56

@@ -20,6 +21,11 @@ protected void failed(Throwable e, Description description) {
2021
log.append("failed ");
2122
}
2223

24+
@Override
25+
protected void skipped(AssumptionViolatedException e, Description description) {
26+
log.append("skipped ");
27+
}
28+
2329
@Override
2430
protected void starting(Description description) {
2531
log.append("starting ");

src/test/java/org/junit/tests/experimental/rules/StopwatchTest.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
package org.junit.tests.experimental.rules;
22

3+
import org.junit.AssumptionViolatedException;
34
import org.junit.Before;
45
import org.junit.Rule;
56
import org.junit.Test;
6-
import org.junit.internal.AssumptionViolatedException;
77
import org.junit.rules.Stopwatch;
88
import org.junit.runner.Description;
99
import org.junit.runner.JUnitCore;
1010
import org.junit.runner.Result;
11-
1211
import static java.util.concurrent.TimeUnit.MILLISECONDS;
1312
import static org.hamcrest.core.Is.is;
1413
import static org.junit.Assert.assertEquals;

src/test/java/org/junit/tests/experimental/rules/TestWatcherTest.java

+36-2
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,41 @@ public void neitherLogSuccessNorFailedForViolatedAssumption() {
3434
ViolatedAssumptionTest.watchedLog = new StringBuilder();
3535
runClasses(ViolatedAssumptionTest.class);
3636
assertThat(ViolatedAssumptionTest.watchedLog.toString(),
37-
is("starting finished "));
37+
is("starting skipped finished "));
38+
}
39+
40+
public static class InternalViolatedAssumptionTest {
41+
private static StringBuilder watchedLog = new StringBuilder();
42+
43+
@Rule
44+
public TestRule watcher = new TestWatcher() {
45+
@Override
46+
protected void starting(Description description) {
47+
watchedLog.append("starting ");
48+
}
49+
50+
@Override
51+
protected void finished(Description description) {
52+
watchedLog.append("finished ");
53+
}
54+
55+
protected void skipped(AssumptionViolatedException e, Description description) {
56+
watchedLog.append("skipped ");
57+
}
58+
};
59+
60+
@Test
61+
public void succeeds() {
62+
throw new AssumptionViolatedException("don't run");
63+
}
64+
}
65+
66+
@Test
67+
public void internalViolatedAssumption() {
68+
InternalViolatedAssumptionTest.watchedLog = new StringBuilder();
69+
runClasses(InternalViolatedAssumptionTest.class);
70+
assertThat(InternalViolatedAssumptionTest.watchedLog.toString(),
71+
is("starting skipped finished "));
3872
}
3973

4074
public static class TestWatcherSkippedThrowsExceptionTest {
@@ -154,4 +188,4 @@ public void testWatcherFailedAndFinishedThrowsException() {
154188
assertThat(result, hasFailureContaining("watcher failed failure"));
155189
assertThat(result, hasFailureContaining("watcher finished failure"));
156190
}
157-
}
191+
}

0 commit comments

Comments
 (0)