[Java] Class GroovyAssert
- groovy.test.GroovyAssert
GroovyAssert
contains a set of static assertion and test helper methods and is supposed to be a Groovy extension of JUnit 4's Assert class. In case JUnit 3 is the choice, the GroovyTestCase is meant to be used for writing tests based on TestCase.
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 import static groovy.test.GroovyAssert.assertNotNull
- See Also:
- GroovyTestCase
- Authors:
- Paul King
- Andre Steingress
- Since:
- 2.3
Field Summary
Modifiers | Name | Description |
---|---|---|
static String | TEST_SCRIPT_NAME_PREFIX |
Methods Summary
Type Params | Return Type | Name and description |
---|---|---|
static void |
assertScript(String script) Asserts that the script runs without any exceptions | |
protected static String |
genericScriptName() @return a generic script name to be used by GroovyShell#evaluate calls. | |
static boolean |
notYetImplemented(Object caller) | |
static Throwable |
shouldFail(Closure code) Asserts that the given code closure fails when it is evaluated | |
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. | |
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. | |
static Throwable |
shouldFail(String script) Asserts that the given script fails when it is evaluated | |
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. |
Field Detail
public static final String TEST_SCRIPT_NAME_PREFIX
Method Detail
public static void assertScript(String script)
Asserts that the script runs without any exceptions
- Parameters:
-
script
- the script that should pass without any exception thrown
protected static String genericScriptName()
- Returns:
- a generic script name to be used by
GroovyShell#evaluate
calls.
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
public static Throwable shouldFail(Closure code)
Asserts that the given code closure fails when it is evaluated
- Parameters:
-
code
- the code expected to fail
- Returns:
- the caught exception
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.
- Parameters:
-
clazz
- the class of the expected exception -
code
- the closure that should fail
- Returns:
- the caught exception
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.
- Parameters:
-
clazz
- the class of the expected exception -
script
- the script that should fail
- Returns:
- the caught exception
public static Throwable shouldFail(String script)
Asserts that the given script fails when it is evaluated
- Parameters:
-
script
- the script expected to fail
- Returns:
- the caught exception
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. 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.
- Parameters:
-
expectedCause
- the class of the expected exception -
code
- the closure that should fail
- Returns:
- the cause
© 2003-2020 The Apache Software Foundation
Licensed under the Apache license.
https://docs.groovy-lang.org/2.4.21/html/gapi/groovy/test/GroovyAssert.html