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
In groovy, the bitwise operators can be overridden with the leftShift (<<) and rightShift (>>) methods defined on the class. It's idiomatic groovy to use the leftShift method for append actions on strings, buffers, streams, arrays, etc and thats what you're seeing here.
It is used to separate where you declare bindings for your closure from the actual code, eg: def myClosure = { x, y -> x + y } the part before -> declares that the closure has two arguments named x and y while the second part is the code of the closure.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With