All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.junit.Assume Maven / Gradle / Ivy

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;

/**
 * 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:
 * 
 * // only provides information if database is reachable.
 * \@Test public void calculateTotalSalary() {
 *    DBConnection dbc = Database.connect();
 *    assumeNotNull(dbc);
 *    // ...
 * }
 * 
* 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(...);
 * 
* * @since 4.4 */ public class 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) { assumeTrue(!b); } /** * 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 one or more null elements in objects, the test will halt and be ignored. */ public static void assumeNotNull(Object... objects) { 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 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 void assumeThat(T actual, Matcher matcher) { if (!matcher.matches(actual)) { throw new AssumptionViolatedException(actual, matcher); } } /** * 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 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 void assumeThat(String message, T actual, Matcher 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. * * For example: *
     * \@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 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 t is * not null. Similar to {@link #assumeNoException(Throwable)}, * but provides an additional message that can explain the details * concerning the assumption. * * @param t 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 t) { assumeThat(message, t, nullValue()); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy