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] 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 #3569

New issue
Copy link
New issue
Copy link
Closed
Closed
[apex] EmptyCatchBlock rule should provide an option to ignore empty catch blocks in test methods#3569
#3581 (+2)
Copy link
Labels
a:false-positivePMD flags a piece of code that is not problematic
Milestone
6.40.0

Description

@SCWells72
SCWells72
opened on Oct 19, 2021
Issue body actions

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

Activity

  1. SCWells72
    added
    a:false-positivePMD flags a piece of code that is not problematic
    on Oct 19, 2021
  2. rsoesemann
    added
    has:pr
    on Oct 21, 2021
  3. rsoesemann
    added this to the 6.40.0 milestone on Oct 21, 2021
  4. rsoesemann
    self-assigned this
    on Oct 21, 2021
  5. adangel
    changed 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, 2021
  6. adangel commented on Oct 22, 2021

    @adangel
    adangel
    on Oct 22, 2021
    Member
    More actions

    In java, the pattern to assert an expected exception is now assertThrows : https://junit.org/junit4/javadoc/latest/org/junit/Assert.html#assertThrows(java.lang.String,%20java.lang.Class,%20org.junit.function.ThrowingRunnable)

    Is there something similar in Apex that we could recommend?

    On the other hand - you would not have an empty catch clause, if you would assert the content of the exception... (e.g. the message).

  7. oowekyala commented on Oct 22, 2021

    @oowekyala
    oowekyala
    on Oct 22, 2021
    Member
    More actions

    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.

  8. SCWells72 commented on Oct 22, 2021

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

  9. rsoesemann
    removed their assignment
    on Oct 22, 2021
  10. SCWells72 commented on Oct 22, 2021

    @SCWells72
    SCWells72
    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
  11. oowekyala commented on Oct 22, 2021

    @oowekyala
    oowekyala
    on Oct 22, 2021
    Member
    More actions

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

  12. SCWells72 commented on Oct 22, 2021

    @SCWells72
    SCWells72
    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
  13. rsoesemann
    closed this as completedin #3580on Oct 23, 2021
  14. rsoesemann
    added 2 commits that reference this issue on Oct 23, 2021

    Merge pull request #3580 from SCWells72/pmd-3569-release-notes

    494520b

    Merge pull request #3574 from SCWells72/pmd-3569

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