[JAVA] The @Rule'mActivityRule' must be public. If you convert the Espresso test to Kotlin, an error will occur.

Thing you want to do

Kotlinization of Espresso test

Executed Convert Java File To Kotlin File

trouble

When I run the test I get the following exception

org.junit.internal.runners.rules.ValidationError: The @Rule 'mActivityRule' must be public.

Cause

This is because the properties declared in Kotlin have only getters and setters when viewed from the Java side, and cannot be accessed as public fields.

Solution

If you want to access the field of the class written in Kotlin from Java, add @JvmField annotation as follows

@Rule @JvmField
var mActivityRule = ActivityTestRule(HogeActivity::class.java)

At the end

Since I don't know Kotlin's language specifications, this happens, so I need to learn the basics. I hope it will be helpful for you.

Helpful site

https://kotlinlang.org/docs/reference/java-to-kotlin-interop.html https://proandroiddev.com/fix-kotlin-and-new-activitytestrule-the-rule-must-be-public-f0c5c583a865

Recommended Posts

The @Rule'mActivityRule' must be public. If you convert the Espresso test to Kotlin, an error will occur.
Validated "up to 6 alphanumeric characters", "only full-width characters can be used", and "an error will occur if left blank"
What to do if you get the error Couldn't find Item without an ID
What to do if you get an error in Basic authentication during Rails test code
What to do if you get an error during rails db: reset
What to do if you get an uninitialized constant Likes Controller error
What to do if you get an error when you hit Heroku logs
What to do if you get an "A server is already running." Error when you try to start the rails server
If you want to satisfy the test coverage of private methods in JUnit
What to do if you get the error message unrecognized selector send to instance "***"
What to do if you get an error on heroku rake db: migrate
If you throw null to Servlet with ajax, it will be converted to blank ""
What to do if you get an Argument Error: wrong number of arguments (given 2, expected 0) in your RSpec test