:wave: Hi Everyone! I’ve setup some instrumentati...
# compose
b
👋 Hi Everyone! I’ve setup some instrumentation tests, but when I try to run them, I get the following error:
Copy code
android.content.ActivityNotFoundException: Unable to find explicit activity class {com.myapp.android.test/androidx.test.core.app.InstrumentationActivityInvoker$BootstrapActivity}
Anyone run into this before? I’ve found a few things that mention adding the activity to the manifest in your androidTest folder, but I don’t have a manifest there, and have never needed one before. Not sure why I would need one now …
😶 1
n
@Bradleycorn I’m just studying compose testing. Why do you need the Activity? If you need just because the Context object, I’m using the code below and it’s working for me…
Copy code
val context: Context
    get() = ApplicationProvider.getApplicationContext()
b
@nglauber I don’t need the activity. My test won’t even start because of the above error. It seems that the test app is trying to launch some “BootstrapActivity” (which the composeRule then uses to set the content), and it can’t find it. The error asks if I’ve declared the activity in my manifest … Umm, no I haven’t, because my app has no such “BootstrapActivity”, and I have no manifest file in my androidTest folder (nor have I ever needed one).
n
humm… dumb question: are you declaring your tests in
androidTest
folder?
b
yep
It’s an extremely basic test setup. I’m not even trying to test anything at this point, just logging the semantics tree. I AM using Hilt for dependency injection, but I’ve set everything up according to the hilt testing guide (which I have working in other projects no problem).
n
Again, I’m just stating on testing, so more silly questions: Do you have these in your build.gradle:
Copy code
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
and these dependencies
Copy code
testImplementation "junit:junit:$junit_version"
    androidTestImplementation "androidx.test.ext:junit:$ext_junit_version"
    androidTestImplementation "androidx.test.espresso:espresso-core:$expresso_core_version"
    // Test rules and transitive dependencies:
    androidTestImplementation("androidx.compose.ui:ui-test-junit4:$compose_version")
    // Needed for createComposeRule, but not createAndroidComposeRule:
    debugImplementation("androidx.compose.ui:ui-test-manifest:$compose_version")
This is what I have and my tests are working fine… (better than I expect)
b
yep … although because I’m using Hilt, I have a custom AndroidJUnitRunner, like this:
Copy code
class HiltTestRunner: AndroidJUnitRunner() {
    override fun newApplication(cl: ClassLoader?, name: String?, context: Context?): Application {
        return super.newApplication(cl, HiltTestCdiApplication_Application::class.java.name, context)
    }
}
And thus my runner is setup accordingly:
Copy code
testInstrumentationRunner "com.myapp.android.HiltTestRunner"
But, I don’t think it’s a Hilt problem, because I removed all of the Hilt Setup, went back to the
AndroidJUnitRunner
, and created a basic test like this:
Copy code
@RunWith(AndroidJUnit4::class)
class ComposeDemoTest {
    /**
     * A ComposeContentTestRule for testing Composable functions.
     */
    @get:Rule()
    var composeTestRule = createComposeRule()

    @OptIn(ExperimentalComposeUiApi::class, ExperimentalMaterialApi::class)
    @Before
    fun initializeUI() {
        composeTestRule.setContent {
            Text("This is a test")
        }
    }

    @Test
    fun demoTest() {
        composeTestRule.onRoot().printToLog("Tree")
    }
}
And it STILL fails with the missing BootstratpActivity …