Permalink
Browse files

@Rule fields/methods must not be static

Fixes KentBeck/junit#339
  • Loading branch information...
1 parent 849a16f commit b4f0afa639b42fa551fdcb26d9c1855ae4d778cf @marcphilipp committed Oct 16, 2011
@@ -43,13 +43,13 @@
private final Class<? extends Annotation> fAnnotation;
- private final boolean fOnlyStaticMembers;
+ private final boolean fStaticMembers;
private final boolean fMethods;
private RuleFieldValidator(Class<? extends Annotation> annotation,
- boolean methods, boolean fOnlyStaticMembers) {
+ boolean methods, boolean fStaticMembers) {
this.fAnnotation= annotation;
- this.fOnlyStaticMembers= fOnlyStaticMembers;
+ this.fStaticMembers= fStaticMembers;
this.fMethods= methods;
}
@@ -68,15 +68,17 @@ public void validate(TestClass target, List<Throwable> errors) {
}
private void validateMember(FrameworkMember<?> member, List<Throwable> errors) {
- optionallyValidateStatic(member, errors);
+ validateStatic(member, errors);
validatePublic(member, errors);
validateTestRuleOrMethodRule(member, errors);
}
- private void optionallyValidateStatic(FrameworkMember<?> member,
+ private void validateStatic(FrameworkMember<?> member,
List<Throwable> errors) {
- if (fOnlyStaticMembers && !member.isStatic())
+ if (fStaticMembers && !member.isStatic())
addError(errors, member, "must be static.");
+ if (!fStaticMembers && member.isStatic())
+ addError(errors, member, "must not be static.");
}
private void validatePublic(FrameworkMember<?> member, List<Throwable> errors) {
@@ -60,6 +60,18 @@ public void acceptNonStaticTestRule() {
}
@Test
+ public void rejectStaticTestRule() {
+ TestClass target= new TestClass(TestWithStaticTestRule.class);
+ RULE_VALIDATOR.validate(target, errors);
+ assertOneErrorWithMessage("The @Rule 'temporaryFolder' must not be static.");
+ }
+
+ public static class TestWithStaticTestRule {
+ @Rule
+ public static TestRule temporaryFolder = new TemporaryFolder();
+ }
+
+ @Test
public void acceptMethodRule() throws Exception {
TestClass target= new TestClass(TestWithMethodRule.class);
RULE_VALIDATOR.validate(target, errors);
@@ -126,6 +138,18 @@ public void acceptMethodNonStaticTestRule() {
}
@Test
+ public void rejectMethodStaticTestRule() {
+ TestClass target= new TestClass(TestMethodWithStaticTestRule.class);
+ RULE_METHOD_VALIDATOR.validate(target, errors);
+ assertOneErrorWithMessage("The @Rule 'getTemporaryFolder' must not be static.");
+ }
+
+ public static class TestMethodWithStaticTestRule {
+ @Rule
+ public static TestRule getTemporaryFolder() { return new TemporaryFolder(); }
+ }
+
+ @Test
public void methodAcceptMethodRuleMethod() throws Exception {
TestClass target= new TestClass(MethodTestWithMethodRule.class);
RULE_METHOD_VALIDATOR.validate(target, errors);

0 comments on commit b4f0afa

Please sign in to comment.