From daa46e409f29cd21f9b3a9036fd858c3c21e564f Mon Sep 17 00:00:00 2001 From: Kevin Cooney Date: Sat, 6 Sep 2014 10:29:40 -0700 Subject: [PATCH] Change AssumptionViolatedException to not set the cause to null; fixes issue #494 --- .../internal/AssumptionViolatedException.java | 7 +++- .../AssumptionViolatedExceptionTest.java | 41 ++++++++++++++++++- 2 files changed, 45 insertions(+), 3 deletions(-) diff --git a/src/main/java/org/junit/internal/AssumptionViolatedException.java b/src/main/java/org/junit/internal/AssumptionViolatedException.java index b1e364faf4c0..89c6aa886227 100644 --- a/src/main/java/org/junit/internal/AssumptionViolatedException.java +++ b/src/main/java/org/junit/internal/AssumptionViolatedException.java @@ -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); + } } /** @@ -87,4 +90,4 @@ public void describeTo(Description description) { } } } -} \ No newline at end of file +} diff --git a/src/test/java/org/junit/AssumptionViolatedExceptionTest.java b/src/test/java/org/junit/AssumptionViolatedExceptionTest.java index 75f86d706071..a1ce2fb2870b 100644 --- a/src/test/java/org/junit/AssumptionViolatedExceptionTest.java +++ b/src/test/java/org/junit/AssumptionViolatedExceptionTest.java @@ -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>")); } @@ -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)); + } }