Java Reference
In-Depth Information
Most of the scripts in this topic are tested using the techniques described in this section. If
the script (or any class, for that matter) has dependencies, however, there's a bit more work
to be done.
Lessons learned (testing scripts)
1 . Groovy scripts provide their own challenges, especially when trying to capture in-
put or output data and logging results.
2 . Groovy source code can be executed programmatically through the
GroovyShell and Binding classes, which then execute any contained assert
methods.
3 . Special subclasses of GroovyTestCase are available to simplify script testing.
True unit testing means testing an isolated class. The success or failure of the test should
not rely on any associated objects. Any dependent objects should be replaced by mocks or
stubs that return predetermined values when accessed.
This is another area that's significantly easier to handle when using Groovy than it is when
using Java. Groovy has several built-in mechanisms for creating mock or stub objects,
which I'll review in the next section.
6.3. Testing classes in isolation
In object-oriented programs no class is an island. Classes normally have dependencies. An
integration test uses all of the dependencies together with the class under test (often called
the CUT for that reason), but to truly test a given class you need to isolate it from its envir-
onment.
To isolate a class you need to provide it with what it needs from the dependencies in order
to do its job. For example, if a class processes data it extracts from the rest of the system,
you need to supply that data in a controlled manner that doesn't involve the rest of the sys-
tem.
Search WWH ::




Custom Search