Skip to content
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

Merged
merged 13 commits into from
Oct 14, 2022

Conversation

tprouvot
Copy link
Contributor

@tprouvot tprouvot commented Oct 11, 2022

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?

  • 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)

@tprouvot
Copy link
Contributor Author

Hi @adangel !
I may have missed some steps, it's the first time I create a new rule.
Feel free to reach me if you want any updates on the PR.

Copy link
Member

@adangel adangel left a 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...

@adangel adangel changed the title [apex] new rule ApexUnitTestClassShouldHaveRunAs #4149 [apex] New rule ApexUnitTestClassShouldHaveRunAs #4149 Oct 14, 2022
tprouvot and others added 6 commits October 14, 2022 09:21
…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>
@tprouvot
Copy link
Contributor Author

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 actu

Hi @adangel thanks for your review !
As you said this rule is pretty simple and a kind of copy of ApexUnitTestClassShouldHaveAsserts.

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.
We could say that we don't need runAs if we do not perform CRUD operation in the test method but it is hard to know when we call external methods which is frequently used to create test data..

I think this version is a good start and we will see if we need to extend the way it works.

Co-authored-by: Andreas Dangel <andreas.dangel@adangel.org>
@pmd-test
Copy link

1 Message
📖 Compared to master:
This changeset changes 0 violations,
introduces 141 new violations, 0 new errors and 0 new configuration errors,
removes 0 violations, 0 errors and 0 configuration errors.
Full report

Generated by 🚫 Danger

@adangel adangel added this to the 6.51.0 milestone Oct 14, 2022
Copy link
Member

@adangel adangel left a 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.

adangel added a commit to adangel/pmd that referenced this pull request Oct 14, 2022
adangel added a commit that referenced this pull request Oct 14, 2022
…unAs

[apex] New rule ApexUnitTestClassShouldHaveRunAs #4149 #4150
@adangel adangel merged commit a88ad1c into pmd:master Oct 14, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[apex] New rule: ApexUnitTestClassShouldHaveRunAs
3 participants