Skip to content

Commit f5dda83

Browse files
authored
Merge pull request #1530 from kcooney/fix-test-assumeWithExpectedException
Add Result#getAssumptionFailureCount
2 parents 0038617 + eb75886 commit f5dda83

8 files changed

+125
-16
lines changed

src/main/java/org/junit/runner/Result.java

+26-5
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ public class Result implements Serializable {
2828
ObjectStreamClass.lookup(SerializedForm.class).getFields();
2929
private final AtomicInteger count;
3030
private final AtomicInteger ignoreCount;
31+
private final AtomicInteger assumptionFailureCount;
3132
private final CopyOnWriteArrayList<Failure> failures;
3233
private final AtomicLong runTime;
3334
private final AtomicLong startTime;
@@ -38,6 +39,7 @@ public class Result implements Serializable {
3839
public Result() {
3940
count = new AtomicInteger();
4041
ignoreCount = new AtomicInteger();
42+
assumptionFailureCount = new AtomicInteger();
4143
failures = new CopyOnWriteArrayList<Failure>();
4244
runTime = new AtomicLong();
4345
startTime = new AtomicLong();
@@ -46,34 +48,35 @@ public Result() {
4648
private Result(SerializedForm serializedForm) {
4749
count = serializedForm.fCount;
4850
ignoreCount = serializedForm.fIgnoreCount;
51+
assumptionFailureCount = serializedForm.assumptionFailureCount;
4952
failures = new CopyOnWriteArrayList<Failure>(serializedForm.fFailures);
5053
runTime = new AtomicLong(serializedForm.fRunTime);
5154
startTime = new AtomicLong(serializedForm.fStartTime);
5255
}
5356

5457
/**
55-
* @return the number of tests run
58+
* Returns the number of tests run
5659
*/
5760
public int getRunCount() {
5861
return count.get();
5962
}
6063

6164
/**
62-
* @return the number of tests that failed during the run
65+
* Returns the number of tests that failed during the run
6366
*/
6467
public int getFailureCount() {
6568
return failures.size();
6669
}
6770

6871
/**
69-
* @return the number of milliseconds it took to run the entire suite to run
72+
* Returns the number of milliseconds it took to run the entire suite to run
7073
*/
7174
public long getRunTime() {
7275
return runTime.get();
7376
}
7477

7578
/**
76-
* @return the {@link Failure}s describing tests that failed and the problems they encountered
79+
* Returns the {@link Failure}s describing tests that failed and the problems they encountered
7780
*/
7881
public List<Failure> getFailures() {
7982
return failures;
@@ -86,6 +89,20 @@ public int getIgnoreCount() {
8689
return ignoreCount.get();
8790
}
8891

92+
/**
93+
* Returns the number of tests skipped because of an assumption failure
94+
*
95+
* @throws UnsupportedOperationException if the result was serialized in a version before JUnit 4.13
96+
* @since 4.13
97+
*/
98+
public int getAssumptionFailureCount() {
99+
if (assumptionFailureCount == null) {
100+
throw new UnsupportedOperationException(
101+
"Result was serialized from a version of JUnit that doesn't support this method");
102+
}
103+
return assumptionFailureCount.get();
104+
}
105+
89106
/**
90107
* @return <code>true</code> if all tests succeeded
91108
*/
@@ -137,7 +154,7 @@ public void testIgnored(Description description) throws Exception {
137154

138155
@Override
139156
public void testAssumptionFailure(Failure failure) {
140-
// do nothing: same as passing (for 4.5; may change in 4.6)
157+
assumptionFailureCount.getAndIncrement();
141158
}
142159
}
143160

@@ -156,13 +173,15 @@ private static class SerializedForm implements Serializable {
156173
private static final long serialVersionUID = 1L;
157174
private final AtomicInteger fCount;
158175
private final AtomicInteger fIgnoreCount;
176+
private final AtomicInteger assumptionFailureCount;
159177
private final List<Failure> fFailures;
160178
private final long fRunTime;
161179
private final long fStartTime;
162180

163181
public SerializedForm(Result result) {
164182
fCount = result.count;
165183
fIgnoreCount = result.ignoreCount;
184+
assumptionFailureCount = result.assumptionFailureCount;
166185
fFailures = Collections.synchronizedList(new ArrayList<Failure>(result.failures));
167186
fRunTime = result.runTime.longValue();
168187
fStartTime = result.startTime.longValue();
@@ -172,6 +191,7 @@ public SerializedForm(Result result) {
172191
private SerializedForm(ObjectInputStream.GetField fields) throws IOException {
173192
fCount = (AtomicInteger) fields.get("fCount", null);
174193
fIgnoreCount = (AtomicInteger) fields.get("fIgnoreCount", null);
194+
assumptionFailureCount = (AtomicInteger) fields.get("assumptionFailureCount", null);
175195
fFailures = (List<Failure>) fields.get("fFailures", null);
176196
fRunTime = fields.get("fRunTime", 0L);
177197
fStartTime = fields.get("fStartTime", 0L);
@@ -184,6 +204,7 @@ public void serialize(ObjectOutputStream s) throws IOException {
184204
fields.put("fFailures", fFailures);
185205
fields.put("fRunTime", fRunTime);
186206
fields.put("fStartTime", fStartTime);
207+
fields.put("assumptionFailureCount", assumptionFailureCount);
187208
s.writeFields();
188209
}
189210

src/test/java/junit/tests/runner/ResultTest.java

+87-8
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import java.io.ByteArrayInputStream;
44
import java.io.ByteArrayOutputStream;
5+
import java.io.File;
6+
import java.io.FileOutputStream;
57
import java.io.IOException;
68
import java.io.InputStream;
79
import java.io.ObjectInputStream;
@@ -10,25 +12,69 @@
1012

1113
import junit.framework.TestCase;
1214
import junit.tests.framework.Success;
15+
import org.junit.Test;
1316
import org.junit.runner.JUnitCore;
1417
import org.junit.runner.Result;
1518
import org.junit.runner.notification.Failure;
1619
import org.junit.tests.running.methods.AnnotationTest;
1720

1821
public class ResultTest extends TestCase {
1922

23+
private Result fromStream;
24+
2025
public void testRunFailureResultCanBeSerialised() throws Exception {
2126
JUnitCore runner = new JUnitCore();
2227
Result result = runner.run(AnnotationTest.FailureTest.class);
2328
assertResultSerializable(result);
2429
}
2530

31+
public void testRunFailureResultCanBeReserialised_v4_12() throws Exception {
32+
JUnitCore runner = new JUnitCore();
33+
Result result = runner.run(AnnotationTest.FailureTest.class);
34+
assertResultReserializable(result, SerializationFormat.V4_12);
35+
}
36+
37+
public void testRunAssumptionFailedResultCanBeSerialised() throws Exception {
38+
JUnitCore runner = new JUnitCore();
39+
Result result = runner.run(AssumptionFailedTest.class);
40+
assertResultSerializable(result);
41+
}
42+
43+
public void testRunAssumptionFailedResultCanBeReserialised_v4_12() throws Exception {
44+
JUnitCore runner = new JUnitCore();
45+
Result result = runner.run(AssumptionFailedTest.class);
46+
assertResultReserializable(result, SerializationFormat.V4_12);
47+
}
48+
49+
public void testRunAssumptionFailedResultCanBeReserialised_v4_13() throws Exception {
50+
JUnitCore runner = new JUnitCore();
51+
Result result = runner.run(AssumptionFailedTest.class);
52+
assertResultReserializable(result, SerializationFormat.V4_13);
53+
}
54+
2655
public void testRunSuccessResultCanBeSerialised() throws Exception {
2756
JUnitCore runner = new JUnitCore();
2857
Result result = runner.run(Success.class);
2958
assertResultSerializable(result);
3059
}
3160

61+
public void testRunSuccessResultCanBeReserialised_v4_12() throws Exception {
62+
JUnitCore runner = new JUnitCore();
63+
Result result = runner.run(Success.class);
64+
assertResultReserializable(result, SerializationFormat.V4_12);
65+
}
66+
67+
public void testRunSuccessResultCanBeReserialised_v4_13() throws Exception {
68+
JUnitCore runner = new JUnitCore();
69+
Result result = runner.run(Success.class);
70+
assertResultReserializable(result, SerializationFormat.V4_13);
71+
}
72+
73+
private enum SerializationFormat {
74+
V4_12,
75+
V4_13
76+
}
77+
3278
private void assertResultSerializable(Result result) throws IOException, ClassNotFoundException {
3379
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
3480
ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);
@@ -37,14 +83,26 @@ private void assertResultSerializable(Result result) throws IOException, ClassNo
3783
byte[] bytes = byteArrayOutputStream.toByteArray();
3884
ObjectInputStream objectInputStream = new ObjectInputStream(new ByteArrayInputStream(bytes));
3985
Result fromStream = (Result) objectInputStream.readObject();
40-
assertSerializedCorrectly(result, fromStream);
41-
42-
InputStream resource = getClass().getResourceAsStream(getName());
43-
assertNotNull("Could not read resource " + getName(), resource);
44-
objectInputStream = new ObjectInputStream(resource);
86+
assertSerializedCorrectly(result, fromStream, SerializationFormat.V4_13);
87+
}
88+
89+
private void assertResultReserializable(Result result, SerializationFormat resourceSerializationFormat)
90+
throws IOException, ClassNotFoundException {
91+
String resourceName = getName();
92+
InputStream resource = getClass().getResourceAsStream(resourceName);
93+
assertNotNull("Could not read resource " + resourceName, resource);
94+
ObjectInputStream objectInputStream = new ObjectInputStream(resource);
4595
fromStream = (Result) objectInputStream.readObject();
46-
47-
assertSerializedCorrectly(new ResultWithFixedRunTime(result), fromStream);
96+
97+
assertSerializedCorrectly(new ResultWithFixedRunTime(result),
98+
fromStream, resourceSerializationFormat);
99+
}
100+
101+
static public class AssumptionFailedTest {
102+
@Test
103+
public void assumptionFailed() throws Exception {
104+
org.junit.Assume.assumeTrue(false);
105+
}
48106
}
49107

50108
/**
@@ -85,14 +143,35 @@ public List<Failure> getFailures() {
85143
public int getIgnoreCount() {
86144
return delegate.getIgnoreCount();
87145
}
146+
147+
@Override
148+
public int getAssumptionFailureCount() {
149+
return delegate.getAssumptionFailureCount();
150+
}
88151
}
89152

90-
private void assertSerializedCorrectly(Result result, Result fromStream) {
153+
private void assertSerializedCorrectly(
154+
Result result, Result fromStream, SerializationFormat serializationFormat) {
91155
assertNotNull(fromStream);
92156

93157
// Exceptions don't implement equals() so we need to compare field by field
94158
assertEquals("failureCount", result.getFailureCount(), fromStream.getFailureCount());
95159
assertEquals("ignoreCount", result.getIgnoreCount(), fromStream.getIgnoreCount());
160+
161+
if (serializationFormat == SerializationFormat.V4_13) {
162+
// assumption failures are serialized
163+
assertEquals("assumptionFailureCount",
164+
result.getAssumptionFailureCount(),
165+
fromStream.getAssumptionFailureCount());
166+
} else {
167+
// assumption failures were not serialized
168+
try {
169+
fromStream.getAssumptionFailureCount();
170+
fail("UnsupportedOperationException expected");
171+
} catch (UnsupportedOperationException expected) {
172+
}
173+
}
174+
96175
assertEquals("runTime", result.getRunTime(), fromStream.getRunTime());
97176
assertEquals("failures", result.getFailures().size(), fromStream.getFailures().size());
98177
int index = 0;

src/test/java/org/junit/tests/experimental/AssumptionTest.java

+12-3
Original file line numberDiff line numberDiff line change
@@ -213,9 +213,18 @@ public void failingAssumptionInConstructorIgnoresClass() {
213213
assertThat(testResult(AssumptionFailureInConstructor.class), isSuccessful());
214214
}
215215

216-
@Test(expected = IllegalArgumentException.class)
217-
public void assumeWithExpectedException() {
218-
assumeTrue(false);
216+
public static class TestClassWithAssumptionFailure {
217+
218+
@Test(expected = IllegalArgumentException.class)
219+
public void assumeWithExpectedException() {
220+
assumeTrue(false);
221+
}
222+
}
223+
224+
@Test
225+
public void assumeWithExpectedExceptionShouldThrowAssumptionViolatedException() {
226+
Result result = JUnitCore.runClasses(TestClassWithAssumptionFailure.class);
227+
assertThat(result.getAssumptionFailureCount(), is(1));
219228
}
220229

221230
final static String message = "Some random message string.";
Binary file not shown.

0 commit comments

Comments
 (0)