I just put this test method together:
@Unroll
def 'super start edit should be called if cell is not empty'( boolean empty ){
given:
DueDateEditor editor = GroovySpy( DueDateEditor ){
isEmpty() >> empty
}
when:
editor.startEdit()
then:
if( empty){
0 * editor.callSuperStartEdit()
}
else {
1 * editor.callSuperStartEdit()
}
where:
empty | _
true | _
false | _
}
... it works OK in terms of the two tests passing... but when you make it fail it's very odd: the output if the parameter empty is false is
super start edit should be called if cell is not empty[1]
... and it is 0 if the parameter empty is true. Is this a bug?
I am writing an additional answer because
GroovySpyhere, a simpleSpyabsolutely suffices,isEmpty(),General remarks:
P.S.: Sorry, I had to make up a sample class under test
DueDateEditorin order to make my test compile and run as expected. As usual, Mike unfortunately didn't provide an MCVE but just a part of it.Update: We talked about
GroovySpyin our comments and, as I said, it will not work if your classes are Java classes and there is a final method in you want to stub, see the Spock manual. Here is proof for you:The test would work if your application classes were Groovy classes only. But if they are Java classes like in my example, the test will fail like this:
So in this case you cannot just use Groovy magic to check interactions. But as I said, you shouldn't do that anyway. Rather make sure that both
startEdit()andcallSuperStartEdit()do the right things. Check their results or, if they arevoid, check their side effects on the state of the subject under test or its collaborators.Update 2: Regarding your original question about indexed method naming, actually @tim_yates gave the correct answer. I just want to add the corresponding Spock manual link explaining method unrolling and how you can influence naming using variables from the
where:block.