3

I am using Spock to testing with groovy and I want a custom test rule on fail. First i did that with MethodRule which is deprecated now. So I tried TestWacher but Spock caches Exceptions before my rule can cache it.

    public class SimpleOnFailed extends TestWatcher {

    @Override
    protected void failed(Throwable e, Description description) {
        print "Prints when test fails"
    }

}

and it never prints my print statement from failed method.

4

1 回答 1

3

There is a known issue with Spock and TestRules throwing exceptions that's not easy to fix because Spock's semantics differ from JUnit's in this regard. (If you are interested in the details, you can find out more at http://forum.spockframework.org.) Solutions are to use a MethodRule instead (which is no longer deprecated in latest JUnit) or write a Spock extension. The latter isn't yet considered a stable SPI, but if you can live with potential changes, have a look at the extensions in the Spock codebase (http://github.spockframework.org).

于 2013-07-19T19:59:40.680 回答