Mocking by Overriding
Suppose we have a class that depends on a method that does some significant work and takes substantial time and resources, such as the following myMethod:
UnitTestingWithGroovy/com/agiledeveloper/CodeWithHeavierDependencies.groovy | |
| package com.agiledeveloper |
| |
| public class CodeWithHeavierDependencies |
| { |
| public void myMethod() |
| { |
| def value = someAction() + 10 |
| |
| println(value) |
| } |
| |
| int someAction() |
| { |
| Thread.sleep(5000) // simulates time consuming action |
| |
| return Math.random() * 100 // Simulated result of some action |
| } |
| } |
We’re interested in testing myMethod (which belongs to CodeWith-HeavierDependencies). However, the method depends on someAction, which simulates a time- and resource-consuming ...
Get Programming Groovy 2 now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.