Spock Has Event Hearing Listeners

Does spock have any kind of test event listener like TestNg does ITestListener

.

So that I can access when test cases fail, etc.

+3


source to share


2 answers


Spock has listeners. Unfortunately, the official documentation, different from others, has a "TODO" under Writing Custom Extensions: http://spockframework.github.io/spock/docs/1.0/extensions.html .

Update: The official docs have been updated to include useful information on custom extensions: http://spockframework.org/spock/docs/1.1/extensions.html . See below for details.

There are two ways: and Global .

annotation based

Three snippets here: annotation, extension and listener.

Annotation:

import java.lang.annotation.*
import org.spockframework.runtime.extension.ExtensionAnnotation

@Retention(RetentionPolicy.RUNTIME)
@Target([ElementType.TYPE, ElementType.METHOD])
@ExtensionAnnotation(ErrorListenerExtension)
@interface ListenForErrors {}

      

Extension (updated):

import org.spockframework.runtime.extension.AbstractAnnotationDrivenExtension
import org.spockframework.runtime.model.SpecInfo

class ListenForErrorsExtension extends AbstractAnnotationDrivenExtension<ListenForErrors> {
    void visitSpec(SpecInfo spec) {
        spec.addListener(new ListenForErrorsListener())
    }

   @Override
   void visitSpecAnnotation(ListenForErrors annotation, SpecInfo spec){
    println "do whatever you need here if you do. This method will throw an error unless you override it"
}
}

      

Listener:

import org.spockframework.runtime.AbstractRunListener
import org.spockframework.runtime.model.ErrorInfo

class ListenForErrorsListener extends AbstractRunListener {
    void error(ErrorInfo error) {
        println "Test failed: ${error.method.name}"
        // Do other handling here
    }
}

      



Then you can use your new annotation for the Spec class or method:

@ListenForErrors
class MySpec extends Specification {
    ...
}

      

Global

It also has three parts: extension, listener and registration.

class ListenForErrorsExtension implements IGlobalExtension {
    void visitSpec(SpecInfo specInfo) {
        specInfo.addListener(new ListenForErrorsListener())
    }
}

      

You can use the same class ListenForErrorsListener

as above.

To register an extension, create a file named org.spockframework.runtime.extension.IGlobalExtension

in the directory META-INF/services

. If you are using Gradle / Maven this will be under src/test/resources

. This file should only contain the fully qualified class name of your global extension, for example:

com.example.tests.ListenForErrorsExtension

      

Links

See Spock built-in extensions for an example: https://github.com/spockframework/spock/tree/groovy-1.8/spock-core/src/main/java/spock/lang https://github.com/spockframework/spock /tree/groovy-1.8/spock-core/src/main/java/org/spockframework/runtime/extension/builtin

+4


source


Spock interacts with Mock:

def "should send messages to all subscribers"() {
    given:
    def subscriber = Mock(Subscriber)

    when:
    publisher.send("hello")

    then:
    1 * subscriber.receive("hello")
}

      



See Interaction Based Testing in the docs

0


source







All Articles