5

I am trying to add the allopen plugin for android.

I added this to my build.gradle(.)

ext.kotlin_version = '1.3.61'

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

I added this to my app/build.gradle

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

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

Due to the project structure, the annotation is in a different module than the one belonging to App

I added this In sidemodule/debug/Mockable

@Target(AnnotationTarget.ANNOTATION_CLASS)
annotation class Mockable

And then this in sidemodule/debug/OpenClass

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

And then this in sidemodule/release/OpenClass

@Target(AnnotationTarget.CLASS)
annotation class OpenClass

I added @OpenClass to my destined to be mocked class However, I'm still receiving:

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

How do I fix it?

Lena Bru
  • 13,521
  • 11
  • 61
  • 126

1 Answers1

0

Was just trying same thing here....this is what worked for me

I added following to build.gradle

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

And then created a OpenForTesting.kt file in com.mypackagecontaining

@com.mypackage.OpenForTesting
annotation class OpenForTesting

And then finally added @OpenForTesting annotation

@OpenForTesting
class SomeClassUnderTest {
John O'Reilly
  • 10,000
  • 4
  • 41
  • 63