-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Verify that all (base-) classes declaring @ClassRules are public #764
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
Conversation
@@ -61,6 +61,11 @@ public Field getField() { | |||
public Class<?> getType() { | |||
return fField.getType(); | |||
} | |||
|
|||
@Override | |||
public Class<?> getDeclaringClass() { |
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.
It would be nice if getDeclaringClass()
returns a TestClass
object, which is JUnit's wrapper for Class objects. Then you can add an isPublic()
method to TestClass
and avoid the duplicate Modifier.isPublic(aClass.getModifiers())
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.
Indeed, that would make the checks simpler. However, TestClass
does quite a few things in its constructor which are not necessary whatsoever to fix this bug. Are you sure you want to have getDeclaringClass()
return a TestClass
?
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.
Just two suggestions for improvement. Everything else is fine. Thanks @ferstl. |
@stefanbirkner I made two new commits for each of your comments. I'll squash them if these changes are OK for everyone. |
I would indeed really like not to call the expensive TestClass constructor in multiple places. I agree that going back to getDeclaringClass() returning a simple Class object is the right approach. Sorry I wasn't able to jump into the discussion before now. |
@dsaff Ok, I removed that change. |
Looks good to me. Please squash! |
- Add the new method getDeclaringClass() to FrameworkMember - FrameworkMethod will return the class where the method is actually declared - FrameworkField will return the class where the field is actually declared - Add validation logic to RuleFieldValidator - Add test for validation logic
@dsaff Done. |
Verify that all (base-) classes declaring @classrules are public
Thanks! |
Just catching up on (very) old JUnit updates. I think returning a @dsaff What do you think of caching a map from |
An
IllegalAccessException
is thrown in case a test class derives from a non-public base class which declares a@ClassRule
.For example, this code ...
… will cause this exception
To solve this issue, I added an additional verification to
RuleFieldValidator
that checks that all@ClassRule
s are declared in public classes. A similar validation is already done for@BeforeClass
and@AfterClass
methods.