GeezerVison is showing github.com — [java] New rule: JUnit5TestShouldBePackagePrivate by ajeans · Pull Request #3247 · pmd/pmd
Conversation
ajeans
commented
May 4, 2021
•
edited by adangel
Loading
Uh oh!
There was an error while loading. Please reload this page.
Add the rule to check that JUnit5 tests methods and classes are package private.
Add unit tests
Describe the PR
Propose a codestyle rule for Jupiter / JUnit5 tests according to the feedback received from @adangel in the issue.
Not sure how to add the 'externalInfoURL', so simply pointed at the rule name, assuming that the site is generated from the XML rule file
Related issues
Ready?
- Added unit tests for fixed bug/feature
- Passing all unit tests
- Complete build
./mvnw clean verify passes (checked automatically by github actions)
- Added (in-code) documentation (if needed)
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
adangel reacted with rocket emoji
…vate'
Add the rule to check that JUnit5 tests methods and classes are package
private.
Add unit tests
AFAICT the site is generated from the XML rule file, so simply add an externalInfoUrl that points at where the rule should end up.
Also put in the correct since date.
oowekyala
changed the title
issue 3239 Implement best practice rule 'JUnit5TestShouldBePackagePri…
[java] New rule: JUnit5TestShouldBePackagePrivate
May 4, 2021
ghost
commented
May 4, 2021
•
edited by ghost
Loading
Uh oh!
There was an error while loading. Please reload this page.
|
1 Message
|
| 📖 |
This changeset changes 0 violations,
introduces 0 new violations, 0 new errors and 0 new configuration errors,
removes 0 violations, 0 errors and 0 configuration errors.
Full report |
| ✅ |
This changeset changes 0 violations,
introduces 2 new violations, 0 new errors and 0 new configuration errors,
removes 0 violations, 0 errors and 0 configuration errors.
Full report |
| ✅ |
This changeset changes 0 violations,
introduces 45 new violations, 0 new errors and 0 new configuration errors,
removes 0 violations, 0 errors and 0 configuration errors.
Full report |
| ✅ |
This changeset changes 0 violations,
introduces 2268 new violations, 15 new errors and 0 new configuration errors,
removes 1877 violations, 0 errors and 0 configuration errors.
Full report |
| ✅ |
This changeset changes 0 violations,
introduces 2268 new violations, 15 new errors and 0 new configuration errors,
removes 1877 violations, 0 errors and 0 configuration errors.
Full report |
| ✅ |
This changeset changes 0 violations,
introduces 2268 new violations, 15 new errors and 0 new configuration errors,
removes 1877 violations, 0 errors and 0 configuration errors.
Full report |
Generated by 🚫 Danger
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
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.
Welcome and thanks for your work on this! I have a couple of suggestions for improvements, please take a look and push new commits when you can
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
ajeans reacted with thumbs up emoji
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Tweak descriptions, add unit test for Junit4...
rather than the body declaration
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
|
@ajeans This new rule detects two violations in the spring-framework: https://chunk.io/pmd/0094edd7ef3f417898be43865c65ec41/diff/spring-framework/index.html#section-violations
Both are private static inner classes. Are these valid cases?
Since they are private, I thought, they should only be picked up by the other (to be implemented) rule JUnit5TestNoPrivateModifier...
We should avoid to report these cases twice.
Besides that, these two cases might be a false positive for JUnit5TestNoPrivateModifier anyway - but probably not so easy to detect. It seems, that they use org.junit.platform.engine.discovery.DiscoverySelectors, which seems to allow private test classes...
So for this rule, I'd suggest to exclude private classes/methods, as this rule should only flag cases, where the class/method is public/protected. This should be possible by restricting the query additionally via [@Private=false()].
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
|
Thanks for the feedbacl @adangel
@ajeans This new rule detects two violations in the spring-framework: https://chunk.io/pmd/0094edd7ef3f417898be43865c65ec41/diff/spring-framework/index.html#section-violations
* https://github.com/spring-projects/spring-framework/tree/v5.0.6.RELEASE/spring-test/src/test/java/org/springframework/test/context/junit/jupiter/FailingBeforeAndAfterMethodsSpringExtensionTestCase.java#L246
* https://github.com/spring-projects/spring-framework/tree/v5.0.6.RELEASE/spring-test/src/test/java/org/springframework/test/context/junit/jupiter/FailingBeforeAndAfterMethodsSpringExtensionTestCase.java#L260
Both are private static inner classes. Are these valid cases?
It appears they are, those are inner static classes annotated with @Test and fed through a @TestFactory to test a JUnit extension.
Since they are private, I thought, they should only be picked up by the other (to be implemented) rule JUnit5TestNoPrivateModifier...
We should avoid to report these cases twice.
Agreed, I added two unit tests and changed the rule to target protected and public modifiers, and ignore private modifiers.
Besides that, these two cases might be a false positive for JUnit5TestNoPrivateModifier anyway - but probably not so easy to detect. It seems, that they use org.junit.platform.engine.discovery.DiscoverySelectors, which seems to allow private test classes...
That's using the jupiter engine dependency directly, so definitely advanced usage. The rule is targeted at devs that simply use the test annotations for simple and documented use cases. If you are doing the test discovery using an hardcoded list of test classes, you're way outside the junit user guide, and this rule doesn't make a lot of sense...
We can have this conversation when we implement the second rule in a separate MR but I would argue that this is an advanced usage and people should simply annotate the class to ignore the rule.
So for this rule, I'd suggest to exclude private classes/methods, as this rule should only flag cases, where the class/method is public/protected. This should be possible by restricting the query additionally via [@Private=false()].
Done with latest commit, hope this is what you had in mind.
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
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.
Thanks! Looks good to me!
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Reviewers
adangel
adangel approved these changes
Uh oh!
There was an error while loading. Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
|