I have a problem using Spy in Spock, it either does not work as it should, or my understanding is wrong, so I'm trying to clarify this. Consider this code (Java):
public class CallingClass { public String functionOne() {
Now I want to check that functionTwo calls functionOne , and also determine the return value from functionOne (imagine, for example, if functionOne really complex and I donβt want to execute it, my test just wants to mute it and set it to return a certain value). To do this, write the following test in Groovy (using Spock):
class CallingClassTest extends Specification { def "check that functionTwo calls functionOne"() { def c = Spy(CallingClass) c.functionOne() >> "mocked function return" when: def s = c.functionTwo() then: 1 * c.functionOne() s == "some string mocked function return" } }
To do something like this, Spock asks me to have a cglib library, so my build file (in Gradle) looks something like this:
apply plugin: 'groovy' repositories { mavenCentral() } dependencies { compile 'org.codehaus.groovy:groovy:latest.release' testCompile 'org.spockframework:spock-core:latest.release' testCompile 'junit:junit:latest.release' testCompile 'cglib:cglib-nodep:latest.release' }
Now, when I run the test, I expect that functionOne will not be called at all, and instead my corrected version will be used. Instead, I get the following:
Condition not satisfied: s == "some string mocked function return" | | | false | 19 differences (44% similarity) | some string (-)o(-------)n(-------)e(----) | some string (m)o(cked fu)n(ction r)e(turn) some string one Condition not satisfied: s == "some string mocked function return" | | | false | 19 differences (44% similarity) | some string (-)o(-------)n(-------)e(----) | some string (m)o(cked fu)n(ction r)e(turn) some string one at CallingClassTest.check that functionTwo calls functionOne(CallingClassTest.groovy:17)
Moreover, if I debug this and set a breakpoint in functionOne , it gets a call: (
What am I missing? Shouldn't my test use a functionOne stub and just return the string "mocked function return" ?