Skip to content

Commit a29f45a

Browse files
alb-i986kcooney
authored andcommitted
Tests expecting AssumptionViolatedException should be marked as passed, not skipped (#1291)
Tests annotated with `@Test(expected = AssumptionViolatedException.class)` which throw AssumptionViolatedException should be marked as passing, not skipped. Fixes #1290
1 parent a58d459 commit a29f45a

File tree

4 files changed

+106
-1
lines changed

4 files changed

+106
-1
lines changed

src/main/java/org/junit/internal/runners/statements/ExpectException.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@ public void evaluate() throws Exception {
1919
next.evaluate();
2020
complete = true;
2121
} catch (AssumptionViolatedException e) {
22-
throw e;
22+
if (!expected.isAssignableFrom(e.getClass())) {
23+
throw e;
24+
}
2325
} catch (Throwable e) {
2426
if (!expected.isAssignableFrom(e.getClass())) {
2527
String message = "Unexpected exception, expected<"

src/test/java/org/junit/internal/AllInternalTests.java

+2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import org.junit.internal.matchers.StacktracePrintingMatcherTest;
55
import org.junit.internal.matchers.ThrowableCauseMatcherTest;
66
import org.junit.internal.runners.ErrorReportingRunnerTest;
7+
import org.junit.internal.runners.statements.ExpectExceptionTest;
78
import org.junit.internal.runners.statements.FailOnTimeoutTest;
89
import org.junit.runner.RunWith;
910
import org.junit.runners.Suite;
@@ -13,6 +14,7 @@
1314
@SuiteClasses({
1415
AnnotatedBuilderTest.class,
1516
ErrorReportingRunnerTest.class,
17+
ExpectExceptionTest.class,
1618
FailOnTimeoutTest.class,
1719
MethodSorterTest.class,
1820
StacktracePrintingMatcherTest.class,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
package org.junit.internal.runners.statements;
2+
3+
import org.junit.Test;
4+
import org.junit.internal.AssumptionViolatedException;
5+
import org.junit.runners.model.Statement;
6+
7+
import static org.hamcrest.CoreMatchers.containsString;
8+
import static org.hamcrest.CoreMatchers.equalTo;
9+
import static org.junit.Assert.assertThat;
10+
import static org.junit.Assert.fail;
11+
12+
/**
13+
* Integration tests can be found in {@link org.junit.tests.running.methods.ExpectedTest}.
14+
* See e.g. {@link org.junit.tests.running.methods.ExpectedTest#expectsAssumptionViolatedException()}
15+
*/
16+
public class ExpectExceptionTest {
17+
18+
@Test
19+
public void whenExpectingAssumptionViolatedExceptionStatementsThrowingItShouldPass() {
20+
Statement delegate = new Fail(new AssumptionViolatedException("expected"));
21+
ExpectException expectException = new ExpectException(delegate, AssumptionViolatedException.class);
22+
23+
try {
24+
expectException.evaluate();
25+
// then AssumptionViolatedException should not be thrown
26+
} catch (Throwable e) { // need to explicitly catch and re-throw as an AssertionError or it might be skipped
27+
fail("should not throw anything, but was thrown: " + e);
28+
}
29+
}
30+
31+
@Test
32+
public void whenExpectingAssumptionViolatedExceptionStatementsThrowingSubclassShouldPass() {
33+
Statement delegate = new Fail(new AssumptionViolatedExceptionSubclass("expected"));
34+
ExpectException expectException = new ExpectException(delegate, AssumptionViolatedException.class);
35+
36+
try {
37+
expectException.evaluate();
38+
// then no exception should be thrown
39+
} catch (Throwable e) {
40+
fail("should not throw anything, but was thrown: " + e);
41+
}
42+
}
43+
44+
@Test
45+
public void whenExpectingAssumptionViolatedExceptionStatementsThrowingDifferentExceptionShouldFail() {
46+
Statement delegate = new Fail(new SomeException("not expected"));
47+
ExpectException expectException = new ExpectException(delegate, AssumptionViolatedException.class);
48+
49+
try {
50+
expectException.evaluate();
51+
fail("should throw 'Unexpected exception' when statement throws an exception which is not the one expected");
52+
} catch (Exception e) {
53+
assertThat(e.getMessage(), equalTo("Unexpected exception, expected<org.junit.internal.AssumptionViolatedException> " +
54+
"but was<org.junit.internal.runners.statements.ExpectExceptionTest$SomeException>"));
55+
}
56+
}
57+
58+
@Test
59+
public void whenExpectingAssumptionViolatedExceptionStatementsPassingShouldFail() throws Exception {
60+
ExpectException expectException = new ExpectException(new PassingStatement(), AssumptionViolatedException.class);
61+
62+
try {
63+
expectException.evaluate();
64+
} catch (AssertionError e) {
65+
assertThat(e.getMessage(), containsString("Expected exception: " + AssumptionViolatedException.class.getName()));
66+
return;
67+
}
68+
fail("ExpectException should throw when the given statement passes");
69+
}
70+
71+
private static class PassingStatement extends Statement {
72+
public void evaluate() throws Throwable {
73+
// nop
74+
}
75+
}
76+
77+
private static class SomeException extends RuntimeException {
78+
public SomeException(String message) {
79+
super(message);
80+
}
81+
}
82+
83+
private static class AssumptionViolatedExceptionSubclass extends AssumptionViolatedException {
84+
public AssumptionViolatedExceptionSubclass(String assumption) {
85+
super(assumption);
86+
}
87+
}
88+
}

src/test/java/org/junit/tests/running/methods/ExpectedTest.java

+13
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import static org.junit.Assert.assertTrue;
66

77
import org.junit.Test;
8+
import org.junit.internal.AssumptionViolatedException;
89
import org.junit.runner.JUnitCore;
910
import org.junit.runner.Result;
1011
import org.junit.runner.notification.Failure;
@@ -67,4 +68,16 @@ public void throwsSubclass() {
6768
public void expectsSuperclass() {
6869
assertTrue(new JUnitCore().run(ExpectSuperclass.class).wasSuccessful());
6970
}
71+
72+
public static class ExpectAssumptionViolatedException {
73+
@Test(expected = AssumptionViolatedException.class)
74+
public void throwsAssumptionViolatedException() {
75+
throw new AssumptionViolatedException("expected");
76+
}
77+
}
78+
79+
@Test
80+
public void expectsAssumptionViolatedException() {
81+
assertTrue(new JUnitCore().run(ExpectAssumptionViolatedException.class).wasSuccessful());
82+
}
7083
}

0 commit comments

Comments
 (0)