首页 > 解决方案 > 使用 MockitoKotlin2 模拟最终类

问题描述

嗨,我正在使用 MockitoKotlin2 库进行模拟,我正在尝试模拟最终的改造类我该怎么做,它是否支持模拟最终类,请问解决方案是什么。

错误

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)

我试图模拟的改造功能

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());
  }

这不是我想在我的场景类中模拟 UI 测试

import com.nhaarman.mockitokotlin2.mock

val response: Response<GenericResponse> = mock()

我已经阅读了关于添加org.mockito:mockito-inline:2.13.0但不确定因为我使用的是 mockitokotlin2

如何用 mockito 模拟最后一堂课

请有任何建议

在此先感谢 R

标签: androidkotlinmockingmockitoretrofit

解决方案


您可以选择使用 a InlineMockMaker(支持模拟最终类),方法是添加名为org.mockito.plugins.MockMakerinsidesrc/test/resources/mockito-extensions的文件,其中包含以下内容:

mock-maker-inline

这指示 Mockito 不使用默认值MockMakermockito-inline依赖项为您执行此操作。

所以我会首先尝试将内联依赖项添加到您的项目中,如果它不适用于您的 MockitoKotlin2 设置,请自行创建文件。


推荐阅读