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; /** * 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. 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. *
* A good example of using assumptions is in Theories where they are needed to exclude certain datapoints that aren't suitable or allowed for a certain test case. *
* Failed assumptions are usually not logged, because there may be many tests that don't apply to certain * configurations. * *
* These methods can be used directly: Assume.assumeTrue(...)
, however, they
* read better if they are referenced through static import:
*
* import static org.junit.Assume.*; * ... * assumeTrue(...); ** * * @see Theories * * @since 4.4 */ public class Assume { /** * Do not instantiate. * @deprecated since 4.13. */ @Deprecated public Assume() { } /** * 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) { assumeThat(b, is(false)); } /** * If called with an expression evaluating to {@code false}, the test will halt and be ignored. * * @param b If
false
, 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 a {@code null} array or one or more {@code null} elements in {@code objects},
* the test will halt and be ignored.
*/
public static void assumeNotNull(Object... objects) {
assumeThat(objects, notNullValue());
assumeThat(asList(objects), everyItem(notNullValue()));
}
/**
* Call to assume that actual
satisfies the condition specified by matcher
.
* If not, the test halts and is ignored.
* Example:
* : * assumeThat(1, is(1)); // passes * foo(); // will execute * assumeThat(0, is(1)); // assumption failure! test halts * int x = 1 / 0; // will never execute ** * @param
actual
satisfies the condition specified by matcher
.
* If not, the test halts and is ignored.
* Example:
* : * 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 ** * @param
* \@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); * } * // ... * } ** * @param e if non-null, the offending exception */ public static void assumeNoException(Throwable e) { assumeThat(e, nullValue()); } /** * Attempts to halt the test and ignore it if Throwable
e
is
* not null
. 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());
}
}