BRDroid
BRDroid

Reputation: 4388

Mocking a final class using MockitoKotlin2

Hi I am using MockitoKotlin2 library for mocking and I am trying to mock a final class of retrofit how can I do that, does it support mocking final classes, what is the solution please.

Error

org.mockito.exceptions.base.MockitoException:
Cannot mock/spy class retrofit2.Response
Mockito cannot mock/spy because :
- final or anonymous class
at com.xx.xxx.test.scenarios.SerOrderViewScenario.setup(SerOrderViewScenario.kt:147)
at com.xx.xxxx.test.BaseTest.scenario(BaseTest.java:75)
at com.xx.xxx.test.NoSerRequiredTests.testFragment_AStart(NoSerRequiredTests.java:34)
at java.lang.reflect.Method.invoke(Native Method)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at androidx.test.internal.runner.junit4.statement.RunBefores.evaluate(RunBefores.java:80)
at androidx.test.internal.runner.junit4.statement.RunAfters.evaluate(RunAfters.java:61)
at androidx.test.rule.ActivityTestRule$ActivityStatement.evaluate(ActivityTestRule.java:527)
at org.junit.rules.TestWatcher$1.evaluate(TestWatcher.java:55)
at org.junit.rules.RunRules.evaluate(RunRules.java:20)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at androidx.test.runner.AndroidJUnit4.run(AndroidJUnit4.java:104)
at org.junit.runners.Suite.runChild(Suite.java:128)
at org.junit.runners.Suite.runChild(Suite.java:27)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at org.junit.runner.JUnitCore.run(JUnitCore.java:115)
at androidx.test.internal.runner.TestExecutor.execute(TestExecutor.java:56)
at androidx.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:388)
at com.xx.xxx.test.runner.UnlockDeviceAndroidJUnitRunner.onStart(UnlockDeviceAndroidJUnitRunner.java:42)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:2145)

Retrofit function I am trying to mock

package retrofit2;

/** An HTTP response. */
public final class Response<T> {
  /** Create a synthetic successful response with {@code body} as the deserialized body. */
  public static <T> Response<T> success(@Nullable T body) {
    return success(body, new okhttp3.Response.Builder() //
        .code(200)
        .message("OK")
        .protocol(Protocol.HTTP_1_1)
        .request(new Request.Builder().url("http://localhost/").build())
        .build());
  }

this is not I am trying to mock in my scenario class for UI testing

import com.nhaarman.mockitokotlin2.mock

val response: Response<GenericResponse> = mock()

I have read about adding org.mockito:mockito-inline:2.13.0 but not sure as I am using mockitokotlin2

How to mock a final class with mockito

Any suggestions please

thanks in advance R

Upvotes: 1

Views: 553

Answers (1)

rieckpil
rieckpil

Reputation: 12031

You can opt-in to use a InlineMockMaker (that supports mocking final classes), by adding file named org.mockito.plugins.MockMaker inside src/test/resources/mockito-extensions with the following content:

mock-maker-inline

This instructs Mockito to not use the default MockMaker. The mockito-inline dependency does this for you.

So I would first try to add the inline dependency to your project and if it doesn't work with your MockitoKotlin2 setup, create the file on your own.

Upvotes: 0

Related Questions