https://kotlinlang.org logo
#compose
Title
# compose
b

Bradleycorn

02/20/2022, 11:20 PM
How can I get a string resource in an instrumented test without using
createAndroidComposeRule()
? I do not want or need to load my app’s main activity. That would load all of my application content, and this is a Unit Test for a single composable. But I need to get a string resource to test with.
For now I’m doing this. It seems to work. 🤷
Copy code
val context = ApplicationProvider.getApplicationContext<Application>()
val contentDescription = context.getString(R.string.my_button_description)
composeRule.onNodeWithContentDescription(contentDescription).assertIsDisplayed()
a

allan.conda

02/21/2022, 1:14 AM
Yeah, that one. Or get the context from setContent using a var
a

Albert Chang

02/21/2022, 1:57 AM
You can use
createAndroidComposeRule<ComponentActivity>()
. This is what
createComposeRule()
actually uses internally.
b

Bradleycorn

02/21/2022, 1:48 PM
Thanks @Albert Chang
4 Views