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

🌐
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.
🌐
Java Guides
javaguides.net › 2023 › 10 › mockito-mocking-final-classes-and.html
Mockito Mocking Final Classes and Methods
July 12, 2024 - Mockito, by default, cannot mock final classes and methods. However, with the introduction of the mockito-inline artifact, it is possible to mock final classes and methods. This is particularly useful when you need to test code that interacts ...
🌐
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(); } }
🌐
Android Pro
androidpro.io › home › blog › how to mock final classes in kotlin with mockito?
How to mock final classes in Kotlin with Mockito? - Android Pro
September 7, 2023 - To enable mocking final classes, configure MockMaker: In app/src/test/resources/mockito-extensions add file:
🌐
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 - Mockito could mock final classes by supplying the mockito-inline dependency. However, starting with Mockito 5.0.0, the inline MockMaker is used by default and supports mocking of constructors, static methods and final classes.
🌐
GitHub
github.com › dart-lang › mockito › issues › 635
Mocking final classes? · Issue #635 · dart-lang/mockito
May 16, 2023 - Hello, Dart 3 introduced some class modifiers, like final. A class marked with final cannot be extended and mocks are basically created by extending a class. So currently I get the following error when trying to mock a final class: Error...
Published   May 16, 2023
Author   slaci
Find elsewhere
Top answer
1 of 9
70

Weird that your solution seems to work.
According to their documentation on Github it says.

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!

My working structure now looks like this.

2 of 9
36

I couldn't get it working with the configuration file either; however, the Mockito team is so kind and also provides a pre-configured Mockito artifact that requires no configuration in the target project.

As a convenience, the Mockito team provides an artifact where this mock maker is preconfigured. Instead of using the mockito-core artifact, include the mockito-inline artifact in your project. Note that this artifact is likely to be discontinued once mocking of final classes and methods gets integrated into the default mock maker.

So, if you use Gradle and want to test your Kotlin code, just add this to your project's dependencies:

testCompile 'org.mockito:mockito-inline:2.8.9'
testCompile('com.nhaarman:mockito-kotlin:1.5.0') {
    exclude group: 'org.jetbrains.kotlin'
    exclude group: 'org.mockito'
}
🌐
Google Groups
groups.google.com › g › mockito › c › d25SPtJ4108
Mockito 2 final class mocking limitations
You can not mock native methods and can no longer strip synchronized modifiers from mocked instances. Package-visible methods will not be mocked by the new mockmaker. Hope that answers your question. Enjoy mocking with Mockito 2!
🌐
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 - 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.
🌐
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...
🌐
Davidvlijmincx
davidvlijmincx.com › home › mockito › mock final class and final method with mockito
Mock final class and final method with Mockito
March 8, 2022 - In the file org.mockito.plugins.MockMaker paste this text mock-maker-inline, now the mock maker is available during tests. With inline mock maker, mocking a final class or method is the same as mocking a non-final class or method. Below we have an example that mocks the final class Cat.
🌐
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 › 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
🌐
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.
🌐
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
🌐
Mindorks
blog.mindorks.com › mockito-cannot-mock-in-kotlin
Mockito cannot mock because : final class in Kotlin - MindOrks
May 31, 2019 - In this blog, we will talk about how we can mock the final class using Mockito. By default, we can't mock the final classes using mockito. Here in this blog, I have talked about multiple ways to test final classes.
🌐
Antonio Leiva
antonioleiva.com › mockito-2-kotlin
How to mock final classes on Kotlin using Mockito 2 (KAD 23) — Antonio Leiva
May 2, 2017 - @Test fun testClosedClass() { val c = Mockito.mock(ClosedClass::class.java) c.doSomething() verify(c).doSomething() } If you use Mockito 1.x, you’ll get the following error: Mockito cannot mock/spy following: - final classes - anonymous classes - primitive types
🌐
LinkedIn
linkedin.com › pulse › mock-mockfinal-classes-finally-yasin-bhojawala
To mock or not to mock...final classes (finally)
September 27, 2020 - An important thing to note here is that the mocking of final classes was not supported not because it's "not a good thing to do" or it's "anti-pattern", it was because they did "not have sufficient support".