Mocking final/static classes/methods is possible with Mockito v2 only.

add this in your gradle file:

testImplementation 'org.mockito:mockito-inline:2.13.0'

This is not possible with Mockito v1, from the Mockito FAQ:

What are the limitations of Mockito

  • Needs java 1.5+

  • Cannot mock final classes

...

Answer from user180100 on Stack Overflow
🌐
Davidvlijmincx
davidvlijmincx.com › posts › writing_higher_quality_tests_with_mockitos_inline_mock_maker
How to use the Mockito's inline mock maker
July 30, 2024 - With the inline mock maker, it's possible to mock static methods. The example shows how to mock the static method Instant.now(). Every call made to it will return a mock of Instant.
🌐
Baeldung
baeldung.com › home › testing › difference between mockito core and mockito inline
Difference Between Mockito Core and Mockito Inline | Baeldung
September 17, 2024 - Mockito Inline is an extension of Mockito Core including additional capabilities for mocking final classes, final fields, static methods, and constructors. This library is useful when you need to mock or stub these types of methods or classes.
Top answer
1 of 16
312

Mocking final/static classes/methods is possible with Mockito v2 only.

add this in your gradle file:

testImplementation 'org.mockito:mockito-inline:2.13.0'

This is not possible with Mockito v1, from the Mockito FAQ:

What are the limitations of Mockito

  • Needs java 1.5+

  • Cannot mock final classes

...

2 of 16
302

Mockito 2 now supports final classes and methods!

But for now that's an "incubating" feature. It requires some steps to activate it which are described in What's New in Mockito 2:

Mocking of final classes and methods is an incubating, opt-in feature. It uses a combination of Java agent instrumentation and subclassing in order to enable mockability of these types. As this works differently to our current mechanism and this one has different limitations and as we want to gather experience and user feedback, this feature had to be explicitly activated to be available ; it can be done via the mockito extension mechanism by creating the file src/test/resources/mockito-extensions/org.mockito.plugins.MockMaker containing a single line:

mock-maker-inline

After you created this file, Mockito will automatically use this new engine and one can do :

 final class FinalClass {
   final String finalMethod() { return "something"; }
 }

 FinalClass concrete = new FinalClass(); 

 FinalClass mock = mock(FinalClass.class);
 given(mock.finalMethod()).willReturn("not anymore");

 assertThat(mock.finalMethod()).isNotEqualTo(concrete.finalMethod());

In subsequent milestones, the team will bring a programmatic way of using this feature. We will identify and provide support for all unmockable scenarios. Stay tuned and please let us know what you think of this feature!

🌐
Die Cut Templates
diecuttemplates.com
Custom Dieline Generator - Download Professional Packaging Templates
Production-ready packaging dielines to help manufacturers save time and reduce waste. 35,000+ customizable templates in SVG/DXF/PDF with Nesting, Manufacturing Cost Calculator, Dielines API and 3D Mockups,
🌐
DZone
dzone.com › testing, deployment, and maintenance › testing, tools, and frameworks › demystifying static mocking with mockito
Demystifying Static Mocking With Mockito
November 28, 2023 - When using the inline mock maker, it is possible to mock static method invocations within the current thread and a user-defined scope. This way, Mockito assures that concurrently and sequentially running tests do not interfere.
🌐
GitHub
github.com › mockito › mockito › issues › 1728
The future of inline mock maker · Issue #1728 · mockito/mockito
June 11, 2019 - * * This class will serve as the programmatic entry point to all mockito internal MockMakers. * Currently the default and only mock maker is the subclassing engine, but with enough feedback we can later * promote the inlining engine for features like final class/methods mocks.
Author   arturdryomov
🌐
Flintk12
flintk12.com
Flint | Personalized learning built for schools
Watch the AI mock up an example student interaction, to see exactly how it would help a struggling student or push an excelling student to go further. Upload rubrics (AP, IB, etc.) for the AI to follow when providing feedback to students, or edit the generated rubric to your liking. Describe what you want in natural language, and let AI do the prompt engineering for you. No prompt engineering skills required. Provide students with inline writing feedback from AI that follows a rubric and guardrails set by the teacher.
🌐
Javadoc.io
javadoc.io › static › org.mockito › mockito-core › 3.2.4 › org › mockito › plugins › InlineMockMaker.html
InlineMockMaker (Mockito 3.2.4 API)
Extension to MockMaker for mock makers that changes inline method implementations and need keep track of created mock objects.
🌐
TestMu AI Community
community.testmu.ai › ask a question
What is the difference between mockito-core and mockito-inline? - TestMu AI Community
February 6, 2025 - What is the difference between mockito-core and mockito-inline? In my project, we already have the mockito-core dependency. However, I need to stub a static method, which requires adding the mockito-inline dependency. I…
Find elsewhere
🌐
Maven Repository
mvnrepository.com › artifact › org.mockito › mockito-inline
Maven Repository: org.mockito » mockito-inline
March 9, 2023 - Mockito preconfigured inline mock maker (intermediate and to be superseeded by automatic usage in a future version)
🌐
Google Groups
groups.google.com › g › mockito › c › EbbD5xwhq5Y
Inline mock maker cannot create mock if some annotations are not on the class path
December 22, 2017 - at org.mockito.internal.creation.bytebuddy.InlineByteBuddyMockMaker.createMockType(InlineByteBuddyMockMaker.java:200)
🌐
Adobe
adobe.com › uk › products › firefly.html
Adobe Firefly - Free Generative AI for Creatives
December 4, 2023 - Explore and create faster with teammates on an infinite canvas. Visualise, test and align on ideas for mood boards, storyboards, brand identities and mock-ups.
🌐
Team Rockstars IT
teamrockstars.nl › home › mock final class and final method with mockito
Mock Final Class And Final Method With Mockito » Team Rockstars IT
December 5, 2023 - In the file org.mockito.plugins.MockMaker paste this text mock-maker-inline, now the mock maker is available during tests.
🌐
GitHub
github.com › mockito › mockito › issues › 2589
Switch the default mockmaker to the inline mockmaker on JDK 17+ · Issue #2589 · mockito/mockito
March 8, 2022 - Detect the current JDK version and make the inline mockmaker the default on JDK 17+, but keep the subclass mockmaker the default for older JDK versions
Author   TimvdLippe
🌐
Pewter Report
pewterreport.com › home › bucs mock draft roundup 2026: post-free agency edition
Bucs Mock Draft Roundup 2026: Post-Free Agency Edition
It’s hard to get past the loss of a legend like Evans, but Bucs general manager Jason Licht and his staff have done well to add some more attitude to the Tampa Bay defense while retaining a reliable target and blocker in Otton and adding a real difference maker to the backfield in Gainwell.
Published   March 15, 2026
🌐
GitHub
github.com › mockito › mockito › issues › 1142
mock-maker-inline slow down my tests · Issue #1142 · mockito/mockito
July 5, 2017 - I actived the mock-maker-inline to mock no-open Kotlin classes following this article. Previusly my tests were kind of slow: ~20s but now the time is ~35s. Mockito version: 2.8.47
Author   BraisGabin
🌐
Refactoring.Guru
refactoring.guru › home › design patterns › creational patterns
Singleton
January 1, 2026 - It may be difficult to unit test the client code of the Singleton because many test frameworks rely on inheritance when producing mock objects. Since the constructor of the singleton class is private and overriding static methods is impossible in most languages, you will need to think of a creative way to mock the singleton.
🌐
rieckpil
rieckpil.de › home › toolbox insights › how to configure mockito agent for java 21+ without warning
How to Configure Mockito Agent for Java 21+ Without Warning - rieckpil
September 8, 2025 - The first part explains that Mockito is self-attaching its inline mock maker and directs us to configure it as a proper Java agent.