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] ApexUnitTestClassShouldHaveAsserts: Test asserts in other methods not detected · Issue #1089 · pmd/pmd

[apex] ApexUnitTestClassShouldHaveAsserts: Test asserts in other methods not detected #1089

New issue
Copy link
New issue
Copy link
Closed
#3571
Closed
[apex] ApexUnitTestClassShouldHaveAsserts: Test asserts in other methods not detected#1089
#3571
Copy link
Labels
a:false-positivePMD flags a piece of code that is not problematic
Milestone
6.40.0

Description

@yippie
yippie
opened on May 10, 2018
Issue body actions

Affects PMD Version: 6.3

Rule: ApexUnitTestClassShouldHaveAsserts

Description: if asserts on not directly inside the test method, the method will be flagged as having no asserts even if the asserts exist in a called method

Code Sample demonstrating the issue:

@isTest
private class MyTestClass {
	
    @isTest
    static void itShouldDoTheThing() { //This gets flagged as having no assert
        //some action
        checkMyWork();
    }

    static void checkMyWork() {
        System.assert(true);
    }
}

Running PMD through: CLI

Activity

  1. rsoesemann commented on May 10, 2018

    @rsoesemann
    rsoesemann
    on May 10, 2018
    Member
    More actions

    PMD and other static analysis tools are not capable to follow the execution flow of methods as they take the source code only. This will never work and you should take the issues from PMD only as a first indication of problems. @SupressWarnings whenever you get such false positives.

  2. jsotuyod
    added
    a:false-positivePMD flags a piece of code that is not problematic
    on May 10, 2018
  3. jsotuyod
    changed the title [-][APEX] Test asserts in other methods not detected[/-] [+][apex] Test asserts in other methods not detected[/+] on May 10, 2018
  4. mkhokhlov commented on Sep 22, 2021

    @mkhokhlov
    mkhokhlov
    on Sep 22, 2021
    More actions

    This will never work

    I may be missing something, but looking at the implementation, is it really impossible to traverse other methods collected in methodCalls?

    That said, I can totally see doing so increasing analysis time and having to come to some compromises (e.g. supporting asserts only in the same class and only one hop from a method annotated @isTest .

  5. SCWells72 commented on Oct 19, 2021

    @SCWells72
    SCWells72
    on Oct 19, 2021
    Contributor
    More actions

    @rsoesemann, is this (still) true?

    PMD and other static analysis tools are not capable to follow the execution flow of methods as they take the source code only.

    In my experience, most static code analyzers do resolve and traverse symbol references to include transitive relationship information in their analysis. Certainly Checkmarx does this, even across language boundaries as it's able to report issues that originate in Apex but are surfaced in presentation tier languages such as Visualforce. This is also how IDE native code inspections work. Does the apex-jorje parser only give you an AST, or does it also include reference resolution information?

    I ask because this one specifically causes noise for folks who take advantage of test assertion usability facades such as sirono-common's Assert class. Basically every test method gets flagged because it uses Assert.* instead of System.assert* even though the former delegates to the latter. It wouldn't surprise me if the emerging Apex mocking class libraries are similarly affected, at least the mock verification side of things.

    Reacted by Robert Sösemann
  6. rsoesemann commented on Oct 19, 2021

    @rsoesemann
    rsoesemann
    on Oct 19, 2021
    Member
    More actions

    @SCWells72 yes this is still true. Not because of Jorje but because of the way PMD works. PMD is a single file processor. There are multiple people trying to solve that for PMD overall (Dataflow PoC will be deprecated with 7.0 see #2124 or only Salesforce. The most recent thread were we discuss this can be found here #2717.

    If you want to help to overcome this please lets meet and/or continue the discussion here.

  7. mkhokhlov commented on Oct 19, 2021

    @mkhokhlov
    mkhokhlov
    on Oct 19, 2021
    More actions

    Being able to traverse methods wrapping calls to System.assert* methods in the same file would still be a big win, I think. I believe the use case is very common:

    @isTest
    public class SomeClassTest {
      @isTest
      static void doThis_expectThat() {
        arrange();
      
        Test.startTest();
        ComplexObject obj = SomeClass.methodUnderTest();
        Test.stopTest();
    
        verifyComplexObject(obj);
      }
    
      private static void verifyComplexObject(ComplexObject obj) {
        System.assert(obj.isGood());
      }
    }
    
    Reacted by Robert Sösemann
  8. rsoesemann commented on Oct 19, 2021

    @rsoesemann
    rsoesemann
    on Oct 19, 2021
    Member
    More actions

    I agree @mkhokhlov. Would you want to contribute that?

  9. SCWells72 commented on Oct 20, 2021

    @SCWells72
    SCWells72
    on Oct 20, 2021
    Contributor
    More actions

    I have a thought on this that would certainly help the use case I've encountered where a usability facade is employed. It can also help with the scenario described by @mkhokhlov, though it's not as natural of a fit. And it doesn't require any ability to determine/navigate symbol references. Effectively this rule would be enhanced with additional patterns (regexp) that define other calls that should be considered valid test assertions for the code base, e.g.,:

       <rule ref="category/apex/bestpractices.xml/ApexUnitTestClassShouldHaveAsserts" message="...">
          <priority>3</priority>
          <properties>
             <property name="additionalAssertPatterns" value="\bAssert\.\w+\(" />
          </properties>
       </rule>

    In this case, additionalAssertPatterns includes a delimited list of regular expressions (need to think about the delimiter so as not to prevent it from being used in a pattern), and the example above would consider a test method that includes any call to Assert._anything_( to provide assertions. When the Java Pattern is compiled, it would be compiled as case-insensitive for Apex's own CI behavior.

    For @mkhokhlov, it would look like:

             <property name="additionalAssertPatterns" value="\bverify\w+\(" />

    It's obviously not nearly as good an option as something that looks for transitive usages of System.assert*, but given that that's likely not going to happen anytime soon, what do you all think? If you're on board, I'll go ahead and put together the change and get a PR going for it.

  10. SCWells72 commented on Oct 20, 2021

    @SCWells72
    SCWells72
    on Oct 20, 2021
    Contributor
    More actions

    For what it's worth, the same approach could be used by the ApexCRUDViolationRule where folks often use facades for pre-query/DML authorization checks that may not fit the fixed patterns expected by that rule. Perhaps others places as well, but those are two that come immediately to mind...

  11. SCWells72 commented on Oct 20, 2021

    @SCWells72
    SCWells72
    on Oct 20, 2021
    Contributor
    More actions

    Okay, I'm implemented this locally and it works quite well. Here's an example configuration that considers all methods of the Assert class and all local methods named verify* to be valid assertion methods:

       <rule ref="category/apex/bestpractices.xml/ApexUnitTestClassShouldHaveAsserts" message="...">
          <priority>3</priority>
          <properties>
             <property name="additionalAssertMethodPattern" value="(Assert\.\w+|verify\w+)" />
          </properties>
       </rule>

    I also added support for fully-qualified standard assert methods, e.g., System.System.assert* since those weren't properly covered. I'll get a pull request going shortly.

  12. rsoesemann
    added
    has:pr
    on Oct 21, 2021
  13. rsoesemann commented on Oct 21, 2021

    @rsoesemann
    rsoesemann
    on Oct 21, 2021
    Member
    More actions

    Far out @SCWells72. Great design that improves the situation a lot. @mkhokhlov this also should solve your problem partly, right? Any improvement ideas?

  14. adangel
    added this to the 6.40.0 milestone on Oct 22, 2021
  15. rsoesemann
    closed this as completedin #3571on Oct 22, 2021
  16. adangel
    changed the title [-][apex] Test asserts in other methods not detected[/-] [+][apex] ApexUnitTestClassShouldHaveAsserts: Test asserts in other methods not detected[/+] on Oct 23, 2021
  17. adangel
    added a commit that references this issue on Oct 23, 2021

    [doc] Update release notes (#1089, #3571)

    2cb76f8
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    a:false-positivePMD flags a piece of code that is not problematic

    Type

    No type

    Projects

    No projects

      Milestone

      • 6.40.0

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      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.