Visitar URL original
Fixes #3814 : implement package-level clinit suppression by mirkoalicastro · Pull Request #3815 · mockito/mockito · GitHub
Skip to content

Fixes #3814 : implement package-level clinit suppression - #3815

Merged
raphw merged 4 commits into
mockito:mainfrom
mirkoalicastro:feature/suppress-static-initialization-packages
May 13, 2026
Merged

raphw merged 4 commits into
mockito:mainfrom
mirkoalicastro:feature/suppress-static-initialization-packages

Conversation

@mirkoalicastro

@mirkoalicastro mirkoalicastro commented Apr 22, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #3814 .

With this PR, the mockito.suppress.clinit property can accept both an exact class name (e.g., com.example.MyClass) or a package wildcard ending in .* (e.g., com.example.*) that matches the package and all sub-packages.

In addition to unit tests, I've also installed locally and created a simple project using both the features.

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 Apr 22, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 77.77778% with 6 lines in your changes missing coverage. Please review.
✅ Project coverage is 86.84%. Comparing base (c9fe6a0) to head (cbe3a09).

Files with missing lines Patch % Lines
.../main/java/org/mockito/internal/PremainAttach.java 75.00% 6 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff              @@
##               main    #3815      +/-   ##
============================================
- Coverage     86.84%   86.84%   -0.01%     
- Complexity     3032     3039       +7     
============================================
  Files           343      343              
  Lines          9152     9167      +15     
  Branches       1131     1135       +4     
============================================
+ Hits           7948     7961      +13     
- Misses          915      917       +2     
  Partials        289      289              

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

@mirkoalicastro
mirkoalicastro force-pushed the feature/suppress-static-initialization-packages branch from 3ed37f8 to 849076f Compare April 22, 2026 10:47
@mirkoalicastro

Copy link
Copy Markdown
Contributor Author

Hi @raphw . Any chance you could look at this? 🙏

@raphw

raphw commented May 5, 2026

Copy link
Copy Markdown
Member

I have been thinking, but I am not sure if this is a good idea. New classes might do useful things in their initializers, like settings fields, and this could lead to unexpected breakage. I think this should list class names for this reason, not packages. As for the principle of least surpise. @mockito/developers What do you think?

@jselbo

jselbo commented May 8, 2026

Copy link
Copy Markdown
Collaborator

I dunno, it seems harmless to me. This property is already kind of a power-user scenario so they probably know what they are doing.

@bric3

bric3 commented May 11, 2026 •

Copy link
Copy Markdown
Member

As for the principle of least surpise. @mockito/developers What do you think?

I dunno, it seems harmless to me. This property is already kind of a power-user scenario so they probably know what they are doing.

I think #3801 is already somewhat already in that area. I think having a wildcard makes sense, and barely change that feature. Also, like Joshua mentioned, it's kind of a niche feature.

However, I do not think the whole feature is not harmless either ; while the mechnism is explicit in the build descriptor via a system property, it's not guaranteed to be visible enough. So I do think Mockito should log something, there's no println in the original PR. That feature is sufficiently hidden that it should print something at least for late contributors to such a code base.

Given it happens in the premain, I'm not sure we should use MockitoLogger (or other logger frameworks, this could cause too early class initialization), so when adding the log only rely on println.

@mirkoalicastro

Copy link
Copy Markdown
Contributor Author

Thanks everyone for taking the time to review.

@raphw I think that those are fair concerns. However, I personally think that the risk is very low since users need to explicitly set the property with a wildcard pattern, so it's unlikely someone would accidentally incur into it. Also, in legacy codebases, listing all the individual classes that need suppression can become fragile as new classes are added, in addition to being verbose.

In general, I find it a great suggestion about adding a log line when a class initializer is suppressed to improve visibility.

@raphw

raphw commented May 11, 2026

Copy link
Copy Markdown
Member

For my part, we can merge this than. There is no best solution, and I expect that people mostly mock packages they own. Libraries avoid extensive logic in static initializers for a reason. If there is no other comment, I will merge this within this week.

@bric3 bric3 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I pre-approve, but please add the println statement.

Comment thread mockito-core/src/main/java/org/mockito/internal/PremainAttach.java
Comment thread mockito-core/src/test/java/org/mockito/internal/PremainAttachTest.java Outdated
Comment thread mockito-core/src/test/java/org/mockito/internal/PremainAttachTest.java Outdated
Comment thread mockito-core/src/main/java/org/mockito/internal/PremainAttach.java
@mirkoalicastro
mirkoalicastro force-pushed the feature/suppress-static-initialization-packages branch from b313bc9 to 92ffb4a Compare May 12, 2026 16:52
@raphw
raphw merged commit 414a999 into mockito:main May 13, 2026
20 checks passed
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.

Package-level clinit suppression

5 participants