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!

🌐
Baeldung
baeldung.com › home › testing › mock final classes and methods with mockito
Mock Final Classes and Methods with Mockito | Baeldung
August 13, 2025 - PowerMock can mock final classes and methods, which Mockito (before version 2.x) cannot do.
Discussions

unable to test a final class using: testng, mockito, powermock
i am unable to use powermock to test final class when using gradle+testng+mockito+powermock. What steps will reproduce the problem? % unzip powermock-final.zip % cd powermock-final % gradle build W... More on github.com
🌐 github.com
6
February 5, 2017
Tests failures after adding mockito-inline on Java 21
It appears that Mockito cannot mock several classes after adding mockito-inline dependency to the project More on github.com
🌐 github.com
13
April 27, 2024
"Mockito can only mock non-private & non-final classes" with Java 8 and Spring boot 2.5.13
HI, Recently, we stared migrating our java8 , spring boot app to spring boot 2.5.13. Since we are using Spring boot 2.5.13, it's pulling down mockito-core 3.9.0 as per the below list https://do... More on github.com
🌐 github.com
2
June 9, 2022
Documentation describes a broken way to mock final classes
There was an error while loading. Please reload this page · Documentation stays that for mocking final classes, one needs to add file mockito-extensions/org.mockito.plugins.MockMaker: More on github.com
🌐 github.com
5
November 13, 2022
🌐
Google Groups
groups.google.com › g › mockito › c › SL5NBky0Txs
Cannot mock/spy because : final class ... gradle only
You should use `mockito-inline` if you want to spy final classes, so I recommend including it. We will make the inline mockmaker the default in the next major version of Mockito, so it is safe to adopt the new artifact ahead of time. ... -- You received this message because you are subscribed to the Google Groups "mockito" group...
🌐
GitHub
github.com › mockito › mockito-kotlin › issues › 285
Mocking final classes · Issue #285 · mockito/mockito-kotlin
Hi, Kotlin classes are final by default, when i use this library to test kotlin classes, it gives me the following error: org.mockito.exceptions.base.MockitoException: Cannot mock/spy class com.app.network.entity.Movies Mockito cannot mock/spy because : - final class
Author   ghost
🌐
GitHub
github.com › powermock › powermock › issues › 746
unable to test a final class using: testng, mockito, powermock · Issue #746 · powermock/powermock
February 5, 2017 - package powermock; import org.powermock.core.classloader.annotations.PrepareForTest; import org.testng.annotations.Test; import org.powermock.api.mockito.PowerMockito; @PrepareForTest(MyFinal.class) public class MyFinalTest { @Test public void myFinal() { MyFinal myFinal = PowerMockito.mock(MyFinal.class); } } Gradle suite > Gradle test > powermock.MyFinalTest.myFinal FAILED java.lang.IllegalArgumentException: Cannot subclass final class class powermock.MyFinal at org.powermock.api.mockito.repackaged.cglib.proxy.Enhancer.generateClass(Enhancer.java:575) at org.powermock.api.mockito.repackaged.
Author   jwtodd
🌐
Google Groups
groups.google.com › g › mockito › c › fRQMrj2BHko
Mockito 2 message - "Mockito can only mock non-private & non-final classes." correct
"Mockito can only mock non-private & non-final classes." The above contradicts it. Or will the message go away when we enable it using: ... org.mockito.exceptions.base.MockitoException: Mockito cannot mock this class: interface com.company.mobjects.ObjectHandler.
Find elsewhere
🌐
GitHub
github.com › mockito › mockito › issues › 3328
Tests failures after adding mockito-inline on Java 21 · Issue #3328 · mockito/mockito
April 27, 2024 - Java : 21 JVM vendor name : Eclipse ... 2022 OS version : 10.0 · You are seeing this disclaimer because Mockito is configured to create inlined mocks....
Author   MikhailNavitski
🌐
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, ... will be properly mocked and error Mockito cannot mock/spy because your class is final class will be there no more....
🌐
Mockito
site.mockito.org › javadoc › current › org › mockito › Spy.html
Spy (Mockito 2.2.7 API)
Mockito doesn't mock final methods so the bottom line is: when you spy on real objects + you try to stub a final method = trouble. Also you won't be able to verify those method as well. One last warning : if you call MockitoAnnotations.initMocks(this) in a super class constructor then this ...
🌐
i(j)nspector
ijnspector.wordpress.com › 2019 › 03 › 25 › mockito-cannot-mock-spy-final-classes
Mockito cannot mock/spy final classes – i(j)nspector
April 24, 2020 - Before version 2.1.0 Mockito wasn't able to mock and spy final classes, and after this version it is still an experimental feature that is disabled by default. This inspection can help spotting fields whose type is final and the fields are annotated ...
🌐
Medium
medium.com › @igorski › mock-final-classes-with-mockito-70e7ee836046
Mock final classes with Mockito | by Igor Stojanovski | Medium
May 10, 2018 - org.mockito.exceptions.base.Mo... class · At this point before Mockito 2, you generally had two choices. Redesign your code, or use Powermock. And the latter is almost never a good thing....
🌐
GitHub
github.com › mockito › mockito › issues › 2678
"Mockito can only mock non-private & non-final classes" with Java 8 and Spring boot 2.5.13 · Issue #2678 · mockito/mockito
June 9, 2022 - `Mockito cannot mock this class: class org.springframework.data.mongodb.core.MongoTemplate Mockito can only mock non-private & non-final classes. If you're not sure why you're getting this error, please report to the mailing list.
Author   rajeshhereforyou
🌐
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: https://javadoc.io/doc/org.mockito/mockito-core/latest/org/mockito/Mockito.html#39 · BUT It doesn't always work.
Author   asolntsev
🌐
Testim
testim.io › blog › mockito-spy
Mockito Spy: A Guide to When and How to Use It
March 3, 2025 - Before wrapping up, let’s share some final tips on Mockito that will certainly be of value to you. If you want to spy on a class using Mockito, it must have at least one parameterless constructor.
🌐
GitHub
github.com › mockito › mockito-kotlin › issues › 489
Mockito cannot mock this class: class pl.inpost. ...
August 3, 2023 - Mockito cannot mock this class: class pl.inpost.recruitmenttask.presentation.shipmentList.Section$HasId.#489
Author   houssemzaier
🌐
GitHub
github.com › dart-lang › http › issues › 956
Can't mock final classes using Mockito. · Issue #956 · dart-lang/http
June 4, 2023 - We get: The reason is that the class ByteStream was marked as final in that PR #920 and Mockito can't mock final classes. Based on the suggestion "Ask the plugin author to replace final by interface or just remove final.
Author   MagnusJohansson
🌐
j-labs
j-labs.pl › home › tech blog › mocks, stubs and spies in unit testing based on mockito
Mocks, stubs and spies in unit testing based on Mockito. Guidebook | j‑labs
December 9, 2025 - We have used Mockito library to create a PeopleRepository mock, passed it as a PersonService constructor parameter and predefined the answer for the getAllPeople method call. As we can see in the provided example, the use of mock is quite easy and elastic. A spy is a mixed object of a real one and a mocked one. It behaves like the real object, but the behaviour of specific methods can be mocked. Spies are useful when there is a class with plenty of methods and there is a need of mocking only part of them.
🌐
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
🌐
Juejin
juejin.cn › post › 6844904160748535815
Mockito不能mock final类的解决办法
May 16, 2020 - We cannot provide a description for this page right now