You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
How can I leverage Mockito.mockStatic?
It runs the real methods called in any static before it mocks them, since calling the mockStatic method initializes the class.
Do you have any plans to support this?I have some scenarios that require disabling static initialization.For example, static initialization is very time-consuming, and I want to start unit testing as soon as possible.
Hi @raphw , it's me again 🙂. I have been working on this feature since Mockito 5 is now supposed to be loaded as a java agent, it is now possible to suppress the <clinit>. Any chance you could have a look at the idea and implementation? I've detailed the PR description: #3798. Thank you!
The issue I have with this approach is that it depends on class loading
order. I would want this to work no matter how.
I think this would need to work as follows:
1. Mockito is loaded as a Java agent.
2. The Java agent comes with an argument that mentions that static
initializers should be mocked.
3. The agent immediately adds a class file transformer.
4. The class file transformer mocks the static initializers of all classes
that are annotated with some (at)MockitoInitializerSuppression
This will then create stable results, but I do not think this should be
added to the regular API.
Would you be interested in attempting this? I am happy to guide you through
this.
With the current approach, using forkEvery = 1 (Gradle) / reuseForks=false (Maven) gives each test class its own JVM, which eliminates cross-class contamination and leads to stable results:
Test A suppresses Foo -> won't leak to test B (different JVM)
Test B loading Foo normally first -> won't block test A from suppressing it (different JVM)
Tests could also be grouped if build time is a concern. Do you think this approach is still worth considering, or should it be disregarded entirely?
That said, your point still holds if the forkEvery = 1 (Gradle) / reuseForks=false (Maven) should not be considered at all. If I understand correctly, your suggestion would involve configuring suppression via agent arguments so that the transformer is active from the very start of the JVM and for all the tests. This would guarantee that the specified classes always have their static initializers suppressed, regardless of when or how they get loaded. That would still be very useful when dealing with legacy code that brings heavy static initialization that developers never want to run during unit tests.
If you're in favour of this second approach, I would be happy to give it a try with some guidance.
I've worked towards your idea and solution is actually much simpler and it produces stable results - suppressing the clinit for all the tests. I've raised a PR for it (#3801) . Any chance you could have a look at it? 🙏
Activity
[-]How can I avoid 拖[/-][+]How can I suppress static initializer?[/+]raphw commentedon Aug 25, 2021
That's not possible in Mockito.
wonsuc commentedon Feb 24, 2022
Any plan to implement this?
raphw commentedon Feb 24, 2022
No, not really. This would cause that a class would only work in "mock mode". This is not what this feature aims to achieve.
Delfic commentedon Feb 16, 2023
How can I leverage
Mockito.mockStatic?It runs the real methods called in any
staticbefore it mocks them, since calling themockStaticmethod initializes the class.raphw commentedon Feb 16, 2023
It's not possible. To mock a class, we need to load it. Also unmocking would alter the class forever what goes against the overall philosophy.
hitime888 commentedon Mar 19, 2024
Do you have any plans to support this?I have some scenarios that require disabling static initialization.For example, static initialization is very time-consuming, and I want to start unit testing as soon as possible.
raphw commentedon Mar 20, 2024
No, this would affect global state and would be indeterministic as Mockito does not control class loading.
Fixes mockito#2398 : Implement suppress static initialization leverag…
mirkoalicastro commentedon Mar 22, 2026
Hi @raphw , it's me again 🙂. I have been working on this feature since Mockito 5 is now supposed to be loaded as a java agent, it is now possible to suppress the
<clinit>. Any chance you could have a look at the idea and implementation? I've detailed the PR description: #3798. Thank you!raphw commentedon Mar 23, 2026
mirkoalicastro commentedon Mar 24, 2026
Hi @raphw, thank you for your feedback.
With the current approach, using forkEvery = 1 (Gradle) / reuseForks=false (Maven) gives each test class its own JVM, which eliminates cross-class contamination and leads to stable results:
Tests could also be grouped if build time is a concern. Do you think this approach is still worth considering, or should it be disregarded entirely?
That said, your point still holds if the forkEvery = 1 (Gradle) / reuseForks=false (Maven) should not be considered at all. If I understand correctly, your suggestion would involve configuring suppression via agent arguments so that the transformer is active from the very start of the JVM and for all the tests. This would guarantee that the specified classes always have their static initializers suppressed, regardless of when or how they get loaded. That would still be very useful when dealing with legacy code that brings heavy static initialization that developers never want to run during unit tests.
If you're in favour of this second approach, I would be happy to give it a try with some guidance.
Fixes mockito#2398 : Implement global suppress static initialization …
Fixes mockito#2398 : Implement global suppress static initialization …
mirkoalicastro commentedon Mar 30, 2026
Hey @raphw .
I've worked towards your idea and solution is actually much simpler and it produces stable results - suppressing the clinit for all the tests. I've raised a PR for it (#3801) . Any chance you could have a look at it? 🙏
hmadhkhwjly71-cell commentedon Mar 31, 2026
هادي 2###
Fixes #2398 : Implement global suppress static initialization leverag…