Skip to content

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

@yogurtearl

Description

@yogurtearl

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

Consider this code:

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

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions