Skip to content

thecodesmith/spock-override-extension

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

14 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Spock Override Extension

This extension enables selective override or ignore of inherited Spock features

Download Build Status Coveralls Coverage Status

Get Started

To use this extension, just add it as a test dependency and use the provided annotations.

Add the library to your build.gradle dependencies:

testCompile 'com.thecodesmith.spock:spock-override-extension:1.0.3'

These annotations will be available:

  • @IgnoreSuperSpecFeatures - This class-level annotation takes an array of method names (features) from the parent class to ignore
  • @OverrideSuperSpec - This method-level annotation overrides the parent feature method of the same method name

Use Case

Sometimes when using Spock you need to inherit feature methods from another Specification class, but need to override or ignore a few. This library provides the override/ignore capability through Spock's extension mechanism.

Below is an example. Note that this also works with non-abstract base classes.

abstract class BaseSpec extends Specification {

    @Shared String thing

    def 'base feature'() {
        expect: thing != null
    }

    def 'override me'() {
        expect: false
    }

    def 'ignore me'() {
        expect: false
    }
}
@IgnoreSuperSpecFeatures(['ignore me'])
class DerivedSpec extends BaseSpec {

    def setupSpec() {
        thing = 'foo'
    }

    def 'derived feature'() {
        expect: true
    }

    @OverrideSuperSpec
    def 'override me'() {
        expect: true
    }
}

Running DerivedSpec results in a green build:

√ (passed)  base feature
√ (passed)  derived feature
√ (passed)  override me
o (ignored) ignore me
Tests passed: 3, ignored: 1 of 4 tests

See UsageSpec.groovy for more examples.

Disclaimer

Most of the time there are better ways to structure tests than using inheritance, and this mechanism isn't necessary. The specific use case behind this extension was the need to run common tests across many modules in a multi-module project. Structuring the tests in this way allows the common tests to be included as a dependency in other modules, and customized as needed to fit the needs to the module. So, think twice before using this extension, but if you need it, you need it!

Contributing

Pull requests are welcome! If you see a missing feature, create a pull request and I will work to get it merged into the project. Reporting issues is a big help as well.

License

This library is licensed under the terms of the Apache License, Version 2.0.

About

Override or ignore inherited Spock feature methods

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages