GeezerVison is showing github.com — [apex] EmptyCatchBlock rule should provide an option to ignore empty catch blocks in test methods · Issue #3569 · pmd/pmd
[apex] EmptyCatchBlock rule should provide an option to ignore empty catch blocks in test methods #3569Description
Affects PMD Version: 6.39.0
Rule: EmptyCatchBlock
Please provide the rule name and a link to the rule documentation:
https://pmd.github.io/pmd-6.39.0/pmd_rules_apex_errorprone.html#emptycatchblock
Description:
There's at least one situation where empty catch blocks are actually expected and even support a best practices approach to the problem being solved, specifically in Apex unit tests when verifying that an expected exception has been thrown.
An options should be added to the EmptyCatchBlock rule to allow instance of this pattern to be ignored.
Code Sample demonstrating the issue:
@IsTest
static void testAddAllNotNull() {
try {
// This should throw an IllegalStateException if provided a null list
CollectionUtil.addAllNotNull((List<Object>) null, null);
System.assert(false, 'Expected an IllegalArgumentException for a null list.');
} catch (IllegalArgumentException e) {
// Expected, but this gets flagged by the PMD Apex EmptyCatchBlock rule
}
}
Expected outcome:
I imagine for backward-compatibility, a new option would be added that defaults to the current behavior if unspecified, e.g.:
<rule ref="category/apex/documentation.xml/EmptyCatchBlock" message="...">
<priority>3</priority>
<properties>
<property name="reportInTestMethods" value="false" />
</properties>
</rule>
Running PMD through: [CLI | Ant | Maven | Gradle | Designer | Other]
CLI and direct API-based integration Activitychanged the title [-][Apex] EmptyCatchBlock rule should provide an option to ignore empty catch blocks in test methods[/-] [+][apex] EmptyCatchBlock rule should provide an option to ignore empty catch blocks in test methods[/+] on Oct 22, 2021adangel commented on Oct 22, 2021 oowekyala commented on Oct 22, 2021 I don't remember in which branch this is implemented, but in the java EmptyCatchBlock, the violation is ignored if its name starts with ignored. I think a nicer fix than adding a property would be to whitelist names starting with ignored and expected.
@IsTest
static void testAddAllNotNull() {
try {
// This should throw an IllegalStateException if provided a null list
CollectionUtil.addAllNotNull((List<Object>) null, null);
System.assert(false, 'Expected an IllegalArgumentException for a null list.');
} catch (IllegalArgumentException expected) {
}
}
I think it's a better solution because the code is then self-documenting. SCWells72 commented on Oct 22, 2021 ContributorAuthor More actions @adangel Unfortunately the Apex assertions are lean, with only System.assert, System.assertEquals, and System.assertNotEquals. I don't expect Salesforce to add any new assertion methods, at least not anytime soon. That why we created our own more expressive Assert facade. And the false positives produced by usages of that facade is exactly what motivated the work in #1089 that I've submitted for review. Oh, and Apex doesn't currently have a lamba/closure/anonymous inner class concept, so in order to pass something that throws the expected exception, you'd have to implement an interface as a distinct inner class. Probably should have led with that limitation of Apex.
@oowekyala that's an interesting idea. I do like the self-contained expressiveness. It seems like both options could be useful, particularly depending on the balance of existing vs. new code. SCWells72 commented on Oct 22, 2021 ContributorAuthor More actions Okay, I found the corresponding configuration properties in the Java rule:
<property name="allowCommentedBlocks" type="Boolean" description="Empty blocks containing comments will be skipped" value="false"/>
<property name="allowExceptionNameRegex" type="String" description="Empty blocks catching exceptions with names matching this regular expression will be skipped" value="^(ignored|expected)$"/>
I can certainly rework my changes to use those exact properties instead of the one I've added. That would obviously bring greater consistency across languages and more flexibility. Reacted by Andreas Dangel and Robert Sösemann oowekyala commented on Oct 22, 2021
I can certainly rework my changes to use those exact properties instead of the one I've added. That would obviously bring greater consistency across languages and more flexibility.
That would be great, thanks :) SCWells72 commented on Oct 22, 2021 ContributorAuthor More actions New commit added to the PR to adopt the same config properties as the analogous Java rule. Reacted by Robert Sösemann
MetadataMetadataDevelopmentNo branches or pull requestsIssue actions
|