aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/org/junit/Assume.java
diff options
context:
space:
mode:
authorPaul Duffin <paulduffin@google.com>2016-12-14 11:49:43 +0000
committerPaul Duffin <paulduffin@google.com>2016-12-20 15:52:52 +0000
commitaeb93fc33cae3aadbb9b46083350ad2dc9aea645 (patch)
treeb316db7dee11d1aeee3510562e036fd41705b8b5 /src/main/java/org/junit/Assume.java
parent26401927b83770db45f00706ccc589955644c6c2 (diff)
downloadjunit-aeb93fc33cae3aadbb9b46083350ad2dc9aea645.tar.gz
Upgrade to JUnit 4.12
The license has changed from Common Public License v1.0 to Eclipse Public License v1.0. This will not compile as it is because it is intended to be built against Hamcrest 1.3 or later but it is being built against Hamcrest 1.1. A follow on patch will fix the compilation errors so that it builds against Hamcrest 1.1. That allows Hamcrest to be upgraded separately. The patch can be reverted once Hamcrest has been upgraded. There are also some Android specific issues that will also be fixed in follow on patches. Bug: 33613916 Test: make checkbuild Change-Id: Ic2c983a030399e3ace1a14927cb143fbd8307b4f
Diffstat (limited to 'src/main/java/org/junit/Assume.java')
-rw-r--r--src/main/java/org/junit/Assume.java204
1 files changed, 134 insertions, 70 deletions
diff --git a/src/main/java/org/junit/Assume.java b/src/main/java/org/junit/Assume.java
index 7b6c21a..b7687f7 100644
--- a/src/main/java/org/junit/Assume.java
+++ b/src/main/java/org/junit/Assume.java
@@ -1,27 +1,25 @@
package org.junit;
import static java.util.Arrays.asList;
+import static org.hamcrest.CoreMatchers.everyItem;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.hamcrest.CoreMatchers.nullValue;
+
import org.hamcrest.Matcher;
-import org.junit.internal.AssumptionViolatedException;
-import org.junit.internal.matchers.Each;
/**
* A set of methods useful for stating assumptions about the conditions in which a test is meaningful.
- * A failed assumption does not mean the code is broken, but that the test provides no useful information.
- * The default JUnit runner treats tests with failing assumptions as ignored. Custom runners may behave differently.
- *
- * For example:
- * <pre>
- * // only provides information if database is reachable.
- * \@Test public void calculateTotalSalary() {
- * DBConnection dbc = Database.connect();
- * assumeNotNull(dbc);
- * // ...
- * }
- * </pre>
+ * A failed assumption does not mean the code is broken, but that the test provides no useful information. Assume
+ * basically means "don't run this test if these conditions don't apply". The default JUnit runner skips tests with
+ * failing assumptions. Custom runners may behave differently.
+ * <p>
+ * A good example of using assumptions is in <a href="https://github.com/junit-team/junit/wiki/Theories">Theories</a> where they are needed to exclude certain datapoints that aren't suitable or allowed for a certain test case.
+ * </p>
+ * Failed assumptions are usually not logged, because there may be many tests that don't apply to certain
+ * configurations.
+ *
+ * <p>
* These methods can be used directly: <code>Assume.assumeTrue(...)</code>, however, they
* read better if they are referenced through static import:<br/>
* <pre>
@@ -29,66 +27,132 @@ import org.junit.internal.matchers.Each;
* ...
* assumeTrue(...);
* </pre>
+ * </p>
+ *
+ * @see <a href="https://github.com/junit-team/junit/wiki/Theories">Theories</a>
+ *
+ * @since 4.4
*/
public class Assume {
- /**
- * If called with an expression evaluating to {@code false}, the test will halt and be ignored.
- * @param b
- */
- public static void assumeTrue(boolean b) {
- assumeThat(b, is(true));
- }
+ /**
+ * If called with an expression evaluating to {@code false}, the test will halt and be ignored.
+ */
+ public static void assumeTrue(boolean b) {
+ assumeThat(b, is(true));
+ }
+
+ /**
+ * 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);
+ }
- /**
- * 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) {
- assumeThat(asList(objects), Each.each(notNullValue()));
- }
+ /**
+ * 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.
+ */
+ 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
+ */
+ 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("alwaysPasses", 1, is(1)); // passes
+ * foo(); // will execute
+ * assumeThat("alwaysFails", 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);
+ }
+ }
- /**
- * 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);
- }
+ /**
+ * Use to assume that an operation completes normally. If {@code e} is non-null, the test will halt and be ignored.
+ *
+ * For example:
+ * <pre>
+ * \@Test public void parseDataFile() {
+ * DataFile file;
+ * try {
+ * file = DataFile.open("sampledata.txt");
+ * } catch (IOException e) {
+ * // stop test and ignore if data can't be opened
+ * assumeNoException(e);
+ * }
+ * // ...
+ * }
+ * </pre>
+ *
+ * @param e if non-null, the offending exception
+ */
+ public static void assumeNoException(Throwable e) {
+ assumeThat(e, nullValue());
+ }
/**
- * Use to assume that an operation completes normally. If {@code t} is non-null, the test will halt and be ignored.
- *
- * For example:
- * <pre>
- * \@Test public void parseDataFile() {
- * DataFile file;
- * try {
- * file = DataFile.open("sampledata.txt");
- * } catch (IOException e) {
- * // stop test and ignore if data can't be opened
- * assumeNoException(e);
- * }
- * // ...
- * }
- * </pre>
- * @param t if non-null, the offending exception
- */
- public static void assumeNoException(Throwable t) {
- assumeThat(t, nullValue());
- }
+ * Attempts to halt the test and ignore it if Throwable <code>e</code> is
+ * not <code>null</code>. Similar to {@link #assumeNoException(Throwable)},
+ * but provides an additional message that can explain the details
+ * concerning the assumption.
+ *
+ * @param e if non-null, the offending exception
+ * @param message Additional message to pass to {@link AssumptionViolatedException}.
+ * @see #assumeNoException(Throwable)
+ */
+ public static void assumeNoException(String message, Throwable e) {
+ assumeThat(message, e, nullValue());
+ }
}