Skip to content

Fix non-deterministic AssertionError when using MockMakers.SUBCLASS (#3406, #2823) - #3795

Merged
raphw merged 1 commit into
mockito:mainfrom
quijote:main
Mar 17, 2026
Merged

raphw merged 1 commit into
mockito:mainfrom
quijote:main

Conversation

@quijote

@quijote quijote commented Mar 17, 2026

Copy link
Copy Markdown
Contributor

MockUtil::mockMakers is a ConcurrentHashMap with random iteration order based on identify hash-code of Class keys.

    private static final Map<Class<? extends MockMaker>, MockMaker> mockMakers =
            new ConcurrentHashMap<>(
                    Collections.singletonMap(defaultMockMaker.getClass(), defaultMockMaker));

Depending on the order the following code in MockUtil::getMockHandlerOrNull sometimes throws AssertionError after MockMakers.SUBCLASS was used:

for (MockMaker mockMaker : mockMakers.values()) {
    MockHandler<?> handler = mockMaker.getHandler(mock);
    if (handler != null) {
        assert getMockMaker(handler.getMockSettings().getMockMaker()) == mockMaker;
        return handler;
    }
}

Specifically the following order causes issues:

image

The problem was previously reported in #3406 and #2823.

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

@raphw

raphw commented Mar 17, 2026

Copy link
Copy Markdown
Member

Good catch, thanks.

@raphw
raphw merged commit 82998ff into mockito:main Mar 17, 2026
@quijote

quijote commented Sep 17, 2026

Copy link
Copy Markdown
Contributor Author

@raphw @jselbo

The bug that was fixed with this PR unfortunately currently blocks update from Mockito 4 to Mockito 5 in our project. The last Mockito version 5.23.0 was released a few days before the fix was merged in March. Are you aware of any plans to release a new version which includes more recent fixes?

@jselbo

jselbo commented Sep 17, 2026

Copy link
Copy Markdown
Collaborator

Thanks for the reminder - we should cut a new release. I'll do that shortly.

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