Search code examples
groovyspock

Is it posible to intercept a Spock feature's execution just prior to the evaluation of a feature's cleanup block?


Is there any way for a custom Spock annotation to intercept a feature's execution prior to the execution of each cleanup block? I realize that I can intercept prior to the execution of the spec's cleanup() method, but I was hoping to intercept the feature prior to the content of the cleanup block running.

For example:

class Extension extends AbstractAnnotationDrivenExtension<Extension>{

  @Override
  void visitSpec(SpecInfo s){
    s.addCleanupInterceptor(new Inter())
  }

}
class Inter implements IMethodInterceptor{

  @Override
  void intercept(IMethodInvocation inv){
    println inv.getMethod().getKind()
    inv.proceed()
  }

}
@CatchErrors
class SampleTest extends Specification {

  def "A sample feature"(){
    when:
    foo()

    then:
    false 

    cleanup:
    println "In cleanup block"
    bar()

  }
}

Which outputs: In cleanup block \n CLEANUP

In the above example, I want to intercept prior to the execution of that final cleanup block. I have attempted to register interceptor via addCleanupInterceptor when I'm in visitSpec(), but to no avail. I don't seem to hit this interceptor until after all blocks of the feature have been executed, instead of before bar() is run.


Solution

  • The answer is no. This has been requested before in Spock issue #538, but has not been implemented yet. Even with Spock 2.0 (based on JUnit 5 Jupiter and compatible with Groovy 3) in front of the door (milestone 2 build has been released a while ago), this feature does not seem to be planned for the release.