MyService.class
public boolean myMethod1() {
boolean success = false
for (1..2) {
success = myMethod2()
}
return success
}
public boolean myMethod2() {
int value = otherService.someMethod() // mocked this method call
boolean saved = false
try {
// trying to persist value
} catch (ValidationException e) {
if (someCondition)
myMethod2() // comes back here instead of going to the method called : "Problem line"
}
return saved
}
I have a test case which has mocked otherService.someMethod()
to return different result for every execution of it.
Using groovy way not grails Grails bug - http://jira.grails.org/browse/GRAILS-4611
When there is a validation exception and upon certain condition i want to call myMethod2()
recursively to get a new output for saving data.
mocked method closure will return a output in such a way that the first pass will return int, which will internally return (true/false) back to the myMethod1()
.
Next(from the loop) time when the otherService.someMethod()
is called it will return an output which will cause a validationException
and
it will call the same method for new output. Now when the mocked method is called for the 3rd time, the mocked
output will return a different value which will not cause validationException. After executing try block,
it comes to the return statement of myMethod2()
but it goes back to "Problem line" instead of going back to myMethod1()
, the one who invoked it.
How to make the test case to call back to myMethod1()
I thought when the return saved
is called it would go to the parent api who invoked it. But according to the stack (thread) it goes to the :Problem Line
.
What i did was, in the catch block, instead of calling just myMethod2()
I modified it to return myMethod2()
It eventually returned true/false
according to the business logic. And the test case also passed.
Thanks for your input dmahapatro