Repository navigation
[java] Fix #2840: CloseResource: allow Mockito mocks by default - #6954
Conversation
ade1b5f to
e1c0e70
Compare
e1c0e70 to
312bc93
Compare
|
Compared to main: (comment created at 2026-08-08 07:25:37+00:00 for 6b0bfc7) |
|
I think it would make sense to only add This change may also create false negatives for mocks that use callRealMethod, but that sounds like a reasonable trade-off. |
| * [#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 |
There was a problem hiding this comment.
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.
|
@zbynek — you're right, and I've dropped One thing I'd rather state plainly than let the diff imply: I removed the spy test case instead of inverting it. With On @UncleOwen — release note entry moved to java-errorprone. Title and description updated to match: this is |
Fixes #2840.
@UncleOwen — both, as you suggested.
Defaults.
org.mockito.Mockito#mock(_*)is added toallowedResourceMethodPatterns.spyandmockStaticare 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, andmockStaticreturns aMockedStatic, which genuinely has to be closed. A violation in either case is a true positive, and there is a test pinningmockStatic.The defaults live in
CloseResourceRule.javarather than inerrorprone.xml, so that is where the pattern went; the note for other frameworks went into the rule description inerrorprone.xml, withEasyMock,JMockandJMockitas examples and an explicit note aboutspyandmockStatic.mockito-coreis already a test dependency ofpmd-java, so no new dependency is needed — unlike the servlet patterns in #6436.Runs, on
443f7a6with temurin-25, for the original three-pattern version of this PR:Update after review (
6b0bfc7). @zbynek pointed out that a spy delegates to the real object, so allowingspy(_*)would turn a true positive into a false negative. Agreed —spyis out of the defaults, and onlymockremains.The
spytest case is removed rather than inverted. Withspyout 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. ThemockandmockStaticcases 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.