Visitar URL original
Preserve method parameters when clearing inline mocks by DragonFSKY · Pull Request #3847 · mockito/mockito · GitHub
Skip to content

Preserve method parameters when clearing inline mocks - #3847

Merged
raphw merged 3 commits into
mockito:mainfrom
DragonFSKY:fix-3763-preserve-method-parameters
Sep 8, 2026
Merged

raphw merged 3 commits into
mockito:mainfrom
DragonFSKY:fix-3763-preserve-method-parameters

Conversation

@DragonFSKY

@DragonFSKY DragonFSKY commented Jul 28, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Preserve explicit MethodParameters metadata when Mockito.clearAllCaches() removes inline instrumentation.

During cache clearing, tracked types were removed before retransformation, so the transformer could not restore parameter metadata on affected JVMs. Keep a short-lived snapshot of the types being cleared, run the existing parameter visitor for them, and skip mock advice so instrumentation is still removed.

Verification

  • The regression fails with arg0 on unchanged main under Temurin 17.0.19 and passes with this change.
  • ./gradlew :mockito-core:check on Temurin 17.0.20: 2,282 passed, 75 conditionally skipped.

Fixes #3763

Track classes undergoing cache cleanup so retransformation restores
method parameter metadata without reapplying mock instrumentation.

Fixes mockito#3763
@DragonFSKY
DragonFSKY force-pushed the fix-3763-preserve-method-parameters branch from 3a4729b to d998af9 Compare August 25, 2026 11:52
@codecov-commenter

codecov-commenter commented Sep 1, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 87.01%. Comparing base (5a676bc) to head (0028c86).

Additional details and impacted files
@@             Coverage Diff              @@
##               main    #3847      +/-   ##
============================================
+ Coverage     86.89%   87.01%   +0.11%     
- Complexity     3062     3070       +8     
============================================
  Files           344      344              
  Lines          9203     9216      +13     
  Branches       1138     1143       +5     
============================================
+ Hits           7997     8019      +22     
+ Misses          918      911       -7     
+ Partials        288      286       -2     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@jselbo

jselbo commented Sep 2, 2026

Copy link
Copy Markdown
Collaborator

Thanks, this seems reasonable. I hope @raphw is able to review because he made a similar fix back in the day: a928c12

@raphw

raphw commented Sep 4, 2026

Copy link
Copy Markdown
Member

I remember that we removed it again because it broke code coverage and other tools: #757

I think the only real fix for this is to update the JVM.

@raphw

raphw commented Sep 4, 2026

Copy link
Copy Markdown
Member

I (Claude) just tried on Java 19 and the bug is indeed fixed on the JDK as we reported it. So ideally you run on a JDK 21 or higher and you should be good.

@raphw

raphw commented Sep 4, 2026

Copy link
Copy Markdown
Member

Actually, it seems like the OpenJDK bug is not working. I replayed this patch with a modern version of Jacoco and it seems to work now. Probably we did not try to register forretransformation and that did not play well.

There is however a bit of stray in the PR.

private volatile Set<Class<?>> clearing = Collections.emptySet();

This will be a problem as transformation can happen in parallel. It is likely rare but if any, this should be a thread local.

Isolate clearing retransformation from callbacks on other threads and remove the thread-local state in finally. Add a regression for concurrent transformation and cleanup.
@raphw

raphw commented Sep 8, 2026

Copy link
Copy Markdown
Member

Looks good with the thread local changes.

@raphw
raphw merged commit e7fd06d into mockito:main Sep 8, 2026
20 checks passed
dongjoon-hyun added a commit to apache/spark-kubernetes-operator that referenced this pull request Sep 29, 2026
### What changes were proposed in this pull request?

This PR aims to upgrade `Mockito` to 5.24.0.

Since `mockito-core` 5.24.0 declares the same dependencies as 5.23.0 (`byte-buddy` 1.17.7, `byte-buddy-agent` 1.17.7, and `objenesis` 3.3), only the `org.mockito:mockito-core` entry of `gradle/verification-metadata.xml` is changed.

### Why are the changes needed?

To bring the latest bug fixes.
- https://github.com/mockito/mockito/releases/tag/v5.24.0 (2026-09-23)
  - Fix data race in `InvocationContainerImpl.invocationForStubbing` (mockito/mockito#3819)
  - Fix non-deterministic `AssertionError` when using `MockMakers.SUBCLASS` (mockito/mockito#3795)
  - Fix `clearInlineMocks` not de-registering singleton mocks (mockito/mockito#3829)
  - Preserve method parameters when clearing inline mocks (mockito/mockito#3847)

### Does this PR introduce _any_ user-facing change?

No. `Mockito` is a test-only dependency.

### How was this patch tested?

Pass the CIs.

### Was this patch authored or co-authored using generative AI tooling?

Generated-by: Claude Opus 5.5

Closes #903 from dongjoon-hyun/SPARK-59838.

Authored-by: Dongjoon Hyun <dongjoon@apache.org>
Signed-off-by: Dongjoon Hyun <dongjoon@apache.org>
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.

The class's MethodParameters are removed after clearAllCaches is invoked

4 participants