GroovyAssert (Groovy 5.0.0)
Description: declaration: package: groovy.test, class: GroovyAssert

Package groovy.test

# Class GroovyAssert

java.lang.Object
groovy.test.GroovyAssert

public class GroovyAssert extends Object

`GroovyAssert` contains a set of static assertion and test helper methods for JUnit 4+. They augment the kind of helper methods found in JUnit 4's `Assert` class. JUnit 3 users typically don't use these methods but instead, the equivalent methods in `GroovyTestCase`.

`GroovyAssert` methods can either be used by fully qualifying the static method like:

         groovy.test.GroovyAssert.shouldFail { ... }

or by importing the static methods with one ore more static imports:

         import static groovy.test.GroovyAssert.shouldFail

Backwards compatibility note: Prior to Groovy 4, `GroovyAssert` extended JUnit 4's `Assert` class. This meant that you could statically import static methods from that class via `GroovyAssert`, e.g.:

         import static groovy.test.GroovyAssert.assertNotNull

This is generally regarded as a code smell since inheritance is primarily to do with instance methods. From Groovy 4, you should import such methods directly, e.g.:

         import static org.junit.Assert.assertNotNull

Since:
  2.3
See Also:
  - `GroovyTestCase`

- ## Field Summary

  Fields
  Modifier and Type
  Field
  Description
  `static final String`
  `TEST_SCRIPT_NAME_PREFIX`

- ## Constructor Summary

  Constructors
  Constructor
  Description
  `GroovyAssert()`

- ## Method Summary

  Modifier and Type
  Method
  Description
  `static void`
  `assertScript(GroovyShell shell, String script)`
  Asserts that the script runs using the given shell without any exceptions
  `static void`
  `assertScript(String script)`
  Asserts that the script runs without any exceptions
  `static void`
  `fail(String message)`
  `protected static String`
  `genericScriptName()`
  `static boolean`
  `isAtLeastJdk(String specVersion)`
  `static boolean`
  `notYetImplemented(Object caller)`
  Runs the calling JUnit test again and fails only if it unexpectedly runs.
  This is helpful for tests that don't currently work but should work one day, when the tested functionality has been implemented.
  `static Throwable`
  `shouldFail(Closure<?> code)`
  Asserts that the given closure fails when executed.
  `static <T extends Throwable>
  T`
  `shouldFail(GroovyShell shell, Class<T> clazz, String script)`
  Asserts that the given script fails when evaluated using the given shell and that a particular type of exception is thrown.
  `static Throwable`
  `shouldFail(GroovyShell shell, String script)`
  Asserts that the given script fails when evaluated using the given shell.
  `static <T extends Throwable>
  T`
  `shouldFail(Class<T> type, Closure<?> code)`
  Asserts that the given closure fails when executed and that a particular type of exception is thrown.
  `static <T extends Throwable>
  T`
  `shouldFail(Class<T> clazz, String script)`
  Asserts that the given script fails when evaluated and that a particular type of exception is thrown.
  `static Throwable`
  `shouldFail(String script)`
  Asserts that the given script fails when evaluated.
  `static <T extends Throwable>
  T`
  `shouldFailWithCause(Class<T> type, Closure<?> code)`
  Asserts that the given closure fails when executed and that a particular exception type can be attributed to the cause.

  ### Methods inherited from class java.lang.Object

  `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- ## Field Details

  - ### TEST_SCRIPT_NAME_PREFIX

    public static final String TEST_SCRIPT_NAME_PREFIX
    See Also:
      - Constant Field Values

- ## Constructor Details

  - ### GroovyAssert

    public GroovyAssert()

- ## Method Details

  - ### genericScriptName

    protected static String genericScriptName()
    Returns:
      a generic script name to be used by `GroovyShell#evaluate`.

  - ### assertScript

    public static void assertScript(String script)
    Asserts that the script runs without any exceptions
    Parameters:
      `script` - the script that should pass without any exception

  - ### assertScript

    public static void assertScript(GroovyShell shell, String script)
    Asserts that the script runs using the given shell without any exceptions
    Parameters:
      `shell` - the shell to use to evaluate the script
      `script` - the script that should pass without any exception

  - ### fail

    public static void fail(String message)

  - ### shouldFail

    public static Throwable shouldFail(String script)
    Asserts that the given script fails when evaluated.
    Parameters:
      `script` - the script expected to fail
    Returns:
      the caught exception
    Throws:
      `AssertionError` - if no failure

  - ### shouldFail

    public static Throwable shouldFail(GroovyShell shell, String script)
    Asserts that the given script fails when evaluated using the given shell.
    Parameters:
      `shell` - the shell to use to evaluate the script
      `script` - the script expected to fail
    Returns:
      the caught exception
    Throws:
      `AssertionError` - if no failure

  - ### shouldFail

    public static <T extends Throwable> T shouldFail(Class<T> clazz, String script)
    Asserts that the given script fails when evaluated and that a particular type of exception is thrown.
    Parameters:
      `clazz` - the class of the expected exception
      `script` - the script expected to fail
    Returns:
      the caught exception
    Throws:
      `AssertionError` - if no failure

  - ### shouldFail

    public static <T extends Throwable> T shouldFail(GroovyShell shell, Class<T> clazz, String script)
    Asserts that the given script fails when evaluated using the given shell and that a particular type of exception is thrown.
    Parameters:
      `shell` - the shell to use to evaluate the script
      `clazz` - the class of the expected exception
      `script` - the script expected to fail
    Returns:
      the caught exception
    Throws:
      `AssertionError` - if no failure

  - ### shouldFail

    public static Throwable shouldFail(Closure<?> code)
    Asserts that the given closure fails when executed.
    Parameters:
      `code` - the block expected to fail
    Returns:
      the caught exception
    Throws:
      `AssertionError` - if no failure

  - ### shouldFail

    public static <T extends Throwable> T shouldFail(Class<T> type, Closure<?> code)
    Asserts that the given closure fails when executed and that a particular type of exception is thrown.
    Parameters:
      `type` - the class of the expected exception
      `code` - the block expected to fail
    Returns:
      the caught exception
    Throws:
      `AssertionError` - if no failure

  - ### shouldFailWithCause

    public static <T extends Throwable> T shouldFailWithCause(Class<T> type, Closure<?> code)
    Asserts that the given closure fails when executed and that a particular exception type can be attributed to the cause. The expected exception is compared recursively with any nested exceptions using getCause() until either a match is found or no more nested exceptions exist.

    If a match is found, the matching exception is returned otherwise the method will fail.

    Parameters:
      `type` - the class of the expected nested exception
      `code` - the block expected to fail
    Returns:
      the cause
    Throws:
      `AssertionError` - if no failure

  - ### notYetImplemented

    public static boolean notYetImplemented(Object caller)
    Runs the calling JUnit test again and fails only if it unexpectedly runs.
    This is helpful for tests that don't currently work but should work one day, when the tested functionality has been implemented.

    The right way to use it for JUnit 3 is:

         public void testXXX() {
           if (GroovyAssert.notYetImplemented(this)) return;
           ... the real (now failing) unit test
         }

    or for JUnit 4:

         @Test
         public void XXX() {
           if (GroovyAssert.notYetImplemented(this)) return;
           ... the real (now failing) unit test
         }

    Idea copied from HtmlUnit (many thanks to Marc Guillemot). Future versions maybe available in the JUnit distribution.
    Returns:
      `false` when not itself already in the call stack
    Throws:
      `AssertionError` - if no exception
    See Also:
      - `NotYetImplemented`

  - ### isAtLeastJdk

    public static boolean isAtLeastJdk(String specVersion)
    Returns:
      true if the JDK version is at least the version given by specVersion (e.g. "1.8", "9.0")
    Since:
      2.5.7