Reuse MockedStatic for consecutive stubbings of the same class#1005
Open
timtebeek wants to merge 2 commits into
Open
Reuse MockedStatic for consecutive stubbings of the same class#1005timtebeek wants to merge 2 commits into
MockedStatic for consecutive stubbings of the same class#1005timtebeek wants to merge 2 commits into
Conversation
Fixes #1004: `MockitoWhenOnStaticToMockStatic` created nested `try (MockedStatic<X>)` blocks for sibling `Mockito.when()` calls on the same static class, which Mockito 5 rejects at runtime with "static mocking is already registered in the current thread". Subsequent same-class stubbings (siblings or nested in inner blocks) now share the enclosing `MockedStatic`.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
MockitoWhenOnStaticToMockStaticpreviously created nestedtry (MockedStatic<X>)blocks for siblingMockito.when()calls on the same static class. Mockito 5 rejects this at runtime withstatic mocking is already registered in the current thread.if/loop/etc. inner blocks) now share the enclosingMockedStatic.What changed
pendingResourcesmap (FQN → variable name) through the recursivemaybeWrapStatementsInTryWithResourcesMockedStaticso the secondwhen(X.foo())in the same block reuses the just-createdmockX1instead of opening a nested try.generatedMocks(variable name → FQN) map as a fallback ingetMatchingFilteredResourcesfor nested blocks. Needed becauseJavaTemplate-generatedtry(MockedStatic<X> ...)resources end up withMockedStatic<{undefined}>as their declared type, breaking pure type-based lookup.reuseMockedStaticto use the cursor's actual block forJavaTemplate.applycoordinates (the recursive caller was passing a synthetic block not in the cursor tree, causing the wrong statement to be returned) and to preserve the original statement's prefix (blank lines).Test plan
shouldShareSingleMockedStaticForConsecutiveStubbingsOfSameClasscovers the exact case from MockitoWhenOnStaticToMockStatic creates nested MockedStatic blocks for the same class #1004.shouldHandleMultipleStaticMocksAndNestedStatements— its previous expected output codified the broken nested-try behavior; now expects a singlemockA1shared across siblings and inside theif-block.MockitoWhenOnStaticToMockStaticTestpass.