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.MockMakercontaining a single line:mock-maker-inlineAfter 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.

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'
}
You need to update your dependencies.
You are compiling your project with jdk15. Bytebuddy complains it cannot instrument class files generated by this jdk (Unsupported class file major version 59)
Support for java 15 was introduced in ByteBuddy release 1.10.6
Note that you dont need to specify the version of ByteBuddy in your project as Mockito pulls it transitively (unless younhave good reason to force a version).
Compatible ByteBuddy version was introduced in Mockito 3.3.10. See commit changing ByteBuddy version to 1.10.10
Instead of analyzing the history of commits in Mockito, I would recommend going with latest and greatest (3.8.0 at the time of writing)
I add this dependency in my project:
testImplementation("org.mockito:mockito-core:5.2.0")
testImplementation ("org.mockito.kotlin:mockito-kotlin:5.4.0")
For me it's working even when I deleted file org.mockito.plugins.MockMaker from resource folder. I use Android studio: Ladybug | 2024.2.1 Patch 2.
I think this is related to java version you use in your project, mine is:
compileOptions {
sourceCompatibility = JavaVersion.VERSION_11
targetCompatibility = JavaVersion.VERSION_11
}
kotlinOptions {
jvmTarget = "11"
}