-
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
Fix incorrect javadoc of the Rule running order #1149
Fix incorrect javadoc of the Rule running order #1149
Conversation
Why this pull request is not getting merged for 4 months? |
Sorry for the delay. I just double-checked: Field rules are applied before method rules. Examplepublic class Example {
@Rule
public TestWatcher methodWatcher() {
return testWatcher("method");
}
@Rule
public TestWatcher fieldWatcher = testWatcher("field");
@Test
public void test() {
System.out.println("test");
}
private static TestWatcher testWatcher(final String prefix) {
return new TestWatcher() {
@Override
protected void starting(Description description) {
System.out.println(prefix + ": starting");
}
@Override
protected void finished(Description description) {
System.out.println(prefix + ": finished");
}
};
}
} This prints:
Do you have an example where methods are actually applied before fields? |
Actually, technically field rules are applied after method rules, but when a rule is applied the new rule runs "around" the existing rule chain. |
@marcphilipp
My Example: public class MethodOrfieldFirst {
@Rule
public TestRule fieldRule = new MyRule("FieldRule");
@Rule
public TestRule methodRule() {
return new MyRule("MethodRule");
}
@Test
public void test() {
System.out.println("Running Test");
}
static class MyRule implements TestRule {
private String name;
MyRule(String name) {
this.name = name;
}
@Override
public Statement apply(Statement base, Description description) {
System.out.println("applying: "+ name);
return base;
}
}
} This prints:
|
Thanks for your example. I now get what you meant. I think the original intention of the Javadoc was do describe the execution order of the How about:
Is that clearer? I'm not really satisfied with it and open to your suggestions. |
805654b
to
2c04652
Compare
Besides exchange the word
Is it better? |
Which rules don't add decorators to the statement chain? |
Some Rules added in the feature. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(doing a pass at trying to close old issues)
Let's try to get this merged.
* before Rules defined by methods. | ||
* undefined, in general. However, Rules defined by methods will always be applied | ||
* before Rules defined by fields. | ||
* Since almost all of the in-box {@link org.junit.rules.TestRule} implementations are adding |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
All in-box rule implementations are adding decorators (in fact, all rules are decorators) so this is confusing. Can we fix it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry for late response, I was on a long holiday without computers.
How about removing the word almost
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about "Since all {@link org.junit.rules.TestRule} implementations..." ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Implementing with decorator pattern is not a requirement. We should not say "all" with out the limitation "in-box". What do you think.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
All rules are decorate the statement chain. Even if a given rule doesn't "look like" a decorator it is one
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I could not get your point. I could not see any decorating in below implementation.
static class MyRule implements TestRule {
private String name;
MyRule(String name) {
this.name = name;
}
@Override
public Statement apply(Statement base, Description description) {
System.out.println("applying: "+ name);
return base;
}
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No one should write rules like that. The implementation of the rule should occur when the returned statement is run.
A test rule is a factory for decorators for statements. On very rare occasions, that factory might returned the passed-in statement but even then it is clearly the decorator pattern.
If you still disagree, let's drop this sentence completely.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I disagree. Let's drop this sentence and take Marc Philipp's
Rules defined by fields will always be applied after Rules defined by methods, i.e. the Statements returned by the former will be executed around those returned by the latter.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
SGTM
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated ^_^
2c04652
to
12f75db
Compare
In the javadoc of Rule and ClassRule, it says field rules are applied before method rules. Actually, method rules are applied before field rules.