Description
Hi,
As of 4.11 (and specifically #343 / #339), it's no longer possible to annotate a single rule as both @ClassRule
and @Rule
(as the former must be static and the latter non-static).
Is it still possible to have a single rule which acts both before & after the class' tests (e.g. to set up and tear down an external resource) and between each tests (e.g. to reset the external resource)?
If not, is there any appetite for allowing such a thing?
Note that there are currently work-arounds:
- Split the responsibilities into independent
@ClassRule
and@Rule
implementations. - Define a static
@ClassRule
field, then declare a non-static@Rule
and set it's value to that of the static field. - Create a
@Rule
that keeps static state (e.g. the external resource), and infers whether it should perform a before-class type action (e.g. set up the resource) or a per-test type action (e.g. reset the resource).
They have drawbacks, however: 1 and 2 require a developer to remember to do two things when writing their test (i.e. there are two coupled rules, and the test won't work as they expect if either is missing); 3 doesn't allow for after-class type actions (e.g. cleaning up an external resource), although these can be provided via an @AfterClass
method (although, again, that couples two elements in the test).
I'd be interested to hear of any other suggestions (either in terms of workarounds or, if there is any interest, in how support for this could be implemented as a JUnit feature).
Thanks,
Rowan
P.S. See also my question on StackOverflow about the same subject.