Simply adding
testImplementation("org.mockito:mockito-inline:2.8.47")
resolved the issue.
Answer from Janani Sampath Kumar on Stack OverflowSimply adding
testImplementation("org.mockito:mockito-inline:2.8.47")
resolved the issue.
- Follow this steps: https://antonioleiva.com/mockito-2-kotlin/
Update the libraries and replace
androidTestImplementationwithtestImplementation. In my case:testImplementation "com.nhaarman.mockitokotlin2:mockito-kotlin:2.1.0 (update)
testImplementation 'org.mockito:mockito-core:3.0.0' (update)
testImplementation 'org.mockito:mockito-android:2.24.5' (replace androidTestImplementation with testImplementation)
Remove MockitoAnnotations.initMocks(this) from each Test class.
Tested with Android Studio 3.5
Unable to mock final classes with android instrumentation tests using Mockito 5.0.0
java - How to mock a final class with mockito - Stack Overflow
java - Cannot mock final Kotlin class using Mockito 2 - Stack Overflow
After AndroidX: Cannot mock/spy class MyClassScope because final class
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
...
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.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!
PowerMock implements its own MockMaker which leads to incompatibility with Mockito mock-maker-inline, even if PowerMock is just added as a dependency and not used. If two org.mockito.plugins.MockMaker exist in path then any only one can be used, which one is undetermined.
PowerMock can however delegate calls to another MockMaker, and for then tests are run without PowerMock. Since PowerMock 1.7.0 this can be configured with using the PowerMock Configuration.
The MockMaker can be configured by creating the file org/powermock/extensions/configuration.properties and setting:
mockito.mock-maker-class=mock-maker-inline
Example of using Mockito mock-maker-inline with PowerMock: https://github.com/powermock/powermock-examples-maven/tree/master/mockito2
Since Mockito 2.1.0 there is a possibility to mock final types, enums, and final methods. It was already mentioned in the comments to the original question.
To do this, you’ll need to create a folder (if dont exist) test/resources/mockito-extensions and add there file with the name org.mockito.plugins.MockMaker and this line:
mock-maker-inline

Links to documentation and tutorial