I have a service class which has 2 methods(one is public and other is protected). I am trying to unit test my code. Below is the code snippet MyServiceClass.groovy
class MyServiceClass {
public boolean myMethod1() {
// some code
boolean success = myMethod2()
// some code
}
public boolean myMethod2() {
// some logic to return true/false
return true
}
}
MyServiceClassTests.groovy
class MyServiceClassTests {
void testMyMethod1() {
// unit test code
}
}
In the above unit test code, I want to mock the myMethod2() return result which is invoked by myMethod1(), i.e., both the methods are in the same service class which is under unit test. How to mock it and get things done??
Have you tried to metaClass?
http://groovy.codehaus.org/JN3525-MetaClasses
I abuse metaClass probably more than I should but it's great when you can't come up with a better way of doing things.