Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Test error:MyActivity has already set content. If you have populated the Activity with a ComposeView, make sure to call setContent on that ComposeView

When running Robolectric unit tests with the latest version of Compose 1.2.0, then the tests using createAndroidComposeRule fail with the following error:

MyActivity has already set content. If you have populated the Activity with a ComposeView, make sure to call setContent on that ComposeView instead of on the test rule; and make sure that that call to setContent {} is done after the ComposeTestRule has run

Code from one of the failing tests:

composeTestRule.setContent {
    Column {
        Text(textTitle)
        DemoScopedInjectedViewModelComposable()
    }
}
like image 986
Sebas LG Avatar asked Dec 05 '25 09:12

Sebas LG


1 Answers

Looking carefully, the error message helps a lot, even though it's talking about a ComposeView instead of an Activity. But according to it:

composeTestRule.setContent { ... }

should be changed to:

composeTestRule.activity.setContent { ... }

and the tests should run without this error occurring anymore.

Alternatively, make sure that your activity is extending ComponentActivity thus composeTestRule.setContent { ... } should just work.

like image 138
coroutineDispatcher Avatar answered Dec 07 '25 21:12

coroutineDispatcher



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!