From 58fb4f0416ebf47bc16aad54660645b6a3f7175e 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 | 37 +++++++++++++++++++ 2 files changed, 42 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/junit/internal/AssumptionViolatedException.java b/src/main/java/org/junit/internal/AssumptionViolatedException.java index b1e364faf4c0f..89c6aa8862277 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 75f86d7060719..dd39da9602b33 100644 --- a/src/test/java/org/junit/AssumptionViolatedExceptionTest.java +++ b/src/test/java/org/junit/AssumptionViolatedExceptionTest.java @@ -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; @@ -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)); + } }