Skip to content

Commit 85c570e

Browse files
author
dsaff
committed
Failed assumptions in @before and @BeforeClass cause the test to pass
1 parent d322fe6 commit 85c570e

File tree

6 files changed

+62
-13
lines changed

6 files changed

+62
-13
lines changed

README.html

+4-1
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,9 @@ <h3>assumeThat</h3>
168168
those functionalities are subsumed in assumeThat, with the appropriate
169169
matcher.</p>
170170

171+
<p>A failing assumption in a <code>@Before</code> or <code>@BeforeClass</code> method will have the same effect
172+
as a failing assumption in each <code>@Test</code> method of the class.</p>
173+
171174
<h3>Theories</h3>
172175

173176
<p><a name="theories" />
@@ -239,7 +242,7 @@ <h3>Other changes</h3>
239242
<code>JUnit3ClassRunner</code>. The new design allows variations in running
240243
individual test classes to be expressed with fewer custom classes.
241244
For a good example, see the source to
242-
<code>org.junit.experimental.theories.runner.api.Theories</code>.</p></li>
245+
<code>org.junit.experimental.theories.Theories</code>.</p></li>
243246
<li><p>The rules for determining which runner is applied by default to a
244247
test class have been simplified:</p>
245248

doc/ReleaseNotes4.4.html

+4-1
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,9 @@ <h3>Assumptions</h3>
123123
those functionalities are subsumed in <code>assumeThat</code>, with the appropriate
124124
matcher.</p>
125125

126+
<p>A failing assumption in a <code>@Before</code> or <code>@BeforeClass</code> method will have the same effect
127+
as a failing assumption in each <code>@Test</code> method of the class.</p>
128+
126129
<p><a name="theories" /></p>
127130

128131
<h3>Theories</h3>
@@ -197,7 +200,7 @@ <h3>Other changes</h3>
197200
<code>JUnit3ClassRunner</code>. The new design allows variations in running
198201
individual test classes to be expressed with fewer custom classes.
199202
For a good example, see the source to
200-
<code>org.junit.experimental.theories.runner.api.Theories</code>.</p></li>
203+
<code>org.junit.experimental.theories.Theories</code>.</p></li>
201204
<li><p>The rules for determining which runner is applied by default to a
202205
test class have been simplified:</p>
203206

doc/ReleaseNotes4.4.txt

+4-1
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,9 @@ inclusion of Hamcrest, we do not need to create `assumeEquals`,
131131
those functionalities are subsumed in `assumeThat`, with the appropriate
132132
matcher.
133133

134+
A failing assumption in a `@Before` or `@BeforeClass` method will have the same effect
135+
as a failing assumption in each `@Test` method of the class.
136+
134137
<a name="theories" />
135138
### Theories ###
136139

@@ -209,7 +212,7 @@ This release contains other bug fixes and new features. Among them:
209212
`JUnit3ClassRunner`. The new design allows variations in running
210213
individual test classes to be expressed with fewer custom classes.
211214
For a good example, see the source to
212-
`org.junit.experimental.theories.runner.api.Theories`.
215+
`org.junit.experimental.theories.Theories`.
213216

214217
- The rules for determining which runner is applied by default to a
215218
test class have been simplified:

src/org/junit/internal/runners/ClassRoadie.java

+9-5
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import java.lang.reflect.Method;
55
import java.util.List;
66

7+
import org.junit.Assume.AssumptionViolatedException;
78
import org.junit.runner.Description;
89
import org.junit.runner.notification.Failure;
910
import org.junit.runner.notification.RunNotifier;
@@ -42,11 +43,14 @@ public void runProtected() {
4243

4344
private void runBefores() throws FailedBefore {
4445
try {
45-
List<Method> befores= fTestClass.getBefores();
46-
for (Method before : befores)
47-
before.invoke(null);
48-
} catch (InvocationTargetException e) {
49-
addFailure(e.getTargetException());
46+
try {
47+
List<Method> befores= fTestClass.getBefores();
48+
for (Method before : befores)
49+
before.invoke(null);
50+
} catch (InvocationTargetException e) {
51+
throw e.getTargetException();
52+
}
53+
} catch (AssumptionViolatedException e) {
5054
throw new FailedBefore();
5155
} catch (Throwable e) {
5256
addFailure(e);

src/org/junit/internal/runners/MethodRoadie.java

+8-5
Original file line numberDiff line numberDiff line change
@@ -116,11 +116,14 @@ else if (fTestMethod.isUnexpected(actual)) {
116116

117117
private void runBefores() throws FailedBefore {
118118
try {
119-
List<Method> befores= fTestMethod.getBefores();
120-
for (Method before : befores)
121-
before.invoke(fTest);
122-
} catch (InvocationTargetException e) {
123-
addFailure(e.getTargetException());
119+
try {
120+
List<Method> befores= fTestMethod.getBefores();
121+
for (Method before : befores)
122+
before.invoke(fTest);
123+
} catch (InvocationTargetException e) {
124+
throw e.getTargetException();
125+
}
126+
} catch (AssumptionViolatedException e) {
124127
throw new FailedBefore();
125128
} catch (Throwable e) {
126129
addFailure(e);

src/org/junit/tests/AssumptionTest.java

+33
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,14 @@
55
import static org.junit.Assume.assumeNoException;
66
import static org.junit.Assume.assumeNotNull;
77
import static org.junit.Assume.assumeThat;
8+
import static org.junit.Assume.assumeTrue;
9+
import static org.junit.experimental.results.PrintableResult.testResult;
10+
import static org.junit.experimental.results.ResultMatchers.isSuccessful;
811
import static org.junit.matchers.StringContains.containsString;
912

1013
import org.junit.Assume;
14+
import org.junit.Before;
15+
import org.junit.BeforeClass;
1116
import org.junit.Test;
1217
import org.junit.Assume.AssumptionViolatedException;
1318
import org.junit.runner.JUnitCore;
@@ -106,4 +111,32 @@ private void assertCompletesNormally() {
106111
@Test(expected=AssumptionViolatedException.class) public void assumeTrueWorks() {
107112
Assume.assumeTrue(false);
108113
}
114+
115+
public static class HasFailingAssumeInBefore {
116+
@Before public void checkForSomethingThatIsntThere() {
117+
assumeTrue(false);
118+
}
119+
120+
@Test public void failing() {
121+
fail();
122+
}
123+
}
124+
125+
@Test public void failingAssumptionInBeforePreventsTestRun() {
126+
assertThat(testResult(HasFailingAssumeInBefore.class), isSuccessful());
127+
}
128+
129+
public static class HasFailingAssumeInBeforeClass {
130+
@BeforeClass public static void checkForSomethingThatIsntThere() {
131+
assumeTrue(false);
132+
}
133+
134+
@Test public void failing() {
135+
fail();
136+
}
137+
}
138+
139+
@Test public void failingAssumptionInBeforeClassPreventsTestRun() {
140+
assertThat(testResult(HasFailingAssumeInBeforeClass.class), isSuccessful());
141+
}
109142
}

0 commit comments

Comments
 (0)