@Rule error in Kotlin: how to fix "The @Rule must be public" in Android tests
Hi everyone! I just started learning Android testing with Espresso and Kotlin. I have a simple test class for an Activity, and I'm trying to use `ActivityTestRule`. The code looks like this:\n\n```kotlin\n@RunWith(AndroidJUnit4::class)\nclass MainActivityTest {\n\n @Rule\n val activityRule = ActivityTestRule(MainActivity::class.java)\n\n @Test\n fun testButtonClick() {\n onView(withId(R.id.my_button)).perform(click())\n }\n}\n```\n\nBut when I run the test, I get this error:\n\n```\njava.lang.Exception: The @Rule must be public\n```\n\nI googled it, and people say that in Java you need to make the field `public`, but in Kotlin I declared it as `val` — isn't that a public field? I tried adding `@JvmField`, but it still doesn't work. Please tell me how to properly declare an `A
No answers yet.
Log in to leave an answer
Blogs
Generics in TypeScript: A Complete Guide to Generics
const at the end of a C++ method: what it means and how to use it in 2025
How to Call Objective-C Code from Swift: Friendship of Languages in iOS
Python Programming Skills: How to Maintain Them in 2025