I cannot mock the final Kotlin class using Mockito 2. Also, I use Robolectric.
This is my test code:
@RunWith(RobolectricTestRunner.class)
@Config(constants = BuildConfig.class, sdk = 21)
public class Test {
@Mock
MyKotlinLoader kotlinLoader;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
}
}
The test does not work when we try to initialize mocks in a method setUp().
In addition, I use the following gradle dependencies in my code:
testCompile 'org.robolectric:robolectric:3.3.2'
testCompile 'org.robolectric:shadows-multidex:3.3.2'
testCompile 'org.robolectric:shadows-support-v4:3.3.2'
testCompile("org.powermock:powermock-api-mockito2:1.7.0") {
exclude module: 'hamcrest-core'
exclude module: 'objenesis'
}
testCompile 'junit:junit:4.12'
testCompile 'org.mockito:mockito-inline:2.8.9'
All other unit tests pass using this configuration, but as soon as I try to make fun of the Kotlin class, it throws the following error:
Mockito cannot mock/spy because :
- final class
Note that I am using Mockito version 2, and I am using a dependency inlinethat automatically makes it possible to mimic the final classes.