Skip to content

[java] Fix #2840: CloseResource: allow Mockito mocks by default - #6954

Merged
UncleOwen merged 2 commits into
pmd:mainfrom
Eljees:fix/2840-closeresource-mockito-defaults
Aug 8, 2026
Merged

UncleOwen merged 2 commits into
pmd:mainfrom
Eljees:fix/2840-closeresource-mockito-defaults

Conversation

@Eljees

@Eljees Eljees commented Aug 6, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #2840.

@UncleOwen — both, as you suggested.

Defaults. org.mockito.Mockito#mock(_*) is added to allowedResourceMethodPatterns. spy and mockStatic are deliberately left out: a spy calls through to the real object by default, so either the spy or the object it wraps still has to be closed, and mockStatic returns a MockedStatic, which genuinely has to be closed. A violation in either case is a true positive, and there is a test pinning mockStatic.

The defaults live in CloseResourceRule.java rather than in errorprone.xml, so that is where the pattern went; the note for other frameworks went into the rule description in errorprone.xml, with EasyMock, JMock and JMockit as examples and an explicit note about spy and mockStatic.

mockito-core is already a test dependency of pmd-java, so no new dependency is needed — unlike the servlet patterns in #6436.

Runs, on 443f7a6 with temurin-25, for the original three-pattern version of this PR:

clean tree                  Tests run: 99,  Failures: 0    BUILD SUCCESS
+ test cases only           Tests run: 102, Failures: 1    "#2840 ... Expected 0 problem(s), 1 problem(s) found."
+ test cases and the fix    Tests run: 102, Failures: 0    BUILD SUCCESS

Update after review (6b0bfc7). @zbynek pointed out that a spy delegates to the real object, so allowing spy(_*) would turn a true positive into a false negative. Agreed — spy is out of the defaults, and only mock remains.

The spy test case is removed rather than inverted. With spy out of the defaults the rule reports 0 violations for that snippet either way: it took the resource as a method parameter, so the case never discriminated on the allowed-pattern list and was not testing what its description claimed. The mock and mockStatic cases do discriminate and stay. The release note entry moved from java to java-errorprone (@UncleOwen). I have not re-run the suite locally since that commit — CI on this branch is the check for it.

AI-assisted (LLM used for drafting); the runs above are mine.

@Eljees
Eljees force-pushed the fix/2840-closeresource-mockito-defaults branch from ade1b5f to e1c0e70 Compare August 6, 2026 20:08
@Eljees
Eljees force-pushed the fix/2840-closeresource-mockito-defaults branch from e1c0e70 to 312bc93 Compare August 7, 2026 10:20
@Eljees

Eljees commented Aug 7, 2026

Copy link
Copy Markdown
Contributor Author

Rebased on current main after #6918 landed (both change CloseResource, so the branch had gone conflicting). Re-ran on ffc2425: clean tree 101 tests green, with this change 104/104 green - the three added cases sit on top of the post-#6918 suite. No changes to the substance of the PR.

@pmd-actions-helper

pmd-actions-helper Bot commented Aug 7, 2026 •

Copy link
Copy Markdown
Contributor

Documentation Preview

Compared to main:
This changeset changes 0 violations,
introduces 0 new violations, 0 new errors and 0 new configuration errors,
removes 229 violations, 16 errors and 9 configuration errors.
There are 0 changed duplications, 0 new duplications and 0 removed duplications.
There are 0 changed CPD errors, 0 new CPD errors and 0 removed CPD errors.

Regression Tester Report

(comment created at 2026-08-08 07:25:37+00:00 for 6b0bfc7)

@zbynek

zbynek commented Aug 7, 2026

Copy link
Copy Markdown
Contributor

I think it would make sense to only add mock and not spy to the defaults. For spy the default behavior is to call real methods of the underlying object, so you should still close either the spy object or the underlying one.

This change may also create false negatives for mocks that use callRealMethod, but that sounds like a reasonable trade-off.

Comment thread docs/pages/release_notes.md Outdated
* [#4953](https://github.com/pmd/pmd/issues/4953): \[core] Deprecate PMDConfiguration#setClassloader and #getClassloader
* [#6865](https://github.com/pmd/pmd/issues/6865): \[core] Include the running PMD version in the "Unable to find referenced rule" error
* java
* [#2840](https://github.com/pmd/pmd/issues/2840): \[java] CloseResource: False positive on mocks

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please move this to the correct category (java-errorprone)

@zbynek: a spy delegates to the real object by default, so the object it
wraps still has to be closed. Allowing Mockito#spy would have turned a true
positive into a false negative, so it is out of the defaults; only
Mockito#mock stays. The rule doc now says why spy and mockStatic are
deliberately absent.

The spy test case is removed rather than inverted. With spy out of the
defaults the rule reports 0 violations for it either way - the snippet took
the resource as a method parameter, so the case never discriminated on the
allowed-pattern list and was not testing what its description claimed. The
mock and mockStatic cases do discriminate and stay.

@UncleOwen: release note entry moved from java to java-errorprone.
@Eljees Eljees changed the title [java] CloseResource: allow Mockito mocks and spies by default [java] CloseResource: allow Mockito mocks by default Aug 8, 2026
@Eljees

Eljees commented Aug 8, 2026

Copy link
Copy Markdown
Contributor Author

@zbynek — you're right, and I've dropped spy. A spy delegates to the real object by default, so if the thing being spied on is a resource, it still has to be closed; whitelisting spy(_*) would hide exactly the leak this rule exists to catch. The defaults are mock(_*) only now, and the rule description says why spy and mockStatic are both deliberately absent.

One thing I'd rather state plainly than let the diff imply: I removed the spy test case instead of inverting it. With spy out of the defaults the rule reports 0 violations for that snippet either way — it took the resource as a method parameter, so the case never discriminated on the allowed-pattern list and wasn't testing what its description claimed. The mock and mockStatic cases do discriminate, so they stay.

On callRealMethod: agreed it's a false-negative surface, and agreed it's an acceptable trade-off — a mock configured to call real methods is indistinguishable from a plain mock at the call site the rule sees.

@UncleOwen — release note entry moved to java-errorprone.

Title and description updated to match: this is mock, not "mocks and spies".

@UncleOwen UncleOwen added this to the 7.27.0 milestone Aug 8, 2026

@UncleOwen UncleOwen left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@UncleOwen
UncleOwen merged commit 6ff6e72 into pmd:main Aug 8, 2026
13 checks passed
@adangel adangel changed the title [java] CloseResource: allow Mockito mocks by default [java] Fix #2840: CloseResource: allow Mockito mocks by default Aug 14, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[java] CloseResource: False positive on mocks

3 participants