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 #494
  • Loading branch information
kcooney committed Sep 9, 2014
1 parent 5408162 commit daa46e4
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 3 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) {
}
}
}
}
}
41 changes: 40 additions & 1 deletion src/test/java/org/junit/AssumptionViolatedExceptionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public void toStringReportsValue(Object actual, Matcher<?> matcher) {
}

@Test
public void AssumptionViolatedExceptionDescribesItself() {
public void assumptionViolatedExceptionWithMatcherDescribesItself() {
AssumptionViolatedException e = new AssumptionViolatedException(3, is(2));
assertThat(StringDescription.asString(e), is("got: <3>, expected: is <2>"));
}
Expand All @@ -48,4 +48,43 @@ public void simpleAssumptionViolatedExceptionDescribesItself() {
AssumptionViolatedException e = new AssumptionViolatedException("not enough money");
assertThat(StringDescription.asString(e), is("not enough money"));
}

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

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

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

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

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

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

0 comments on commit daa46e4

Please sign in to comment.