O'Reilly logo

Programming Groovy 2 by Venkat Subramaniam

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required