Class Assumptions


  • @API(status=STABLE,
         since="5.0")
    public class Assumptions
    extends java.lang.Object
    Assumptions is a collection of utility methods that support conditional test execution based on assumptions.

    In direct contrast to failed assertions, failed assumptions do not result in a test failure; rather, a failed assumption results in a test being aborted.

    Assumptions are typically used whenever it does not make sense to continue execution of a given test method — for example, if the test depends on something that does not exist in the current runtime environment.

    Although it is technically possible to extend this class, extension is strongly discouraged. The JUnit Team highly recommends that the methods defined in this class be used via static imports.

    Since:
    5.0
    See Also:
    TestAbortedException, Assertions
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      protected Assumptions()
      Protected constructor allowing subclassing but not direct instantiation.
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static <V> V abort()
      Abort the test without a message.
      static <V> V abort​(java.lang.String message)
      Abort the test with the given message.
      static <V> V abort​(java.util.function.Supplier<java.lang.String> messageSupplier)
      Abort the test with the supplied message.
      static void assumeFalse​(boolean assumption)
      Validate the given assumption.
      static void assumeFalse​(boolean assumption, java.lang.String message)
      Validate the given assumption.
      static void assumeFalse​(boolean assumption, java.util.function.Supplier<java.lang.String> messageSupplier)
      Validate the given assumption.
      static void assumeFalse​(java.util.function.BooleanSupplier assumptionSupplier)
      Validate the given assumption.
      static void assumeFalse​(java.util.function.BooleanSupplier assumptionSupplier, java.lang.String message)
      Validate the given assumption.
      static void assumeFalse​(java.util.function.BooleanSupplier assumptionSupplier, java.util.function.Supplier<java.lang.String> messageSupplier)
      Validate the given assumption.
      static void assumeTrue​(boolean assumption)
      Validate the given assumption.
      static void assumeTrue​(boolean assumption, java.lang.String message)
      Validate the given assumption.
      static void assumeTrue​(boolean assumption, java.util.function.Supplier<java.lang.String> messageSupplier)
      Validate the given assumption.
      static void assumeTrue​(java.util.function.BooleanSupplier assumptionSupplier)
      Validate the given assumption.
      static void assumeTrue​(java.util.function.BooleanSupplier assumptionSupplier, java.lang.String message)
      Validate the given assumption.
      static void assumeTrue​(java.util.function.BooleanSupplier assumptionSupplier, java.util.function.Supplier<java.lang.String> messageSupplier)
      Validate the given assumption.
      static void assumingThat​(boolean assumption, Executable executable)
      Execute the supplied Executable, but only if the supplied assumption is valid.
      static void assumingThat​(java.util.function.BooleanSupplier assumptionSupplier, Executable executable)
      Execute the supplied Executable, but only if the supplied assumption is valid.
      private static void throwAssumptionFailed​(java.lang.String message)  
      • Methods inherited from class java.lang.Object

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

      • Assumptions

        protected Assumptions()
        Protected constructor allowing subclassing but not direct instantiation.
        Since:
        5.3
    • Method Detail

      • assumeTrue

        public static void assumeTrue​(boolean assumption)
                               throws org.opentest4j.TestAbortedException
        Validate the given assumption.
        Parameters:
        assumption - the assumption to validate
        Throws:
        org.opentest4j.TestAbortedException - if the assumption is not true
      • assumeTrue

        public static void assumeTrue​(java.util.function.BooleanSupplier assumptionSupplier)
                               throws org.opentest4j.TestAbortedException
        Validate the given assumption.
        Parameters:
        assumptionSupplier - the supplier of the assumption to validate
        Throws:
        org.opentest4j.TestAbortedException - if the assumption is not true
      • assumeTrue

        public static void assumeTrue​(java.util.function.BooleanSupplier assumptionSupplier,
                                      java.lang.String message)
                               throws org.opentest4j.TestAbortedException
        Validate the given assumption.
        Parameters:
        assumptionSupplier - the supplier of the assumption to validate
        message - the message to be included in the TestAbortedException if the assumption is invalid
        Throws:
        org.opentest4j.TestAbortedException - if the assumption is not true
      • assumeTrue

        public static void assumeTrue​(boolean assumption,
                                      java.util.function.Supplier<java.lang.String> messageSupplier)
                               throws org.opentest4j.TestAbortedException
        Validate the given assumption.
        Parameters:
        assumption - the assumption to validate
        messageSupplier - the supplier of the message to be included in the TestAbortedException if the assumption is invalid
        Throws:
        org.opentest4j.TestAbortedException - if the assumption is not true
      • assumeTrue

        public static void assumeTrue​(boolean assumption,
                                      java.lang.String message)
                               throws org.opentest4j.TestAbortedException
        Validate the given assumption.
        Parameters:
        assumption - the assumption to validate
        message - the message to be included in the TestAbortedException if the assumption is invalid
        Throws:
        org.opentest4j.TestAbortedException - if the assumption is not true
      • assumeTrue

        public static void assumeTrue​(java.util.function.BooleanSupplier assumptionSupplier,
                                      java.util.function.Supplier<java.lang.String> messageSupplier)
                               throws org.opentest4j.TestAbortedException
        Validate the given assumption.
        Parameters:
        assumptionSupplier - the supplier of the assumption to validate
        messageSupplier - the supplier of the message to be included in the TestAbortedException if the assumption is invalid
        Throws:
        org.opentest4j.TestAbortedException - if the assumption is not true
      • assumeFalse

        public static void assumeFalse​(boolean assumption)
                                throws org.opentest4j.TestAbortedException
        Validate the given assumption.
        Parameters:
        assumption - the assumption to validate
        Throws:
        org.opentest4j.TestAbortedException - if the assumption is not false
      • assumeFalse

        public static void assumeFalse​(java.util.function.BooleanSupplier assumptionSupplier)
                                throws org.opentest4j.TestAbortedException
        Validate the given assumption.
        Parameters:
        assumptionSupplier - the supplier of the assumption to validate
        Throws:
        org.opentest4j.TestAbortedException - if the assumption is not false
      • assumeFalse

        public static void assumeFalse​(java.util.function.BooleanSupplier assumptionSupplier,
                                       java.lang.String message)
                                throws org.opentest4j.TestAbortedException
        Validate the given assumption.
        Parameters:
        assumptionSupplier - the supplier of the assumption to validate
        message - the message to be included in the TestAbortedException if the assumption is invalid
        Throws:
        org.opentest4j.TestAbortedException - if the assumption is not false
      • assumeFalse

        public static void assumeFalse​(boolean assumption,
                                       java.util.function.Supplier<java.lang.String> messageSupplier)
                                throws org.opentest4j.TestAbortedException
        Validate the given assumption.
        Parameters:
        assumption - the assumption to validate
        messageSupplier - the supplier of the message to be included in the TestAbortedException if the assumption is invalid
        Throws:
        org.opentest4j.TestAbortedException - if the assumption is not false
      • assumeFalse

        public static void assumeFalse​(boolean assumption,
                                       java.lang.String message)
                                throws org.opentest4j.TestAbortedException
        Validate the given assumption.
        Parameters:
        assumption - the assumption to validate
        message - the message to be included in the TestAbortedException if the assumption is invalid
        Throws:
        org.opentest4j.TestAbortedException - if the assumption is not false
      • assumeFalse

        public static void assumeFalse​(java.util.function.BooleanSupplier assumptionSupplier,
                                       java.util.function.Supplier<java.lang.String> messageSupplier)
                                throws org.opentest4j.TestAbortedException
        Validate the given assumption.
        Parameters:
        assumptionSupplier - the supplier of the assumption to validate
        messageSupplier - the supplier of the message to be included in the TestAbortedException if the assumption is invalid
        Throws:
        org.opentest4j.TestAbortedException - if the assumption is not false
      • assumingThat

        public static void assumingThat​(java.util.function.BooleanSupplier assumptionSupplier,
                                        Executable executable)
        Execute the supplied Executable, but only if the supplied assumption is valid.

        Unlike the other assumption methods, this method will not abort the test. If the assumption is invalid, this method does nothing. If the assumption is valid and the executable throws an exception, it will be treated like a regular test failure. That exception will be rethrown as is but masked as an unchecked exception.

        Parameters:
        assumptionSupplier - the supplier of the assumption to validate
        executable - the block of code to execute if the assumption is valid
        See Also:
        assumingThat(boolean, Executable)
      • assumingThat

        public static void assumingThat​(boolean assumption,
                                        Executable executable)
        Execute the supplied Executable, but only if the supplied assumption is valid.

        Unlike the other assumption methods, this method will not abort the test. If the assumption is invalid, this method does nothing. If the assumption is valid and the executable throws an exception, it will be treated like a regular test failure. That exception will be rethrown as is but masked as an unchecked exception.

        Parameters:
        assumption - the assumption to validate
        executable - the block of code to execute if the assumption is valid
        See Also:
        assumingThat(BooleanSupplier, Executable)
      • abort

        @API(status=STABLE,
             since="5.9")
        public static <V> V abort()
        Abort the test without a message.

        Although aborting with an explicit message is recommended, this may be useful when maintaining legacy code.

        See Javadoc for abort(String) for an explanation of this method's generic return type V.

        Throws:
        org.opentest4j.TestAbortedException - always
        Since:
        5.9
      • abort

        @API(status=STABLE,
             since="5.9")
        public static <V> V abort​(java.lang.String message)
        Abort the test with the given message.

        The generic return type V allows this method to be used directly as a single-statement lambda expression, thereby avoiding the need to implement a code block with an explicit return value. Since this method throws a TestAbortedException before its return statement, this method never actually returns a value to its caller. The following example demonstrates how this may be used in practice.

        
         Stream.of().map(entry -> abort("assumption not met"));
         
        Parameters:
        message - the message to be included in the TestAbortedException
        Throws:
        org.opentest4j.TestAbortedException - always
        Since:
        5.9
      • abort

        @API(status=STABLE,
             since="5.9")
        public static <V> V abort​(java.util.function.Supplier<java.lang.String> messageSupplier)
        Abort the test with the supplied message.

        See Javadoc for abort(String) for an explanation of this method's generic return type V.

        Parameters:
        messageSupplier - the supplier of the message to be included in the TestAbortedException
        Throws:
        org.opentest4j.TestAbortedException - always
        Since:
        5.9
      • throwAssumptionFailed

        private static void throwAssumptionFailed​(java.lang.String message)