How can an annotated class be mocked (or spied) in Spock so that logic which relies on the annotation can be asserted?
A contrived example in Groovy (could be Java too) shows a method that checks for a specific annotation value in a collection of objects. Imagine more complex logic performed after filtering by annotation, which I'd like to assert by mocking objects in the collection.
@FooAnnotation('FOOBAR')
class MainGroovy {
def findFOOBARs(Object... candidates) {
candidates.findAll{ it.class.getAnnotation(FooAnnotation)?.value() == 'FOOBAR' }
//Do something with the filtered objects.
}
}
Passing a Spy fails the annotation filter, so nothing can be asserted about any subsequent logic.
@Unroll
def test() {
given:
def foobars = mg.findFOOBARs(mg, new Object(), 'STRING')
expect:
foobars.size() == 1
where:
mg << [new MainGroovy(), Spy(MainGroovy)]
}