what does >> and 1* means in this groovy statement?
Asked Answered
G

1

37

I am working on grails/groovy project and while reading their test documentation came across this line of code

1 * myService.prova() >> { true }

I am unable to understand what does 1 * means here also as >> is right shift operator what does it mean here ? I have searched so much but unable to get any proper explanation. Grails Test Documenation

Grannias answered 4/8, 2015 at 6:17 Comment(0)
E
45

This is not groovy per se, but the testing framework called Spock (which is very popular among Groovy developers, for good reasons :-) - http://spockframework.github.io/spock/docs/1.0/index.html

This expression in particular is a way to instruct Spock that it should expect exactly one call to the method prova in myService, and that this call should be mocked to return true. See Interaction based testing , in particular the section called Combining Mocking and Stubbing.

Elizbeth answered 4/8, 2015 at 6:27 Comment(1)
The link in my response (spockframework.github.io/spock/docs/1.0/index.html) is pretty exhaustive regarding mocking with Spock. There's also grails.github.io/grails-doc/latest/guide/… which refers to the same guide (link seems to be broken ATM). In the Grails guide you can also find how to mock using meta-programming, but I'd try to stick to Spock if that's what you are already using.Elizbeth

© 2022 - 2024 — McMap. All rights reserved.