Simple Mockito verify works in JUnit but not Spock

Roughly speaking, a then-block may only contain assertions in the form of boolean expressions. A Mockito verification expression doesn't fit this contract, as it will return a falsy value (null, false, 0) when it passes, which is interpreted as a failed assertion by Spock.

To solve this problem, you can either write a helper method that wraps around the verification expressions and always returns true, or you can use Spock's built-in mocking framework instead of Mockito.


I have a use case requiring PowerMockito to mock final methods in Java classes (where Spock mocking won't work), but also need to verify they did get called, because the final methods are builder-style and return "this", which makes the tests pass even if the mocked call wasn't called.

My solution was to append "|| true" to my verify calls, like this:

given:
when(myMock.setSomething("xyzzy")).thenReturn(myMock)

when:
def result = objectBeingTested.isExecutedWith("xyzzy")

then:
result == expectedResult
Mockito.verify(myMock).setSomething("xyzzy") || true         // this passes
Mockito.verify(myMock).setSomething("wrongValue") || true    // this FAILS appropriately