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.shouldFailBackwards 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.assertNotNullThis 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
Modifiers | Name | Description |
---|---|---|
static String |
TEST_SCRIPT_NAME_PREFIX |
Type Params | Return Type | Name and description |
---|---|---|
|
public static void |
assertScript(String script) Asserts that the script runs without any exceptions |
|
public static void |
assertScript(GroovyShell shell, String script) Asserts that the script runs using the given shell without any exceptions |
|
public static void |
fail(String message) |
|
protected static String |
genericScriptName()
|
|
public static boolean |
isAtLeastJdk(String specVersion)
|
|
public static boolean |
notYetImplemented(Object caller) |
|
public static Throwable |
shouldFail(Closure code) Asserts that the given code closure fails when it is evaluated |
|
public static Throwable |
shouldFail(Class clazz, Closure code) Asserts that the given code closure fails when it is evaluated and that a particular type of exception is thrown. |
|
public static Throwable |
shouldFail(Class clazz, String script) Asserts that the given script fails when it is evaluated and that a particular type of exception is thrown. |
|
public static Throwable |
shouldFail(GroovyShell shell, Class clazz, String script) Asserts that the given script fails when it is evaluated using the given shell and that a particular type of exception is thrown. |
|
public static Throwable |
shouldFail(String script) Asserts that the given script fails when it is evaluated |
|
public static Throwable |
shouldFail(GroovyShell shell, String script) Asserts that the given script fails when it is evaluated using the given shell |
|
public static Throwable |
shouldFailWithCause(Class expectedCause, Closure code) Asserts that the given code closure fails when it is evaluated and that a particular Exception type can be attributed to the cause. |
Asserts that the script runs without any exceptions
script
- the script that should pass without any exception thrownAsserts that the script runs using the given shell without any exceptions
shell
- the shell to use to evaluate the scriptscript
- the script that should pass without any exception thrown
GroovyShell#evaluate
calls.
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.
false
when not itself already in the call stackAsserts that the given code closure fails when it is evaluated
code
- the code expected to failAsserts that the given code closure fails when it is evaluated and that a particular type of exception is thrown.
clazz
- the class of the expected exceptioncode
- the closure that should failAsserts that the given script fails when it is evaluated and that a particular type of exception is thrown.
clazz
- the class of the expected exceptionscript
- the script that should failAsserts that the given script fails when it is evaluated using the given shell and that a particular type of exception is thrown.
shell
- the shell to use to evaluate the scriptclazz
- the class of the expected exceptionscript
- the script that should failAsserts that the given script fails when it is evaluated
script
- the script expected to failAsserts that the given script fails when it is evaluated using the given shell
shell
- the shell to use to evaluate the scriptscript
- the script expected to failAsserts that the given code closure fails when it is evaluated and that a particular Exception type can be attributed to the cause. The expected exception class 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.
expectedCause
- the class of the expected exceptioncode
- the closure that should fail