GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — [apex] New rule ApexUnitTestClassShouldHaveRunAs #4149 by tprouvot · Pull Request #4150 · pmd/pmd

Conversation

@tprouvot

@tprouvot tprouvot commented Oct 11, 2022 •
edited by adangel
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor

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

  • Fixes [apex] New rule: ApexUnitTestClassShouldHaveRunAs #4149

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.

All reactions
tprouvot and others added 4 commits August 30, 2022 09:11
@tprouvot
Add new Assert class updates
986192b
@tprouvot
Merge branch 'pmd:master' into master
5d4457b
@tprouvot
Merge branch 'pmd:master' into master
c0f88d0
@tprouvot
Add new apex rule ApexUnitTestClassShouldHaveRunAs
a48b56b
@tprouvot

tprouvot commented Oct 12, 2022

Copy link
Copy Markdown
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.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

adangel
adangel requested changes Oct 14, 2022
View reviewed changes

@adangel adangel left a comment

Copy link
Copy Markdown
Member

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...

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread ...a/net/sourceforge/pmd/lang/apex/rule/bestpractices/ApexUnitTestClassShouldHaveRunAsRule.java Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread ...es/net/sourceforge/pmd/lang/apex/rule/bestpractices/xml/ApexUnitTestClassShouldHaveRunAs.xml Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread ...a/net/sourceforge/pmd/lang/apex/rule/bestpractices/ApexUnitTestClassShouldHaveRunAsRule.java Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread pmd-apex/src/main/resources/rulesets/apex/apexunit.xml Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread pmd-apex/src/main/resources/rulesets/apex/quickstart.xml Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread pmd-apex/src/main/resources/category/apex/bestpractices.xml Resolved

Uh oh!

There was an error while loading. Please reload this page.

@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
@tprouvot @adangel
Update pmd-apex/src/main/java/net/sourceforge/pmd/lang/apex/rule/best…
0da5cc9
…practices/ApexUnitTestClassShouldHaveRunAsRule.java

Co-authored-by: Andreas Dangel <[email protected]>
@tprouvot
revert changes on deprecvated rule
d6f3b3c
@tprouvot
Add a test for a normal class
ef48c76
@tprouvot
Set default message and priority to the rule
3cd67ea
@tprouvot @adangel
Update pmd-apex/src/main/resources/category/apex/bestpractices.xml
bac0e6e
Co-authored-by: Andreas Dangel <[email protected]>
@tprouvot @adangel
Update pmd-apex/src/main/java/net/sourceforge/pmd/lang/apex/rule/best…
150f109
…practices/ApexUnitTestClassShouldHaveRunAsRule.java

Co-authored-by: Andreas Dangel <[email protected]>
@tprouvot

tprouvot commented Oct 14, 2022

Copy link
Copy Markdown
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.

adangel reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

tprouvot added 2 commits October 14, 2022 10:24
@tprouvot
Remove unused imports
d0d2d2a
@tprouvot
Merge branch 'feature/addApexUnitShouldHaveRunAs' of https://github.c…
a72f133
…om/tprouvot/pmd into feature/addApexUnitShouldHaveRunAs
adangel
adangel reviewed Oct 14, 2022
View reviewed changes
Comment thread pmd-core/src/main/resources/rulesets/releases/6510.xml Resolved

Uh oh!

There was an error while loading. Please reload this page.

@tprouvot @adangel
Update pmd-core/src/main/resources/rulesets/releases/6510.xml
a88ad1c
Co-authored-by: Andreas Dangel <[email protected]>
@ghost

ghost commented Oct 14, 2022

Copy link
Copy Markdown
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

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@adangel adangel added this to the 6.51.0 milestone Oct 14, 2022
adangel
adangel approved these changes Oct 14, 2022
View reviewed changes

@adangel adangel left a comment

Copy link
Copy Markdown
Member

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.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

tprouvot reacted with thumbs up emoji tprouvot reacted with hooray emoji tprouvot reacted with rocket emoji
All reactions
  • 👍 1 reaction
  • 🎉 1 reaction
  • 🚀 1 reaction
adangel added a commit to adangel/pmd that referenced this pull request Oct 14, 2022
@adangel
[doc] Update release notes (pmd#4149, pmd#4150)
8834af1
adangel added a commit that referenced this pull request Oct 14, 2022
@adangel
Merge pull request #4150 from tprouvot:feature/addApexUnitShouldHaveR…
d575586
…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

Reviewers

@adangel adangel adangel approved these changes

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

6.51.0

Development

Successfully merging this pull request may close these issues.

[apex] New rule: ApexUnitTestClassShouldHaveRunAs

Uh oh!

There was an error while loading. Please reload this page.

2 participants

@tprouvot @adangel
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.
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.