Skip to content

How can I suppress static initializer? #2398

Closed

Description

@Boreaso

Hey guys, I want to avoid to execute static initializer of the mocked class. Is it possible in mockito now?

Activity

  1. changed the title [-]How can I avoid 拖[/-] [+]How can I suppress static initializer?[/+] on Aug 23, 2021
  2. raphw commented on Aug 25, 2021

    @raphw
    Member

    That's not possible in Mockito.

  3. wonsuc commented on Feb 24, 2022

    @wonsuc

    Any plan to implement this?

  4. raphw commented on Feb 24, 2022

    @raphw
    Member

    No, not really. This would cause that a class would only work in "mock mode". This is not what this feature aims to achieve.

  5. Delfic commented on Feb 16, 2023

    @Delfic

    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.

  6. raphw commented on Feb 16, 2023

    @raphw
    Member

    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.

  7. hitime888 commented on Mar 19, 2024

    @hitime888

    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.

  8. raphw commented on Mar 20, 2024

    @raphw
    Member

    No, this would affect global state and would be indeterministic as Mockito does not control class loading.

  9. added a commit that references this issue on Mar 22, 2026
    279ba48
  10. mirkoalicastro commented on Mar 22, 2026

    @mirkoalicastro
    Contributor

    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!

  11. raphw commented on Mar 23, 2026

    @raphw
    Member
  12. mirkoalicastro commented on Mar 24, 2026

    @mirkoalicastro
    Contributor

    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:

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

  13. added 2 commits that reference this issue on Mar 30, 2026
    6c563a1
    cc71499
  14. mirkoalicastro commented on Mar 30, 2026

    @mirkoalicastro
    Contributor

    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? 🙏

  15. hmadhkhwjly71-cell commented on Mar 31, 2026

    @hmadhkhwjly71-cell

    هادي 2###

  16. added a commit that references this issue on Apr 8, 2026
    9929fcf
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions