July 2013
Intermediate to advanced
370 pages
8h 27m
English
We saw an example of using Expando as a mock object. We can also
use a Map. A map, as we know, has keys and associated values.
The values can be objects or even closures. We can take advantage of this to use
a Map in place of a collaborator.
Here’s a rewrite of the example using Expando
from Section 18.8, Mocking Using Expando, this time using
a Map:
| UnitTestingWithGroovy/TestUsingMap.groovy | |
| | import com.agiledeveloper.ClassWithDependency |
| | |
| | class TestUsingMap extends GroovyTestCase { |
| | void testMethodA() { |
| | def text = '' |
| | def fileMock = [write : { text = it }] |
| | |
| | def testObj = new ClassWithDependency() |
| | testObj.methodA(1, fileMock) |
| | |
| | assertEquals "The value is 1.", text |
| | } |
| | } |
Read now
Unlock full access