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 #1089DescriptionAffects 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 Activityrsoesemann commented on May 10, 2018 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. changed the title [-][APEX] Test asserts in other methods not detected[/-] [+][apex] Test asserts in other methods not detected[/+] on May 10, 2018mkhokhlov commented on Sep 22, 2021
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 . SCWells72 commented on Oct 19, 2021 @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 rsoesemann commented on Oct 19, 2021 @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. mkhokhlov commented on Oct 19, 2021 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 rsoesemann commented on Oct 19, 2021 I agree @mkhokhlov. Would you want to contribute that? SCWells72 commented on Oct 20, 2021 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. SCWells72 commented on Oct 20, 2021 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... SCWells72 commented on Oct 20, 2021 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. rsoesemann commented on Oct 21, 2021 Far out @SCWells72. Great design that improves the situation a lot. @mkhokhlov this also should solve your problem partly, right? Any improvement ideas? changed the title [-][apex] Test asserts in other methods not detected[/-] [+][apex] ApexUnitTestClassShouldHaveAsserts: Test asserts in other methods not detected[/+] on Oct 23, 2021
MetadataMetadataDevelopmentNo branches or pull requestsIssue actions
|