Does spock has any Test event listener like how TestNg has ITestListener
. ?
So that I can have access, when the test cases failed etc.
The main difference between Spock and JUnit is in breadth of capabilities. Both frameworks can be used for unit testing. However, Spock offers much more — including mocking and integration testing. JUnit requires a third party library for mocking.
Both Groovy and Java build and run on the JVM. A large enterprise build can run both JUnit and Spock tests in the same time. Spock uses the JUnit runner infrastructure and therefore is compatible with all existing Java infrastructure. For example, code coverage with Spock is possible in the same way as JUnit.
Listeners in Selenium possess the ability to listen to any events like entering data, a button click, navigation to a page, an exception, etc. These are defined with the help of an interface in Selenium and help to customize system behavior by performing actions on events as defined in code.
Spock does have listeners. Unfortunately the official documentation, which is otherwise excellent, has "TODO" under Writing Custom Extensions: http://spockframework.github.io/spock/docs/1.0/extensions.html.
Update: The official docs have been updated to include helpful information about custom extensions: http://spockframework.org/spock/docs/1.1/extensions.html. See those for more details.
There are two ways: Annotation-based and Global.
Annotation-based
Three pieces here: the annotation, the extension, and the listener.
The annotation:
import java.lang.annotation.*
import org.spockframework.runtime.extension.ExtensionAnnotation
@Retention(RetentionPolicy.RUNTIME)
@Target([ElementType.TYPE, ElementType.METHOD])
@ExtensionAnnotation(ListenForErrorsExtension)
@interface ListenForErrors {}
The 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"
}
}
The 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
}
}
You can then use your new annotation on a Spec class or method:
@ListenForErrors
class MySpec extends Specification {
...
}
Global
This also has three pieces: the extension, the listener, and the registration.
class ListenForErrorsExtension implements IGlobalExtension {
void visitSpec(SpecInfo specInfo) {
specInfo.addListener(new ListenForErrorsListener())
}
}
You can use the same ListenForErrorsListener
class as above.
To register the extension, create a file named org.spockframework.runtime.extension.IGlobalExtension
in the META-INF/services
directory. If using Gradle/Maven, this will be under src/test/resources
. This file should contain only the fully qualified class name of your global extension, for example:
com.example.tests.ListenForErrorsExtension
References
For examples, see the Spock built-in extensions here: 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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With