Skip to content

Fix clearInlineMocks not de-registering singleton mocks - #3829

Merged
jselbo merged 2 commits into
mockito:mainfrom
mirkoalicastro:fix/clear-inline-mocks-singleton
May 29, 2026
Merged

jselbo merged 2 commits into
mockito:mainfrom
mirkoalicastro:fix/clear-inline-mocks-singleton

Conversation

@mirkoalicastro

@mirkoalicastro mirkoalicastro commented May 29, 2026 •

Copy link
Copy Markdown
Contributor

Description

Problem: singleton mocks were leaking since clearInlineMocks() didn't clear their mocking state.

clearInlineMocks() and clearInlineMock(...) left singleton mocks registered, so re-mocking the same instance threw "already registered as a mock" exception.

This PR fixes it:

  • clearInlineMocks() clears the mockedSingletons registry
  • clearInlineMock(...) removes the instance from that registry: discussed and agreed to not cover.

Without the changes, both the newly-introduce unit tests would fail.

Checklist

  • Read the contributing guide
  • PR should be motivated, i.e. what does it fix, why, and if relevant how
  • If possible / relevant include an example in the description, that could help all readers
    including project members to get a better picture of the change
  • Avoid other runtime dependencies
  • Meaningful commit history ; intention is important please rebase your commit history so that each
    commit is meaningful and help the people that will explore a change in 2 years
  • The pull request follows coding style (run ./gradlew spotlessApply for auto-formatting)
  • Mention Fixes #<issue number> in the description if relevant
  • At least one commit should end with Fixes #<issue number> if relevant

@codecov-commenter

codecov-commenter commented May 29, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 86.84%. Comparing base (af081a3) to head (65186d4).

Additional details and impacted files
@@            Coverage Diff            @@
##               main    #3829   +/-   ##
=========================================
  Coverage     86.84%   86.84%           
  Complexity     3042     3042           
=========================================
  Files           343      343           
  Lines          9174     9175    +1     
  Branches       1136     1136           
=========================================
+ Hits           7967     7968    +1     
  Misses          917      917           
  Partials        290      290           

☔ View full report in Codecov by Sentry.
📢 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 May 29, 2026

Copy link
Copy Markdown
Collaborator

Also I am curious, are you using the new mockSingleton API? Or did AI find this bug?

@mirkoalicastro

mirkoalicastro commented May 29, 2026 •

Copy link
Copy Markdown
Contributor Author

Also I am curious, are you using the new mockSingleton API? Or did AI find this bug?

Actually, I was curious about your last commit (af081a3), so I started looking into it and found the discrepancy in the clearMock. Then AI highlighted the other discrepancy 😅

@mirkoalicastro
mirkoalicastro requested a review from jselbo May 29, 2026 16:20
@jselbo
jselbo merged commit 70a97b0 into mockito:main May 29, 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.

3 participants