android - Kotlin and new ActivityTestRule : The @Rule must be public -
i'm trying make ui test android app in kotlin. since new system using activitytestrule, can't make work: compiles correctly, , @ runtime, get:
java.lang.exception: @rule 'mactivityrule' must public. @ org.junit.internal.runners.rules.rulefieldvalidator.adderror(rulefieldvalidator.java:90) @ org.junit.internal.runners.rules.rulefieldvalidator.validatepublic(rulefieldvalidator.java:67) @ org.junit.internal.runners.rules.rulefieldvalidator.validatefield(rulefieldvalidator.java:55) @ org.junit.internal.runners.rules.rulefieldvalidator.validate(rulefieldvalidator.java:50) @ org.junit.runners.blockjunit4classrunner.validatefields(blockjunit4classrunner.java:170) @ org.junit.runners.blockjunit4classrunner.collectinitializationerrors(blockjunit4classrunner.java:103) @ org.junit.runners.parentrunner.validate(parentrunner.java:344) @ org.junit.runners.parentrunner.<init>(parentrunner.java:74) @ org.junit.runners.blockjunit4classrunner.<init>(blockjunit4classrunner.java:55) @ android.support.test.internal.runner.junit4.androidjunit4classrunner.<init>(androidjunit4classrunner.java:38) @ android.support.test.runner.androidjunit4.<init>(androidjunit4.java:36) @ java.lang.reflect.constructor.constructnative(native method) @ java.lang.reflect.constructor.newinstance(constructor.java:417) @ android.support.test.internal.runner.junit4.androidannotatedbuilder.buildandroidrunner(androidannotatedbuilder.java:57) @ android.support.test.internal.runner.junit4.androidannotatedbuilder.runnerforclass(androidannotatedbuilder.java:45) @ org.junit.runners.model.runnerbuilder.saferunnerforclass(runnerbuilder.java:57) @ org.junit.internal.builders.alldefaultpossibilitiesbuilder.runnerforclass(alldefaultpossibilitiesbuilder.java:29) @ org.junit.runner.computer.getrunner(computer.java:38) @ org.junit.runner.computer$1.runnerforclass(computer.java:29) @ org.junit.runners.model.runnerbuilder.saferunnerforclass(runnerbuilder.java:57) @ org.junit.runners.model.runnerbuilder.runners(runnerbuilder.java:98) @ org.junit.runners.model.runnerbuilder.runners(runnerbuilder.java:84) @ org.junit.runners.suite.<init>(suite.java:79) @ org.junit.runner.computer.getsuite(computer.java:26) @ android.support.test.internal.runner.testrequestbuilder.classes(testrequestbuilder.java:691) @ android.support.test.internal.runner.testrequestbuilder.build(testrequestbuilder.java:654) @ android.support.test.runner.androidjunitrunner.buildrequest(androidjunitrunner.java:329) @ android.support.test.runner.androidjunitrunner.onstart(androidjunitrunner.java:226) @ android.app.instrumentation$instrumentationthread.run(instrumentation.java:1584)
here how declared mactivityrule:
runwith(javaclass<androidjunit4>()) largetest public class radistest { rule public val mactivityrule: activitytestrule<mainactivity> = activitytestrule(javaclass<mainactivity>()) ... }
it public :/
junit allows provide rules through test class field or getter method.
what annotated in kotlin property though, junit won't recognize.
here possible ways specify junit rule in kotlin:
through annotated getter method
from m13, annotation processor supports annotation targets. when write
@rule public val mactivityrule: activitytestrule<mainactivity> = activitytestrule(javaclass<mainactivity>())
though, annotation use property
target default (not visible java).
you can annotate property getter however, public , satisfies junit requirements rule getter:
@get:rule public val mactivityrule: activitytestrule<mainactivity> = activitytestrule(javaclass<mainactivity>())
alternatively, can define rule function instead of property (achieving manually same result @get:rule
).
through annotated public field
kotlin allows since beta candidate deterministically compile properties fields on jvm, in case annotations , modifiers apply generated field. done using kotlin's @jvmfield
property annotation answered @jkschneider.
side note: sure prefix rule
annotation @
character the supported syntax annotations, , avoid @publicfield
will dropped.
Comments
Post a Comment