android / android/android-test

Give a better error message with you omit `@get:Rule`

Open
#2,393 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
Java
Stars
1.2k
Forks
342
Avg merge
11h 29m
Merged PRs (30d)
2

Description

If you forget `@get:Rule`, it should provide a message that says `"Did you forgot @get:Rule? "`

Consider this code:
```kotlin
// forgot to put the @get:Rule
val composeTestRule = createComposeRule()

@Test
fun `test compose stuff`() = with(composeTestRule) {
setContent {
Text("Hi there")
}
}
```

Without `@get:Rule` you get this:
```
java.lang.NullPointerException
at androidx.test.internal.util.Checks.checkNotNull(Checks.java:41)
at androidx.test.ext.junit.rules.ActivityScenarioRule.getScenario(ActivityScenarioRule.java:127)
at androidx.compose.ui.test.junit4.AndroidComposeTestRule_androidKt.getActivityFromTestRule(AndroidComposeTestRule.android.kt:398)
at androidx.compose.ui.test.junit4.AndroidComposeTestRule_androidKt.access$getActivityFromTestRule(AndroidComposeTestRule.android.kt:1)
at androidx.compose.ui.test.junit4.AndroidComposeTestRule_androidKt$createAndroidComposeRule$1.invoke(AndroidComposeTestRule.android.kt:126)
at androidx.compose.ui.test.junit4.AndroidComposeTestRule_androidKt$createAndroidComposeRule$1.invoke(AndroidComposeTestRule.android.kt:126)
at androidx.compose.ui.test.junit4.AndroidComposeTestRule$special$$inlined$AndroidComposeUiTestEnvironment$1.getActivity(ComposeUiTest.android.kt:215)
at androidx.compose.ui.test.AndroidComposeUiTestEnvironment$AndroidComposeUiTestImpl.getActivity(ComposeUiTest.android.kt:449)
at androidx.compose.ui.test.AndroidComposeUiTestEnvironment$AndroidComposeUiTestImpl.setContent(ComposeUiTest.android.kt:538)
at androidx.compose.ui.test.junit4.AndroidComposeTestRule.setContent(AndroidComposeTestRule.android.kt:381)
```

Should add something like this to `ActivityScenarioRule.getScenario` to give a hint:

```kotlin
checkNotNull(scenario) { "scenario was null, did you forgot @get:Rule somewhere?" }
```

Contributor guide

Open the contributing guide

Research direction

Start at ActivityScenarioRule.getScenario(), the entry point named in the issue, and compare its current null handling with the AndroidComposeTestRule stack trace. The work is done when omitting @get:Rule produces a clear message suggesting that annotation instead of the shown NullPointerException.

Written by the indexing model from the issue text.

Assessment

Tech stack
android, kotlin
Domain
mobile, testing
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
52/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.