Class GroovyAssert
- java.lang.Object
-
- org.junit.Assert
-
- groovy.test.GroovyAssert
-
public class GroovyAssert extends Assert
GroovyAssertcontains a set of static assertion and test helper methods and is supposed to be a Groovy extension of JUnit 4'sAssertclass. In case JUnit 3 is the choice, theGroovyTestCaseis meant to be used for writing tests based onTestCase.GroovyAssertmethods can either be used by fully qualifying the static method likegroovy.test.GroovyAssert.shouldFail { ... }or by importing the static methods with one ore more static importsimport static groovy.test.GroovyAssert.shouldFail import static groovy.test.GroovyAssert.assertNotNull- Since:
- 2.3
- See Also:
GroovyTestCase
-
-
Field Summary
Fields Modifier and Type Field Description static java.lang.StringTEST_SCRIPT_NAME_PREFIX
-
Constructor Summary
Constructors Constructor Description GroovyAssert()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static voidassertScript(java.lang.String script)Asserts that the script runs without any exceptionsprotected static java.lang.StringgenericScriptName()static booleanisAtLeastJdk(java.lang.String specVersion)static booleannotYetImplemented(java.lang.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 java.lang.ThrowableshouldFail(Closure code)Asserts that the given code closure fails when it is evaluatedstatic java.lang.ThrowableshouldFail(java.lang.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 java.lang.ThrowableshouldFail(java.lang.Class clazz, java.lang.String script)Asserts that the given script fails when it is evaluated and that a particular type of exception is thrown.static java.lang.ThrowableshouldFail(java.lang.String script)Asserts that the given script fails when it is evaluatedstatic java.lang.ThrowableshouldFailWithCause(java.lang.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.-
Methods inherited from class org.junit.Assert
assertArrayEquals, assertArrayEquals, assertArrayEquals, assertArrayEquals, assertArrayEquals, assertArrayEquals, assertArrayEquals, assertArrayEquals, assertArrayEquals, assertArrayEquals, assertArrayEquals, assertArrayEquals, assertArrayEquals, assertArrayEquals, assertArrayEquals, assertArrayEquals, assertArrayEquals, assertArrayEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertFalse, assertFalse, assertNotEquals, assertNotEquals, assertNotEquals, assertNotEquals, assertNotEquals, assertNotEquals, assertNotEquals, assertNotEquals, assertNotNull, assertNotNull, assertNotSame, assertNotSame, assertNull, assertNull, assertSame, assertSame, assertThat, assertThat, assertThrows, assertThrows, assertTrue, assertTrue, fail, fail
-
-
-
-
Field Detail
-
TEST_SCRIPT_NAME_PREFIX
public static final java.lang.String TEST_SCRIPT_NAME_PREFIX
- See Also:
- Constant Field Values
-
-
Method Detail
-
genericScriptName
protected static java.lang.String genericScriptName()
- Returns:
- a generic script name to be used by
GroovyShell#evaluatecalls.
-
assertScript
public static void assertScript(java.lang.String script) throws java.lang.ExceptionAsserts that the script runs without any exceptions- Parameters:
script- the script that should pass without any exception thrown- Throws:
java.lang.Exception
-
shouldFail
public static java.lang.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
-
shouldFail
public static java.lang.Throwable shouldFail(java.lang.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 exceptioncode- the closure that should fail- Returns:
- the caught exception
-
shouldFailWithCause
public static java.lang.Throwable shouldFailWithCause(java.lang.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 exceptioncode- the closure that should fail- Returns:
- the cause
-
shouldFail
public static java.lang.Throwable shouldFail(java.lang.Class clazz, java.lang.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 exceptionscript- the script that should fail- Returns:
- the caught exception
-
shouldFail
public static java.lang.Throwable shouldFail(java.lang.String script)
Asserts that the given script fails when it is evaluated- Parameters:
script- the script expected to fail- Returns:
- the caught exception
-
notYetImplemented
public static boolean notYetImplemented(java.lang.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:
falsewhen not itself already in the call stack
-
isAtLeastJdk
public static boolean isAtLeastJdk(java.lang.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
-
-