Cannot mock Kotlin final class using Mockito 2

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 {

    // more mocks

    @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.

+13
6

Powermock , :

import static org.powermock.api.mockito.PowerMockito.mock;
import static org.powermock.api.mockito.PowerMockito.spy;
import static org.powermock.api.mockito.PowerMockito.when;

@RunWith(RobolectricTestRunner.class)
@Config(constants = BuildConfig.class, sdk = 21)
@PowerMockIgnore({ "org.mockito.*", "org.robolectric.*", "android.*" })
@PrepareForTest({FinalClass1.class, FinalClass2.class})
public class Test {
    @Rule
    public PowerMockRule rule = new PowerMockRule();

    ... // your code here
}
+1

PowerMock MockMaker, Mockito mock-maker-inline, PowerMock . org.mockito.plugins.MockMaker, , .

PowerMock MockMaker, PowerMock. PowerMock 1.7.0 PowerMock.

MockMaker , org/powermock/extensions/configuration.properties :

mockito.mock-maker-class=mock-maker-inline

Mockito mock-maker-inline PowerMock: https://github.com/powermock/powermock-examples-maven/tree/master/mockito2

+13

, . , . , :

import com.nhaarman.mockito_kotlin.mock
class MyFinalClass {...}
(snip)
private val MyFinalClass = mock()

, :

class MyFinalClass : MyInterface {...}
(snip)
private val MyInterface = mock()
0

kotlin .

open .

: open class MyClasss{}

0

Kotlin :

open class OpenClass() : SomeInterface by FinalClass()

, , SomeInterface FinalClass() open . .

I had to resort to this in a project where the target class lived in a library and seemed to be inviolable by a all-opencompiler plugin.

0
source

Try adding this dependency below to your build.gradle.

testImplementation 'org.mockito:mockito-inline:2.8.47'

Replace with your version of mockito instead of 2.8.47. This will help you avoid using Powermock to solve this problem.

You can see in the link below how this thing works.

How to mock the final class with mokito

0
source

All Articles