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!

🌐
GitHub
github.com › mockito › mockito › issues › 3528
Mockito 5.14 + Java 21 + Maven surefire agent config : Cannot mock final class · Issue #3528 · mockito/mockito
December 6, 2024 - Mockito works fine when mocking normal classes, but specifically not final classes. The pom configuration has been picked up as the Mockito and javaagent warnings do not show up in the maven output anymore.
Author   lwiddershoven
🌐
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
🌐
Google Groups
groups.google.com › g › mockito › c › SL5NBky0Txs
Cannot mock/spy because : final class ... gradle only
I assume that since the test works ... you need the view member email addresses permission to view the original message ... 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 ...
🌐
GitHub
github.com › quarkusio › quarkus › issues › 46866
Mockito cannot mock/spy because final class - Error in Java Quarkus GRPC with Mockito · Issue #46866 · quarkusio/quarkus
March 18, 2025 - Good afternoon. I have the following error in the validation class I'm mocking up: [ERROR] RequestValidatorTest.setUp:32 Mockito Cannot mock/spy class com.cies.grpc.proto.ValidateNumberActivationRequest Mockito cannot mock/spy because: -...
Author   cesarjv
🌐
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.MockitoException: Cannot mock/spy class org.igorski.finalexample.PinProvider Mockito cannot mock/spy because : - final class
🌐
Davidvlijmincx
davidvlijmincx.com › home › mockito › mock final class and final method with mockito
Mock final class and final method with Mockito
March 8, 2022 - 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. When you try to mock a final class without the mockito inline mockmaker you get an error message like this: mockito cannot mock/spy because : - final class.
🌐
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.
Find elsewhere
🌐
Mindorks
blog.mindorks.com › mockito-cannot-mock-in-kotlin
Mockito cannot mock because : final class in Kotlin - MindOrks
May 31, 2019 - The test case which we wrote it was correct but the error shows that we can't mock the final class. ... All Kotlin Classes are Final by default. Now, there are two ways to do it. Let's talk about them one by one. Firstly we can make the Utility class open and then can we test the class using the above code.This can be a problem as because now we have to change the code base. Secondly and most importantly, Mockito 2.10 provided a new update where we can mock the final classes.
🌐
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 mo...
Author   ghost
🌐
GitHub
github.com › mockito › mockito › issues › 2873
Unable to mock final classes with android instrumentation tests using Mockito 5.0.0 · Issue #2873 · mockito/mockito
January 16, 2023 - Consider the following Kotlin classes: data class DataClass(val intValue: Int) ... sealed class SealedClass { class SealedClass1(val intValue: Int) : TestSealedClass() } And the following Java class: final public class JavaClass {} None ...
Author   ZOlbrys
🌐
GitHub
github.com › mockito › mockito › issues › 3556
cannot mock/spy because : final class · Issue #3556 · mockito/mockito
December 17, 2024 - The error indicates that ZoomSDK is a final class, and Mockito cannot mock or spy on final classes by default.
Published   Dec 17, 2024
🌐
Coderanch
coderanch.com › t › 600197 › java › mockito-mock-final-classes
how to use mockito to mock final classes (Java in General forum at Coderanch)
December 12, 2012 - //java.lang.reflect.Field Field mockIT = Mockito.mock(Field.class); Mockito.doThrow(new IllegalAccessException()).when(mockIT).getInt(String.class); mockIT.getInt(str); output: Exception in thread "main" org.mockito.exceptions.base.MockitoException: Cannot mock/spy class java.lang.reflect.Field Mockito cannot mock/spy following: - final classes - anonymous classes - primitive types does any one know how can I mock the Final class
🌐
GitHub
github.com › mockito › mockito › issues › 1601
After AndroidX: Cannot mock/spy class MyClassScope because final class · Issue #1601 · mockito/mockito
January 28, 2019 - org.mockito.exceptions.base.MockitoException: Cannot mock/spy class com.example.simplekodein.di.ActivityScope Mockito cannot mock/spy because : - final class at com.example.simplekodein.MainActivityTest.setup(MainActivityTest.kt:44)
Author   MrSilverstein
🌐
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 - Mocking a final class or method looks the same as mocking non-final classes and methods. Using this mock maker will prevent the mockito cannot mock/spy because – final class exception from happening again.
🌐
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 - However, starting with Mockito 5.0.0, the inline MockMaker is used by default and supports mocking of constructors, static methods and final classes. The subclass MockMaker is still available via the new mockito-subclass artifact, which is necessary on the Graal VM native image as the inline mocker doesn't work. The ArgumentMatcher interface allows the creation of a custom matcher which is used as method arguments for detailed matches. The ArgumentMatcher now supports varargs, with one argument, via the type() method. For example, in order to mock the following method with a varargs argument:
🌐
DEV Community
dev.to › scottshipp › how-to-fix-a-mockito-cannot-mock-this-class-exception-in-a-spring-boot-app-457e
How to Fix A "Mockito cannot mock this class" Exception in A Spring Boot App - DEV Community
September 5, 2019 - [ERROR] testGetGear(org.gearbu... mock this class: interface org.gearbuddy.data.GearRepository. Mockito can only mock non-private & non-final classes....
🌐
GitHub
github.com › mockito › mockito › issues › 1105
mockito-inline can't mock finals · Issue #1105 · mockito/mockito
June 1, 2017 - Im trying to mock JsonObject using mockito-inline. When running from console it runs perfectly but when trying to use Android Studio i get org.mockito.exceptions.base.MockitoException: Cannot mock/spy class com.google.gson.JsonObject Moc...
Author   martofeld
🌐
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
🌐
GitHub
github.com › quarkusio › quarkus › issues › 45025
Quarkus-mockito includes mockito-subclass dependency breaking the mocking of final classes on Java 21+ · Issue #45025 · quarkusio/quarkus
December 10, 2024 - Mockito can mock final classes, but with this mock maker it can not (see Mockito issue referenced above). I am not sure why the Mockito-subclass dependency is used, it might have a purpose or be needed on older JVMs.
Author   lwiddershoven