July 2013
Intermediate to advanced
370 pages
8h 27m
English
In Section 13.1, Injecting Methods Using Categories, we discussed how categories provide controlled aspect-oriented programming (AOP) in Groovy. In this section we’ll see how we can use it to create mocks.
| UnitTestingWithGroovy/TestUsingCategories.groovy | |
| | import com.agiledeveloper.CodeWithHeavierDependencies |
| | |
| | class TestUsingCategories extends GroovyTestCase { |
| | void testMyMethod() { |
| | def testObj = new CodeWithHeavierDependencies() |
| | |
| | use(MockHelper) { |
| | testObj.myMethod() |
| | |
| | assertEquals 35, MockHelper.result |
| | } |
| | } |
| | } |
| | |
| | class MockHelper { |
| | def static result |
| | |
| | def static println(self, text) { result = text } |
| | |
| | def static someAction(CodeWithHeavierDependencies self) { 25 } ... |
Read now
Unlock full access