Package org.junit

Class Assume


  • public class Assume
    extends java.lang.Object
    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(...);
     

    Since:
    4.4
    See Also:
    Theories
    • Constructor Summary

      Constructors 
      Constructor Description
      Assume()
      Deprecated.
      since 4.13.
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static void assumeFalse​(boolean b)
      The inverse of assumeTrue(boolean).
      static void assumeFalse​(java.lang.String message, boolean b)
      static void assumeNoException​(java.lang.String message, java.lang.Throwable e)
      Attempts to halt the test and ignore it if Throwable e is not null.
      static void assumeNoException​(java.lang.Throwable e)
      Use to assume that an operation completes normally.
      static void assumeNotNull​(java.lang.Object... objects)
      If called with a null array or one or more null elements in objects, the test will halt and be ignored.
      static <T> void assumeThat​(java.lang.String message, T actual, org.hamcrest.Matcher<T> matcher)
      Call to assume that actual satisfies the condition specified by matcher.
      static <T> void assumeThat​(T actual, org.hamcrest.Matcher<T> matcher)
      Call to assume that actual satisfies the condition specified by matcher.
      static void assumeTrue​(boolean b)
      If called with an expression evaluating to false, the test will halt and be ignored.
      static void assumeTrue​(java.lang.String message, boolean b)
      If called with an expression evaluating to false, the test will halt and be ignored.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • Assume

        @Deprecated
        public Assume()
        Deprecated.
        since 4.13.
        Do not instantiate.
    • Method Detail

      • assumeTrue

        public static void assumeTrue​(boolean b)
        If called with an expression evaluating to false, the test will halt and be ignored.
      • assumeTrue

        public static void assumeTrue​(java.lang.String message,
                                      boolean b)
        If called with an expression evaluating to false, the test will halt and be ignored.
        Parameters:
        b - If false, the method will attempt to stop the test and ignore it by throwing AssumptionViolatedException.
        message - A message to pass to AssumptionViolatedException.
      • assumeNotNull

        public static void assumeNotNull​(java.lang.Object... objects)
        If called with a null array or one or more null elements in objects, the test will halt and be ignored.
      • assumeThat

        public static <T> void assumeThat​(T actual,
                                          org.hamcrest.Matcher<T> 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
         
        Type Parameters:
        T - the static type accepted by the matcher (this can flag obvious compile-time problems such as assumeThat(1, is("a"))
        Parameters:
        actual - the computed value being compared
        matcher - an expression, built of Matchers, specifying allowed values
        See Also:
        CoreMatchers, JUnitMatchers
      • assumeThat

        public static <T> void assumeThat​(java.lang.String message,
                                          T actual,
                                          org.hamcrest.Matcher<T> matcher)
        Call to assume that 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
         
        Type Parameters:
        T - the static type accepted by the matcher (this can flag obvious compile-time problems such as assumeThat(1, is("a"))
        Parameters:
        actual - the computed value being compared
        matcher - an expression, built of Matchers, specifying allowed values
        See Also:
        CoreMatchers, JUnitMatchers
      • assumeNoException

        public static void assumeNoException​(java.lang.Throwable e)
        Use to assume that an operation completes normally. If e 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);
           }
           // ...
         }
         
        Parameters:
        e - if non-null, the offending exception