【问题标题】:How to enable AllOpen plugin for android如何为 android 启用 AllOpen 插件
【发布时间】:2020-05-18 09:22:19
【问题描述】:

我正在尝试为 android 添加 allopen 插件。

我将此添加到我的build.gradle(.)

ext.kotlin_version = '1.3.61'

classpath "org.jetbrains.kotlin:kotlin-allopen:$kotlin_version"

我将此添加到我的app/build.gradle

apply plugin: 'kotlin-allopen'
apply plugin: 'io.fabric'

allOpen {
    annotation('com.myapp.sidemodule.Mockable')
}

由于项目结构,注解与属于 App 的模块位于不同的模块中

我添加了这个 在sidemodule/debug/Mockable中

@Target(AnnotationTarget.ANNOTATION_CLASS)
annotation class Mockable

然后这个在sidemodule/debug/OpenClass中

@Mockable
@Target(AnnotationTarget.CLASS)
annotation class OpenClass

然后这个在sidemodule/release/OpenClass中

@Target(AnnotationTarget.CLASS)
annotation class OpenClass

我添加了 @OpenClass给我注定要被嘲讽的班级 但是,我仍然收到:

E/TestRunner: failed: testCanDetectBadResponse(com.myapp.test.core.SomeTests)
    ----- begin exception -----
    org.mockito.exceptions.base.MockitoException: 
    Cannot mock/spy class com.myapp.models.MyResponse
    Mockito cannot mock/spy because :
     - final class
        at com.nhaarman.mockitokotlin2.SpyingKt.spy(Spying.kt:52)
        at com.myapp.test.core.SomeTests.testCanDetectBadResponse(SomeTests.kt:163)
        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 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.ext.junit.runners.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:392)
        at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:2189)
    ----- end exception -----

我该如何解决?

【问题讨论】:

  • 你能不能尝试用注解类 Mockable 的 @Target(AnnotationTarget.CLASS) 替换 @Target(AnnotationTarget.ANNOTATION_CLASS) ?
  • 它没有帮助。

标签: android kotlin-allopen


【解决方案1】:

只是在这里尝试同样的事情......这对我有用

我在build.gradle添加了关注

allOpen {
    annotation('com.mypackage.OpenForTesting')
}

然后在com.mypackage里面创建了一个OpenForTesting.kt文件

@com.mypackage.OpenForTesting
annotation class OpenForTesting

然后最后加上@OpenForTesting注解

@OpenForTesting
class SomeClassUnderTest {

【讨论】:

    猜你喜欢
    • 2017-06-18
    • 2019-11-16
    • 2017-05-18
    • 2014-01-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多