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
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!

🌐
InfoQ
infoq.com › news › 2023 › 01 › mockito-5
Mockito 5 Supports Mocking Constructors, Static Methods and Final Classes out of the Box - InfoQ
January 30, 2023 - Before this release, Mockito didn't support mocking final classes out of the box, such as the following final class which contains one method: public final class Answer { public String retrieveAnswer() { return "The answer is 2"; } } In the unit test, a stub is used to replace the answer of the retrieveAnswer() method: @Test void testMockito() { Answer mockedAnswer = mock(); String answer = "42 is the Answer to the Ultimate Question of Life, the Universe, and Everything"; when(mockedAnswer.retrieveAnswer()).thenReturn(answer); assertEquals(answer, mockedAnswer.retrieveAnswer()); }
🌐
Baeldung
baeldung.com › home › testing › mock final classes and methods with mockito
Mock Final Classes and Methods with Mockito | Baeldung
August 13, 2025 - In this quick article, we discussed how to mock final classes and methods with Mockito by using a Mockito extension and PowerMock.
🌐
Java Guides
javaguides.net › 2023 › 10 › mockito-mocking-final-classes-and.html
Mockito Mocking Final Classes and Methods
July 12, 2024 - This is particularly useful when you need to test code that interacts with third-party libraries or legacy code with final classes and methods. This tutorial will demonstrate how to mock final classes and methods in Mockito. To use Mockito with JUnit 5 and enable mocking of final classes and ...
🌐
Medium
medium.com › @igorski › mock-final-classes-with-mockito-70e7ee836046
Mock final classes with Mockito | by Igor Stojanovski | Medium
May 10, 2018 - @ExtendWith(MockitoExtension.class) class PersonTest { @Mock PinProvider pinProvider; @Test public void shouldCreatePersonWithCorrectPin() { String samplePin = "samplePin"; when(pinProvider.getPin()).thenReturn(samplePin); Person person = new Person("Star", "Lord", pinProvider); assertThat(person.getPin()).isEqualTo(samplePin); } } Since it is a JUnit 5 test is uses the official MockitoExtension. It is supposed to test the following class: @Getter public class Person { private final String name; private final String surname; private final String pin; public Person(String name, String surname, PinProvider pinProvider) { this.name = name; this.surname = surname; this.pin = pinProvider.getPin(); } }
🌐
GitHub
github.com › mockito › mockito › issues › 2789
Documentation describes a broken way to mock final classes · Issue #2789 · mockito/mockito
November 13, 2022 - Documentation stays that for mocking final classes, one needs to add file mockito-extensions/org.mockito.plugins.MockMaker:
Author   asolntsev
🌐
DZone
dzone.com › testing, deployment, and maintenance › testing, tools, and frameworks › mock final class
Mock Final Class
March 17, 2014 - public final class Plane { public static final int ENGINE_ID_RIGHT = 2; public static final int ENGINE_ID_LEFT = 1; public boolean verifyAllSystems() { throw new UnsupportedOperationException("Fail if not mocked!"); } public void startEngine(int engineId) { throw new UnsupportedOperationException( "Fail if not mocked!
🌐
Medium
medium.com › @ramesh.kc825 › mock-final-method-and-final-class-testing-274675a9152a
Mock Final Method And Final Class Testing | by Ramesh KC | Medium
January 19, 2020 - 2. To write the test case for the final class, the steps is same to create a text file and add a single line text mock-maker-inline ... import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; import org.assertj.core....
Find elsewhere
🌐
Davidvlijmincx
davidvlijmincx.com › home › mockito › mock final class and final method with mockito
Mock final class and final method with Mockito
March 8, 2022 - This post explains how to mock final methods and classes. Mockito doesn't support this behavior by default. To stub final methods and mock final classes, we need to enable the inline mock maker first.
🌐
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 - Below is an example of how we can achieve this. The code is run before and after each test so that mockito can tell which test is invalid. With the inline mock maker, you can mock final classes and methods. You can do this using mock(SomeClazz.class) like you would with every other object you want to mock.
🌐
Wesome
wesome.org › mockito-3-mock-final-class-and-method
Mockito 3 Mock Final Class and Method | wesome.org
Mockito by default can mock interface, abstract class, and normal class, but it cannot mock the final class and final method.
🌐
GitHub
github.com › android › codelab-android-dagger › issues › 77
Mockito cannot mock final classes error · Issue #77 · android/codelab-android-dagger
October 16, 2023 - To fix the below error which occurs when running unit tests that have a dependency with the UserManager class, we need to update Mockito to a version higher than 5.0.0 because mocking final classes feature is enabled by default.
Author   khahani
🌐
Quora
quora.com › Is-there-a-way-to-mock-a-final-class-in-Java-using-Mockito
Is there a way to mock a final class in Java using Mockito? - Quora
Mocking frameworks make it easy to write test, but they have their limitations. Both Mockito and EasyMock cannot mock classes (or methods) that are final.Before Mockito can be used for mocking final classes and methods, it need...
🌐
Prgrmmng
prgrmmng.com › home › series › java testing junit mockito testcontainers › mocking final classes and methods with mockito
Mocking Final Classes and Methods with Mockito | prgrmmng.com
September 8, 2025 - Learn how to mock final classes and methods in Mockito, handle legacy code, and ensure reliable unit tests with JUnit 5 and best practices
🌐
TestMu AI Community
community.testmu.ai › ask a question
What is the correct way to mock a final class with Mockito? - TestMu AI Community
February 24, 2025 - I have a final class like this: public final class RainOnTrees { public void startRain() { // some code here } } I am using this class inside another class: public class Seasons { RainOnTrees rain = new RainOnTrees(); public void ...
🌐
GitHub
github.com › dart-lang › mockito › issues › 635
Mocking final classes? · Issue #635 · dart-lang/mockito
May 16, 2023 - So currently I get the following error when trying to mock a final class: Error: The class 'ClassName' can't be implemented outside of its library because it's a final class. In java there is a package called mockito-inline which somehow solves mocking final classes (don't know how).
Published   May 16, 2023
Author   slaci
🌐
Medium
medium.com › pragmatic-programmers › mocking-final-classes-and-methods-fc0c89143744
Mocking Final Classes and Methods | by Kenneth Kousen | The Pragmatic Programmers | Medium
May 3, 2023 - You can’t extend those classes, and by definition you can’t override the methods either. You can find the answers to that quandary in the following video: ... Mockito can handle mocking final classes and final methods without a problem.
🌐
GitHub
github.com › mockito › mockito › wiki › What's-new-in-Mockito-2
What's new in Mockito 2
The extension repository is here ... JUnit 5 goes live we will look into providing built-in integration (in Mockito). Mockito Continuous Integration and Deployment improvements · For a long time our users suffered a disbelief when Mockito refused to mock a final class...
Author   mockito
🌐
W3Docs
w3docs.com › java
How to mock a final class with mockito
Here's an example of how to mock a final class with PowerMockito: import org.powermock.api.mockito.PowerMockito; import org.powermock.core.classloader.annotations.PrepareForTest; @PrepareForTest(FinalClass.class) public class TestClass { @Test public void testMethod() { FinalClass mock = PowerMockito.mock(FinalClass.class); // Set up mock behavior, etc.
🌐
Stack Overflow
stackoverflow.com › questions › 79604591 › how-to-mock-a-final-class-with-mockito-core-5-2-0-and-without-mockito-inline
java - How to mock a final class with mockito-core > 5.2.0 and without mockito-inline? - Stack Overflow
Firstly, mockito-inline is now deprecated because it is now the default - mockito-core now uses the inline mock maker. See github.com/mockito/mockito/releases/tag/v5.0.0 Secondly, make sure you are not pulling in some unexpected mockito version (print tree of dependencies using your build tool - Spring has its own version overrides).