-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
[apex] New rule ApexUnitTestClassShouldHaveRunAs #4149 #4150
[apex] New rule ApexUnitTestClassShouldHaveRunAs #4149 #4150
Conversation
Hi @adangel ! |
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 for your PR!
Please have a look at my comments.
Currently the rule basically only checks, whether in the test method at least once System.runAs
has been used. Is this sufficient? It's a very simple check... (but ApexUnitTestClassShouldHaveAsserts does only check for at least one assert as well).
Maybe this simple check is enough for the beginning, maybe not - I don't know. That would add up to false-negatives - e.g. Statements that are outside of a RunAs block, but should actually have been inside a RunAs block.
And it will really flag all tests, that don't use System.runAs - maybe there are cases where you don't need a user, since you are testing just some plain logic. That would add up to false-positives.
I'm not an Apex developer, so I can't tell.
The documentation on salesforce also doesn't have a real example, because you won't test a System.debug call as they do in a RunAs... and there are no asserts in the examples as well...
...a/net/sourceforge/pmd/lang/apex/rule/bestpractices/ApexUnitTestClassShouldHaveRunAsRule.java
Outdated
Show resolved
Hide resolved
...es/net/sourceforge/pmd/lang/apex/rule/bestpractices/xml/ApexUnitTestClassShouldHaveRunAs.xml
Outdated
Show resolved
Hide resolved
...a/net/sourceforge/pmd/lang/apex/rule/bestpractices/ApexUnitTestClassShouldHaveRunAsRule.java
Outdated
Show resolved
Hide resolved
…practices/ApexUnitTestClassShouldHaveRunAsRule.java Co-authored-by: Andreas Dangel <andreas.dangel@adangel.org>
Co-authored-by: Andreas Dangel <andreas.dangel@adangel.org>
…practices/ApexUnitTestClassShouldHaveRunAsRule.java Co-authored-by: Andreas Dangel <andreas.dangel@adangel.org>
Hi @adangel thanks for your review ! As you mentioned it, it may exist some cases where we don't need a user to run the test but most of the time we do. I think this version is a good start and we will see if we need to extend the way it works. |
…om/tprouvot/pmd into feature/addApexUnitShouldHaveRunAs
Co-authored-by: Andreas Dangel <andreas.dangel@adangel.org>
Generated by 🚫 Danger |
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.
LGTM, thanks! 🎉
I think this version is a good start and we will see if we need to extend the way it works.
Yes, let's see how the feedback is.
I'll merge it later today.
Describe the PR
According to Salesforce best practices, when executing test method we should not rely on the user's running context.
The use of System.runAs is recommended to execute the test with a newly created user so that the test does not rely on environment data and user's configuration
Related issues
Ready?
./mvnw clean verify
passes (checked automatically by github actions)