Skip to content

Feat: automatically detect class to mock in mockStatic and mockConstruction#3731

Merged
TimvdLippe merged 1 commit intomockito:mainfrom
giulong:feature/mock-static-reified
Sep 26, 2025
Merged

Feat: automatically detect class to mock in mockStatic and mockConstruction#3731
TimvdLippe merged 1 commit intomockito:mainfrom
giulong:feature/mock-static-reified

Conversation

@giulong
Copy link
Copy Markdown
Contributor

@giulong giulong commented Sep 21, 2025

As PR #2779 introduced the ability to automatically detect the class to mock for mocks and spies, this commit allows doing the same with mockStatic and mockConstruction.

Added overloaded reified methods to match all the counterparts that consume an explicit Class literal parameter.

For instance, users can now leverage these:

MockedStatic<SomeClass> mock = mockStatic();
MockedConstruction<SomeClass> mock = mockConstruction();

instead of:

MockedStatic<SomeClass> mock = mockStatic(SomeClass.class);
MockedConstruction<SomeClass> mock = mockConstruction(SomeClass.class);

The following methods have been added:

  • mockStatic(T...)
  • mockStatic(Answer, T...)
  • mockStatic(String, T...)
  • mockStatic(MockSettings, T...)
  • mockConstruction(T...)
  • mockConstruction(MockInitializer, T...)
  • mockConstruction(MockSettings, T...)
  • mockConstruction(Function<Context, MockSettings>, T...)
  • mockConstruction(MockSettings, MockInitializer, T...)
  • mockConstruction(Function<Context, MockSettings>, MockInitializer, T...)

Unit tests to cover all the new lines/branches have been added.

The Javadoc of each newly added method is copied from its counterpart that consume an explicit Class literal parameter,
of course documenting the reified parameter instead of the class literal one.
As part of the Javadoc, @since 5.21.0 have been added since 5.20.0 is the current version.

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
Copy link
Copy Markdown

codecov-commenter commented Sep 21, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 86.44%. Comparing base (1d4b473) to head (0268b32).
⚠️ Report is 26 commits behind head on main.

Additional details and impacted files
@@             Coverage Diff              @@
##               main    #3731      +/-   ##
============================================
+ Coverage     86.39%   86.44%   +0.05%     
- Complexity     2970     2984      +14     
============================================
  Files           341      341              
  Lines          9016     9030      +14     
  Branches       1111     1111              
============================================
+ Hits           7789     7806      +17     
+ Misses          946      943       -3     
  Partials        281      281              

☔ 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.

@giulong giulong force-pushed the feature/mock-static-reified branch 8 times, most recently from 65ee9e9 to de99f73 Compare September 21, 2025 20:17

@Test
public void ensure_reified_mocked_static_can_be_called_without_parameters() {
Assume.assumeThat(System.getenv("MOCK_MAKER"), not(equalTo("mock-maker-subclass")));
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added this assumption in every test to skip them in ci jobs that use SubclassByteBuddyMockMaker, which does not support the creation of static/construction mocks

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use the same assumption as previously in this test suite, but then remove the not:

Assume.assumeThat(Plugins.getMockMaker(), not(instanceOf(InlineMockMaker.class)));
We also only need 1 assumption for this.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We also only need 1 assumption for this.

Yep that double assumption was mistakenly left since the first thing I made was:

Assume.assumeThat(Plugins.getMockMaker(), not(instanceOf(SubclassByteBuddyMockMaker.class)));

This was to follow the same logic already in place:

Assume.assumeThat(Plugins.getMockMaker(), not(instanceOf(InlineMockMaker.class)));

but somehow that didn't work in ci actions that failed regardless of that assumption:

The used MockMaker SubclassByteBuddyMockMaker does not support the creation of static mocks

So I made that tricky inspection on env vars, now replaced as suggested (much better).

Copy link
Copy Markdown
Contributor

@TimvdLippe TimvdLippe left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, thanks for taking care of that! We do need to update the assumptions to be consistent with the other assumptions


@Test
public void ensure_reified_mocked_static_can_be_called_without_parameters() {
Assume.assumeThat(System.getenv("MOCK_MAKER"), not(equalTo("mock-maker-subclass")));
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use the same assumption as previously in this test suite, but then remove the not:

Assume.assumeThat(Plugins.getMockMaker(), not(instanceOf(InlineMockMaker.class)));
We also only need 1 assumption for this.

@giulong giulong force-pushed the feature/mock-static-reified branch 2 times, most recently from 353768d to b44052a Compare September 25, 2025 20:03
…uction

As PR mockito#2779 introduced the ability to automatically detect the class to mock
for mocks and spies, this commit allows doing the same with mockStatic
and mockConstruction.

Added overloaded reified methods to match all the counterparts that consume
an explicit Class literal parameter.

For instance, users can now leverage these:
* MockedStatic<SomeClass> mock = mockStatic();
* MockedConstruction<SomeClass> mock = mockConstruction();

instead of:
* MockedStatic<SomeClass> mock = mockStatic(SomeClass.class);
* MockedConstruction<SomeClass> mock = mockConstruction(SomeClass.class);

The following methods have been added:
* mockStatic(T...)
* mockStatic(Answer, T...)
* mockStatic(String, T...)
* mockStatic(MockSettings, T...)
* mockConstruction(T...)
* mockConstruction(MockInitializer, T...)
* mockConstruction(MockSettings, T...)
* mockConstruction(Function<Context, MockSettings>, T...)
* mockConstruction(MockSettings, MockInitializer, T...)
* mockConstruction(Function<Context, MockSettings>, MockInitializer, T...)
@giulong giulong force-pushed the feature/mock-static-reified branch from b44052a to 0268b32 Compare September 25, 2025 20:17
@giulong giulong requested a review from TimvdLippe September 25, 2025 20:47
Copy link
Copy Markdown
Contributor

@TimvdLippe TimvdLippe left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@TimvdLippe TimvdLippe merged commit e6682a3 into mockito:main Sep 26, 2025
19 checks passed
dongjoon-hyun added a commit to apache/spark-kubernetes-operator that referenced this pull request Dec 10, 2025
### What changes were proposed in this pull request?

This PR aims to upgrade `mockito` to 5.21.0 for Apache Spark K8s Operator `v0.7`.

### Why are the changes needed?

To bring the latest features and bug fixes.
- https://github.com/mockito/mockito/releases/tag/v5.21.0
  - mockito/mockito#3758
  - mockito/mockito#3752
  - mockito/mockito#3753
  - mockito/mockito#3759
  - mockito/mockito#3731

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

No behavior change.

### How was this patch tested?

Pass the CIs.

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

No.

Closes #425 from dongjoon-hyun/SPARK-54672.

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