Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge pull request #489 from matthewfarwell/assume-message

Fixes #355 Improve Assume to allow custom message
  • Loading branch information...
commit 51350c377bdaaece72dc7975fab6da35e649183e 2 parents 2e53904 + 3bb48f0
@dsaff dsaff authored
View
100 src/main/java/org/junit/Assume.java
@@ -41,36 +41,84 @@ public static void assumeTrue(boolean b) {
}
/**
+ * The inverse of {@link #assumeTrue(boolean)}.
+ */
+ public static void assumeFalse(boolean b) {
+ assumeTrue(!b);
+ }
+
+ /**
+ * If called with an expression evaluating to {@code false}, the test will halt and be ignored.
+ *
+ * @param b If <code>false</code>, the method will attempt to stop the test and ignore it by
+ * throwing {@link AssumptionViolatedException}.
+ * @param message A message to pass to {@link AssumptionViolatedException}.
+ */
+ public static void assumeTrue(String message, boolean b) {
+ if (!b) throw new AssumptionViolatedException(message);
+ }
+
+ /**
+ * The inverse of {@link #assumeTrue(String, boolean)}.
+ */
+ public static void assumeFalse(String message, boolean b) {
+ assumeTrue(message, !b);
+ }
+
+ /**
* If called with one or more null elements in <code>objects</code>, the test will halt and be ignored.
* @param objects
*/
- public static void assumeNotNull(Object... objects) {
+ public static void assumeNotNull(Object... objects) {
assumeThat(asList(objects), everyItem(notNullValue()));
}
- /**
- * Call to assume that <code>actual</code> satisfies the condition specified by <code>matcher</code>.
- * If not, the test halts and is ignored.
- * Example:
- * <pre>:
- * assumeThat(1, is(1)); // passes
- * foo(); // will execute
- * assumeThat(0, is(1)); // assumption failure! test halts
- * int x = 1 / 0; // will never execute
- * </pre>
- *
- * @param <T> the static type accepted by the matcher (this can flag obvious compile-time problems such as {@code assumeThat(1, is("a"))}
- * @param actual the computed value being compared
- * @param matcher an expression, built of {@link Matcher}s, specifying allowed values
- *
- * @see org.hamcrest.CoreMatchers
- * @see org.junit.matchers.JUnitMatchers
- */
+ /**
+ * Call to assume that <code>actual</code> satisfies the condition specified by <code>matcher</code>.
+ * If not, the test halts and is ignored.
+ * Example:
+ * <pre>:
+ * assumeThat(1, is(1)); // passes
+ * foo(); // will execute
+ * assumeThat(0, is(1)); // assumption failure! test halts
+ * int x = 1 / 0; // will never execute
+ * </pre>
+ *
+ * @param <T> the static type accepted by the matcher (this can flag obvious compile-time problems such as {@code assumeThat(1, is("a"))}
+ * @param actual the computed value being compared
+ * @param matcher an expression, built of {@link Matcher}s, specifying allowed values
+ *
+ * @see org.hamcrest.CoreMatchers
+ * @see org.junit.matchers.JUnitMatchers
+ */
public static <T> void assumeThat(T actual, Matcher<T> matcher) {
if (!matcher.matches(actual))
throw new AssumptionViolatedException(actual, matcher);
}
+ /**
+ * Call to assume that <code>actual</code> satisfies the condition specified by <code>matcher</code>.
+ * If not, the test halts and is ignored.
+ * Example:
+ * <pre>:
+ * assumeThat(1, is(1)); // passes
+ * foo(); // will execute
+ * assumeThat(0, is(1)); // assumption failure! test halts
+ * int x = 1 / 0; // will never execute
+ * </pre>
+ *
+ * @param <T> the static type accepted by the matcher (this can flag obvious compile-time problems such as {@code assumeThat(1, is("a"))}
+ * @param actual the computed value being compared
+ * @param matcher an expression, built of {@link Matcher}s, specifying allowed values
+ *
+ * @see org.hamcrest.CoreMatchers
+ * @see org.junit.matchers.JUnitMatchers
+ */
+ public static <T> void assumeThat(String message, T actual, Matcher<T> matcher) {
+ if (!matcher.matches(actual))
+ throw new AssumptionViolatedException(message, actual, matcher);
+ }
+
/**
* Use to assume that an operation completes normally. If {@code t} is non-null, the test will halt and be ignored.
*
@@ -92,4 +140,18 @@ public static void assumeNotNull(Object... objects) {
public static void assumeNoException(Throwable t) {
assumeThat(t, nullValue());
}
+
+ /**
+ * Attempts to halt the test and ignore it if Throwable <code>t</code> is
+ * not <code>null</code>. Similar to {@link #assumeNoException(Throwable)},
+ * but provides an additional message that can explain the details
+ * concerning the assumption.
+ *
+ * @see #assumeNoException(Throwable)
+ * @param t if non-null, the offending exception
+ * @param message Additional message to pass to {@link AssumptionViolatedException}.
+ */
+ public static void assumeNoException(String message, Throwable t) {
+ assumeThat(message, t, nullValue());
+ }
}
View
61 src/main/java/org/junit/internal/AssumptionViolatedException.java
@@ -5,21 +5,59 @@
import org.hamcrest.SelfDescribing;
import org.hamcrest.StringDescription;
+/**
+ * An exception class used to implement <i>assumptions</i> (state in which a given test
+ * is meaningful and should or should not be executed). A test for which an assumption
+ * fails should not generate a test case failure.
+ *
+ * @see Assume
+ */
public class AssumptionViolatedException extends RuntimeException implements SelfDescribing {
- private static final long serialVersionUID= 1L;
+ private static final long serialVersionUID= 2L;
+ private final String fAssumption;
+
+ private final boolean fValueMatcher;
private final Object fValue;
private final Matcher<?> fMatcher;
- public AssumptionViolatedException(Object value, Matcher<?> matcher) {
+ public AssumptionViolatedException(String assumption, boolean valueMatcher, Object value, Matcher<?> matcher) {
super(value instanceof Throwable ? (Throwable) value : null);
+ fAssumption= assumption;
fValue= value;
fMatcher= matcher;
+ fValueMatcher= valueMatcher;
+ }
+
+ /**
+ * An assumption exception with the given <i>value</i> (String or
+ * Throwable) and an additional failing {@link Matcher}.
+ */
+ public AssumptionViolatedException(Object value, Matcher<?> matcher) {
+ this(null, true, value, matcher);
}
+ /**
+ * An assumption exception with the given <i>value</i> (String or
+ * Throwable) and an additional failing {@link Matcher}.
+ */
+ public AssumptionViolatedException(String assumption, Object value, Matcher<?> matcher) {
+ this(assumption, true, value, matcher);
+ }
+
+ /**
+ * An assumption exception with the given message only.
+ */
public AssumptionViolatedException(String assumption) {
- this(assumption, null);
+ this(assumption, false, null, null);
+ }
+
+ /**
+ * An assumption exception with the given message and a cause.
+ */
+ public AssumptionViolatedException(String assumption, Throwable t) {
+ this(assumption, false, t, null);
}
@Override
@@ -28,13 +66,20 @@ public String getMessage() {
}
public void describeTo(Description description) {
- if (fMatcher != null) {
+ if (fAssumption != null)
+ description.appendText(fAssumption);
+
+ if (fValueMatcher) {
+ if (fAssumption != null)
+ description.appendText(": ");
+
description.appendText("got: ");
description.appendValue(fValue);
- description.appendText(", expected: ");
- description.appendDescriptionOf(fMatcher);
- } else {
- description.appendText("failed assumption: " + fValue);
+
+ if (fMatcher != null) {
+ description.appendText(", expected: ");
+ description.appendDescriptionOf(fMatcher);
+ }
}
}
}
View
80 src/test/java/org/junit/tests/experimental/AssumptionTest.java
@@ -4,12 +4,19 @@
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.fail;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertSame;
import static org.junit.Assume.assumeNoException;
import static org.junit.Assume.assumeNotNull;
import static org.junit.Assume.assumeThat;
import static org.junit.Assume.assumeTrue;
import static org.junit.experimental.results.PrintableResult.testResult;
import static org.junit.experimental.results.ResultMatchers.isSuccessful;
+
+import java.util.ArrayList;
+import java.util.List;
+
import org.junit.Assume;
import org.junit.Before;
import org.junit.BeforeClass;
@@ -174,4 +181,77 @@ public AssumptionFailureInConstructor() {
public void assumeWithExpectedException() {
assumeTrue(false);
}
+
+ final static String message = "Some random message string.";
+ final static Throwable t = new Throwable();
+
+ /**
+ * @see AssumptionTest#assumptionsWithMessage()
+ */
+ public static class HasAssumeWithMessage {
+ @Test
+ public void testMethod() {
+ assumeTrue(message, false);
+ }
+ }
+
+ @Test
+ public void assumptionsWithMessage() {
+ final List<Failure> failures =
+ runAndGetAssumptionFailures(HasAssumeWithMessage.class);
+
+ assertTrue(failures.get(0).getMessage().contains(message));
+ }
+
+ /**
+ * @see AssumptionTest#assumptionsWithMessageAndCause()
+ */
+ public static class HasAssumeWithMessageAndCause {
+ @Test
+ public void testMethod() {
+ assumeNoException(message, t);
+ }
+ }
+
+ @Test
+ public void assumptionsWithMessageAndCause() {
+ final List<Failure> failures =
+ runAndGetAssumptionFailures(HasAssumeWithMessageAndCause.class);
+ assertTrue(failures.get(0).getMessage().contains(message));
+ assertSame(failures.get(0).getException().getCause(), t);
+ }
+
+ public static class HasFailingAssumptionWithMessage {
+ @Test
+ public void assumptionsFail() {
+ assumeThat(message, 3, is(4));
+ fail();
+ }
+ }
+
+ @Test
+ public void failedAssumptionsWithMessage() {
+ final List<Failure> failures =
+ runAndGetAssumptionFailures(HasFailingAssumptionWithMessage.class);
+
+ assertEquals(failures.size(), 1);
+ assertTrue(failures.get(0).getMessage().contains(message));
+ }
+
+ /**
+ * Helper method that runs tests on <code>clazz</code> and returns any
+ * {@link Failure} objects that were {@link AssumptionViolatedException}s.
+ */
+ private static List<Failure> runAndGetAssumptionFailures(Class<?> clazz) {
+ final List<Failure> failures = new ArrayList<Failure>();
+ final JUnitCore core = new JUnitCore();
+ core.addListener(new RunListener() {
+ @Override
+ public void testAssumptionFailure(Failure failure) {
+ failures.add(failure);
+ }
+ });
+ core.run(clazz);
+ return failures;
+ }
}
View
2  src/test/java/org/junit/tests/experimental/AssumptionViolatedExceptionTest.java
@@ -47,6 +47,6 @@ public void AssumptionViolatedExceptionDescribesItself() {
@Test
public void simpleAssumptionViolatedExceptionDescribesItself() {
AssumptionViolatedException e= new AssumptionViolatedException("not enough money");
- assertThat(StringDescription.asString(e), is("failed assumption: not enough money"));
+ assertThat(StringDescription.asString(e), is("not enough money"));
}
}
Please sign in to comment.
Something went wrong with that request. Please try again.