jtanistra / spock

Automatically exported from code.google.com/p/spock
0 stars 0 forks source link

Support JUnit's TestRule annotation #206

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
It would be nice if I could count on any JUnit method rules being applied 
before the setup() method and before initialisation of any non shared ivars.

Original issue reported on code.google.com by luke.da...@gradle.biz on 30 Sep 2011 at 1:01

GoogleCodeExporter commented 8 years ago
Unfortunately that's not how JUnit rules work, and we have to comply with their 
semantics. But you can always write a Spock  extension to achieve that.

In JUnit 4.9 they've introduced two new kinds of rules (MethodRule and 
ClassRule) and Spock 0.6 will support them both. Not sure if it solves your 
problem but maybe it does (I know it was a frequent complaint).

Original comment by pnied...@gmail.com on 30 Sep 2011 at 1:31

GoogleCodeExporter commented 8 years ago
What if we had a special annotation that changed the semantics to do what I am 
talking about? So you could use MethodRule impls, but change their lifecycle.

Original comment by luke.da...@gradle.biz on 30 Sep 2011 at 1:34

GoogleCodeExporter commented 8 years ago
I think it's better to add support for JUnit 4.9's TestRule, which will solve 
your problem. A TestRule annotated with @Rule will run "around" setup() and 
cleanup(). If annotated with @ClassRule, it will run "around" setupSpec() and 
cleanupSpec(). TestRule replaces MethodRule, which has been deprecated.

Alternatively, you can always write a Spock extension.

Original comment by pnied...@gmail.com on 3 Oct 2011 at 1:15

GoogleCodeExporter commented 8 years ago
Supporting TestRule sounds good. In this case it's one of the rules we use in 
Gradle so an extension would be more work.

Original comment by lda...@gmail.com on 3 Oct 2011 at 9:00

GoogleCodeExporter commented 8 years ago

Original comment by lda...@gmail.com on 3 Oct 2011 at 9:01

GoogleCodeExporter commented 8 years ago

Original comment by pnied...@gmail.com on 4 Oct 2011 at 8:00

GoogleCodeExporter commented 8 years ago
There is now full support for TestRule, including @ClassRule. The only 
limitation is that test failures will be reported even if a TestRule suppresses 
them. This is because Spock and JUnit report failures at different times. Maybe 
this can be reconciled, but I’m not sure.

Original comment by pnied...@gmail.com on 19 Jan 2012 at 2:43