-
Notifications
You must be signed in to change notification settings - Fork 3.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Is it / should it be possible to have a single rule act before, between, and after tests? #793
Comments
In my opinion forcing rules to be non-static was an annoying change and an absolutely unnecessary restriction. |
@phoenix384 I would be willing to accept a pull request where fields annotated with both |
@kcooney That sounds perfectly reasonable to me, and would satisfy my use case. I'm willing to at least look into submitting a pull request. I'll post back here if I'm not going to be able to sort that out in the short term. |
Thanks to @rowanhill fields and methods annotated with I still think we need a real solution to the problem of having a single entity that can act as a class rule and a test rule, so I'm leaving this bug open. |
...except that such a field cannot implement
I couldn't agree more! |
@sbrannen A It sounds like you want an API that works on the test method level and the class level. If someone has ideas for how we might design a new API that works on the class level and method level, I suggest writing up a concrete proposal in Google Docs and adding a link to that doc on this bug |
Woops! Didn't mean to close this. |
@kcooney, while it's certainly true that a See my comment here: #351 (comment) Now, I agree that we should define a new API, but I would slate that for JUnit 5 instead of 4.x. In 4.13 we have the opportunity to support a rule that is applied at both the class level and method level and has access to the test instance. As it stands now, it is possible to have a rule that is applied at both the class level and method level, but such a rule does not have access to the test instance since it cannot implement And well, that's a bit of a lie: it can implement In summary, what I'm suggesting is that JUnit -- for method-level application -- always invoke a rule as a Make sense? Cheers, Sam |
I think it's would be confusing and possibly error prone to have a rule be called via the We might skip 4.13 and jump directly to 5.0, so now is a good time to think of a replacement API. |
If we don't want to allow Something like the following could work: public interface ClassAndMethodRule {
Statement applyAtClassLevel(Statement base, Description description);
Statement applyAtMethodLevel(Statement base, Description description, Object testInstance);
// or
// Statement applyAtMethodLevel(Statement base, FrameworkMethod method, Object testInstance);
// or
// Statement applyAtMethodLevel(Statement base, Description description, FrameworkMethod method, Object testInstance);
} Thoughts? |
What about the idea of decorators I suggested in proposal 2 for JUnit Lambda: https://github.com/junit-team/junit-lambda/blob/master/src/test/java/org/junit/lambda/proposal02/examples/DecoratedTests.java Johannes Von meinem iPad gesendet
|
@jlink I would.love to.use annotations to.define rules, but there are a few problems.with having a class-level annotation for applying rules. The order of class annotations are you nefined, and annotations would limit what data you could pass in to create the Rule |
@sbrannen thanks for the proposal. We would need to define how you apply instances of the new rule type to a test, how you order them, and how they interact with Again I find it easier to discuss bigger design proposals in a Google Doc, but we can try to discuss here |
2015-05-16 15:57 GMT+02:00 Kevin Cooney [email protected]:
Johannes
|
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:
@ClassRule
and@Rule
implementations.@ClassRule
field, then declare a non-static@Rule
and set it's value to that of the static field.@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.
The text was updated successfully, but these errors were encountered: