Skip to content

Commit

Permalink
Add Result#getAssumptionFailureCount
Browse files Browse the repository at this point in the history
Fix unit test assumeWithExpectedException: it was throwing AssumptionViolatedException thus being skipped (see #98).
  • Loading branch information
alb-i986 committed May 13, 2016
1 parent 54d6e94 commit c4c8ebd
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
15 changes: 14 additions & 1 deletion src/main/java/org/junit/runner/Result.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public class Result implements Serializable {
ObjectStreamClass.lookup(SerializedForm.class).getFields();
private final AtomicInteger count;
private final AtomicInteger ignoreCount;
private final AtomicInteger assumptionFailureCount;
private final CopyOnWriteArrayList<Failure> failures;
private final AtomicLong runTime;
private final AtomicLong startTime;
Expand All @@ -38,6 +39,7 @@ public class Result implements Serializable {
public Result() {
count = new AtomicInteger();
ignoreCount = new AtomicInteger();
assumptionFailureCount = new AtomicInteger();
failures = new CopyOnWriteArrayList<Failure>();
runTime = new AtomicLong();
startTime = new AtomicLong();
Expand All @@ -46,6 +48,7 @@ public Result() {
private Result(SerializedForm serializedForm) {
count = serializedForm.fCount;
ignoreCount = serializedForm.fIgnoreCount;
assumptionFailureCount = serializedForm.assumptionFailureCount;
failures = new CopyOnWriteArrayList<Failure>(serializedForm.fFailures);
runTime = new AtomicLong(serializedForm.fRunTime);
startTime = new AtomicLong(serializedForm.fStartTime);
Expand Down Expand Up @@ -86,6 +89,13 @@ public int getIgnoreCount() {
return ignoreCount.get();
}

/**
* @return the number of tests skipped because of an assumption failure
*/
public int getAssumptionFailureCount() {
return assumptionFailureCount.get();
}

/**
* @return <code>true</code> if all tests succeeded
*/
Expand Down Expand Up @@ -137,7 +147,7 @@ public void testIgnored(Description description) throws Exception {

@Override
public void testAssumptionFailure(Failure failure) {
// do nothing: same as passing (for 4.5; may change in 4.6)
assumptionFailureCount.getAndIncrement();
}
}

Expand All @@ -156,13 +166,15 @@ private static class SerializedForm implements Serializable {
private static final long serialVersionUID = 1L;
private final AtomicInteger fCount;
private final AtomicInteger fIgnoreCount;
private final AtomicInteger assumptionFailureCount;
private final List<Failure> fFailures;
private final long fRunTime;
private final long fStartTime;

public SerializedForm(Result result) {
fCount = result.count;
fIgnoreCount = result.ignoreCount;
assumptionFailureCount = result.assumptionFailureCount;
fFailures = Collections.synchronizedList(new ArrayList<Failure>(result.failures));
fRunTime = result.runTime.longValue();
fStartTime = result.startTime.longValue();
Expand All @@ -172,6 +184,7 @@ public SerializedForm(Result result) {
private SerializedForm(ObjectInputStream.GetField fields) throws IOException {
fCount = (AtomicInteger) fields.get("fCount", null);
fIgnoreCount = (AtomicInteger) fields.get("fIgnoreCount", null);
assumptionFailureCount = (AtomicInteger) fields.get("assumptionFailureCount", null);
fFailures = (List<Failure>) fields.get("fFailures", null);
fRunTime = fields.get("fRunTime", 0L);
fStartTime = fields.get("fStartTime", 0L);
Expand Down
15 changes: 12 additions & 3 deletions src/test/java/org/junit/tests/experimental/AssumptionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -188,9 +188,18 @@ public void failingAssumptionInConstructorIgnoresClass() {
assertThat(testResult(AssumptionFailureInConstructor.class), isSuccessful());
}

@Test(expected = IllegalArgumentException.class)
public void assumeWithExpectedException() {
assumeTrue(false);
public static class TestClassWithAssumptionFailure {

@Test(expected = IllegalArgumentException.class)
public void assumeWithExpectedException() {
assumeTrue(false);
}
}

@Test
public void assumeWithExpectedExceptionShouldThrowAssumptionViolatedException() {
Result result = JUnitCore.runClasses(TestClassWithAssumptionFailure.class);
assertThat(result.getAssumptionFailureCount(), is(1));
}

final static String message = "Some random message string.";
Expand Down

0 comments on commit c4c8ebd

Please sign in to comment.