Java Reference
In-Depth Information
Listing 6.12. Testing Groovy scripts using GroovyShellTestCase
The first test finds the script to run and executes it using the shell instantiated in the su-
perclass. The other tests use the
withBinding
method to override the
out
variable and
provide an input parameter. The results are the same as instantiating the
GroovyShell
and
Binding
classes directly.
The previous example showed how to capture standard output from a script, but normally
scripts return concrete values. The
withBinding
method returns whatever the script re-
turns. As a trivial example, consider the following powerful Groovy calculator, saved in a
file called
calc.groovy
:
z = x + y
Because none of the three variables (
x
,
y
, and
z
) are declared, they can all be accessed
through the script's binding. The next listing shows a test for this script that validates the
returned value.