Simply adding

testImplementation("org.mockito:mockito-inline:2.8.47")

resolved the issue.

Answer from Janani Sampath Kumar on Stack Overflow
🌐
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
Discussions

Unable to mock final classes with android instrumentation tests using Mockito 5.0.0
There was an error while loading. Please reload this page · Consider the following Kotlin classes: More on github.com
🌐 github.com
2
January 16, 2023
java - How to mock a final class with mockito - Stack Overflow
I'm using 5.3.1, which I read online is supposed to support mocking final class by default. ... but I get runtime error: "Mockito cannot mock/spy because : - final class" 2023-10-19T22:40:00.51Z+00:00 More on stackoverflow.com
🌐 stackoverflow.com
java - Cannot mock final Kotlin class using Mockito 2 - Stack Overflow
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. ... The mock-maker-inline works but it's really slow I recommend you to use the all-open Compiler Plugin from Kotlin... More on stackoverflow.com
🌐 stackoverflow.com
July 17, 2017
After AndroidX: Cannot mock/spy class MyClassScope because final class
was learning and working based on this Kodein example.. The original example project is working. I have created a new project to write everthing manually, so that I can understand each step. But th... More on github.com
🌐 github.com
0
January 28, 2019
🌐
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.
🌐
Medium
medium.com › android-news › quickly-solve-mocking-with-kotlin-551372c285f4
Quickly solve Mocking with Kotlin | by Akshay Chordiya | AndroidPub | Medium
October 19, 2017 - Just change your Mockito Gradle dependency from the Mockito core: testImplementation "org.mockito:mockito-core:$mockito_version" ... TADA….it’s solved! Easy Peasy! Use Mockito-Kotlin library which provides some helper functions to make the ...
🌐
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 - None of these classes are mockable in an instrumentation test, i.e. in Kotlin: @RunWith(AndroidJUnit4::class) class ExampleInstrumentedTest { @Test fun mockFinalClasses() { val mockSealedClass = Mockito.mock<TestSealedClass.SealedClass1>() val mockDataClass = Mockito.mock<TestDataClass>() val mockJavaClass = Mockito.mock<JavaClass>() } }
Author   ZOlbrys
🌐
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.
🌐
Antonio Leiva
antonioleiva.com › mockito-2-kotlin
How to mock final classes on Kotlin using Mockito 2 (KAD 23) — Antonio Leiva
May 2, 2017 - Mockito cannot mock/spy following: - final classes - anonymous classes - primitive types · As we have said, Mockito 2 is able to mock it all, so we’re going to update the dependency. At the time of writing this article the latest version is 2.8.9. But check it out because they are updating very often lately.
Find elsewhere
🌐
Codexpedia
codexpedia.com › home › android › mockito cannot mock final classes in kotlin
Mockito cannot mock final classes in Kotlin - Codexpedia
October 8, 2017 - org.mockito.exceptions.base.MockitoException: Cannot mock/spy class com.example.data.model.WeatherData Mockito cannot mock/spy because : – final class This happens because Kotlin classes and functions are declared as final/closed by default, but Mockito cannot mock/spy the object if it is ...
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 › 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) It is really almost 1:1 , just renamed some variables/classes differently to see. e.g.: ActivityScopeClass --> SpecialCommand2Scope · Furtheron I use kotlin 1.3.20 and I've changed all Android support libraries like: implementation 'com.android.support:appcompat-v7:28.0.0' through AndroidX stuff like: implementation 'androidx.appcompat:appcompat:1.0.0-beta01' Thes
Author   MrSilverstein
🌐
GitHub
github.com › mockito › mockito-kotlin › issues › 442
Why mockito cannot mock fun interface? · Issue #442 · mockito/mockito-kotlin
September 13, 2021 - fun interface FunInterfaceToTest ... withSettings().extraInterfaces(...)) You are seeing this disclaimer because Mockito is configured to create inlined mocks....
Author   Jacks0N23
🌐
Kotlin Discussions
discuss.kotlinlang.org › android
Mockito 2 in Kotlin - Android - Kotlin Discussions
January 14, 2017 - Hi, According to several descriptions from Stackoverflow etc. I tried to use mockito 2 in my project to mock final classes/functions, Unfortunately I could not bring it to work, because I cant figure out how to configu…
🌐
Medium
medium.com › 21buttons-tech › mocking-kotlin-classes-with-mockito-the-fast-way-631824edd5ba
Mocking Kotlin classes with Mockito — the fast way | by Brais Gabín Moreira | 21 Buttons Engineering | Medium
April 4, 2018 - Mockito can’t mock final classes and, by default, all classes in Kotlin are final. Let’s see how to workaround this issue without any speed decrease.
🌐
Stack Overflow
stackoverflow.com › questions › 39744420 › kotlin-mockito-cannot-mock-spy-spring-rest-api
unit testing - Kotlin - Mockito cannot mock/spy (Spring REST API) - Stack Overflow
June 5, 2017 - Since all classes in Kotlin are final by default, and Mockito can't spy on final classes: Cannot mock/spy class bye.persistence.jdbcTrial Mockito cannot mock/spy following: - final classes -
🌐
GitHub
github.com › mockito › mockito-kotlin › issues › 441
Mockito cannot mock/spy because : VM does not support modification of given type · Issue #441 · mockito/mockito-kotlin
September 9, 2021 - Mockito cannot mock/spy because : VM does not support modification of given type#441 · Copy link · MastaP · opened · on Sep 9, 2021 · Issue body actions · Hi, After switching kotlin's api/language from 1.4 to 1.5, the following code: @Test ...
Author   MastaP
🌐
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
🌐
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.