Skip to content

Commit

Permalink
Change AssumptionViolatedException to not set the cause to null; fixe…
Browse files Browse the repository at this point in the history
…s issue junit-team#494
  • Loading branch information
kcooney committed Sep 6, 2014
1 parent 5408162 commit 58fb4f0
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,14 @@ public class AssumptionViolatedException extends RuntimeException implements Sel
private final Matcher<?> matcher;

public AssumptionViolatedException(String assumption, boolean valueMatcher, Object value, Matcher<?> matcher) {
super(value instanceof Throwable ? (Throwable) value : null);
this.assumption = assumption;
this.value = value;
this.matcher = matcher;
this.valueMatcher = valueMatcher;

if (value instanceof Throwable) {
initCause((Throwable) value);
}
}

/**
Expand Down Expand Up @@ -87,4 +90,4 @@ public void describeTo(Description description) {
}
}
}
}
}
37 changes: 37 additions & 0 deletions src/test/java/org/junit/AssumptionViolatedExceptionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.hamcrest.CoreMatchers.nullValue;
import static org.junit.Assert.assertThat;
import static org.junit.Assume.assumeThat;

Expand Down Expand Up @@ -48,4 +49,40 @@ public void simpleAssumptionViolatedExceptionDescribesItself() {
AssumptionViolatedException e = new AssumptionViolatedException("not enough money");
assertThat(StringDescription.asString(e), is("not enough money"));
}
@Test
public void nullCause() {
AssumptionViolatedException e = new AssumptionViolatedException("invalid number");
assertThat(e.getCause(), nullValue());
}

@Test
public void causeWithObjectAndMatcher() {
Throwable testObject = new Exception();
AssumptionViolatedException e = new AssumptionViolatedException(testObject, containsString("test matcher"));
assertThat(e.getCause(), is(testObject));
}

@Test
public void causeWithAssumptionObjectAndMatcher() {
Throwable testObject = new Exception();
AssumptionViolatedException e = new AssumptionViolatedException(
"sample assumption", testObject, containsString("test matcher"));

assertThat(e.getCause(), is(testObject));
}

@Test
public void causeWithMainConstructor() {
Throwable testObject = new Exception();
AssumptionViolatedException e = new AssumptionViolatedException(
"sample assumption", false, testObject, containsString("test matcher"));
assertThat(e.getCause(), is(testObject));
}

@Test
public void causeWithExplicitThrowableConstructor() {
Throwable cause = new Exception();
AssumptionViolatedException e = new AssumptionViolatedException("invalid number", cause);
assertThat(e.getCause(), is(cause));
}
}

0 comments on commit 58fb4f0

Please sign in to comment.