As of Spock 0.7, overriding test methods in subclasses isn't supported, and you'll have to find a different way to structure your tests. For example, you could use the template method pattern, where a test method in the base class calls some abstract or concrete helper methods, which are then implemented or overridden in subclasses.
Overridden spock test methods still running
-
30-06-2023 - |
Question
I have a series of classes that extend from a base class. I've written spock tests classes for the classes with a base test class which tests normal functionality but in the case of a few classes the standard test logic does not work.
My plan was to simply override the base test methods when needed, but it appears that spock still runs them.
Example:
Base test:
def "testing name"() {
expect:
assert STANDARD CODE HERE
}
Subclass test:
def "testing name"() {
expect:
assert CUSTOM CODE HERE
}
But when I run the test, the base test's method is still running and failing.
Solution
OTHER TIPS
You can make the original feature return early on a condition:
class BaseSpec extends Specification
@Shared
def ignored = [:] {
def myTest(){
if (ignored['myTest']) return
...
}
}
The derived class can set the ignore in the setupSpec:
class DerivedSpec extends BaseSpec
def setupSpec() {
ignored['myTest'] = true
}
def myTest(){
//override
}
}
Ugly, but it works.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow