Robolectric tests are supposedly supported now by Jetpack Compose, since beta09. But I attempted to implement a very simple test (the test for the feature in AOSP, see runClickScenario()
) but it does not work for me. Error I get is:
java.util.concurrent.ExecutionException: java.lang.RuntimeException: java.lang.IllegalAccessException: class androidx.test.espresso.base.ThreadPoolExecutorExtractor$2 cannot access a member of class androidx.loader.content.ModernAsyncTask with modifiers "public static final"
java.lang.RuntimeException: java.util.concurrent.ExecutionException: java.lang.RuntimeException: java.lang.IllegalAccessException: class androidx.test.espresso.base.ThreadPoolExecutorExtractor$2 cannot access a member of class androidx.loader.content.ModernAsyncTask with modifiers "public static final"
at androidx.test.espresso.Espresso.onIdle(Espresso.java:15)
at androidx.test.espresso.Espresso.onIdle(Espresso.java:1)
at androidx.compose.ui.test.junit4.android.EspressoLink_androidKt.runEspressoOnIdle(EspressoLink.android.kt:91)
at androidx.compose.ui.test.junit4.RobolectricIdlingStrategy$runUntilIdle$1.invoke(RobolectricIdlingStrategy.android.kt:69)
at androidx.compose.ui.test.junit4.RobolectricIdlingStrategy$runUntilIdle$1.invoke(RobolectricIdlingStrategy.android.kt:51)
at androidx.compose.ui.test.junit4.AndroidSynchronization_androidKt.runOnUiThread(AndroidSynchronization.android.kt:33)
at androidx.compose.ui.test.junit4.RobolectricIdlingStrategy.runUntilIdle(RobolectricIdlingStrategy.android.kt:51)
at androidx.compose.ui.test.junit4.AndroidComposeTestRule.waitForIdle(AndroidComposeTestRule.android.kt:293)
at androidx.compose.ui.test.junit4.AndroidComposeTestRule.waitForIdle(AndroidComposeTestRule.android.kt:286)
at androidx.compose.ui.test.junit4.AndroidComposeTestRule.setContent(AndroidComposeTestRule.android.kt:281)
(next line in stack trace is my test code.)
I am using:
It did not matter whether I ran the test in AS or on command line.
Robolectric is intended to be fully compatible with Android's official testing libraries since version 4.0. As such we encourage you to try these new APIs and provide feedback. At some point the Robolectric equivalents will be deprecated and removed.
Today, we're releasing version 1.2 of Jetpack Compose, Android's modern, native UI toolkit, continuing to build out our roadmap.
Espresso and UIAutomator can still test a Compose Layout — searching by text, resource, etc. However, they don't have access to Composables' semantics and can't fully test them. Therefore, it's recommended to use the Jetpack Compose testing library as it can access semantics and fully test Composables on the screen.
Introduction and setup In this codelab you'll learn about testing UIs created with Jetpack Compose. You will write your first tests while learning about testing in isolation, debugging tests, semantics trees and synchronization. Download Android Studio Arctic Fox on the beta channel.
The compose-codelabs repo contains starter code for all codelabs in the pathway. For this codelab, use the TestingCodelab project. Compose tests are instrumented tests. This means they require a device (physical device or emulator) to run on.
When a test is synchronized, your Compose app is advanced in time using a virtual clock. This means Compose tests don't run in real time, so they can pass as fast as possible. However, if you don't use the methods that synchronize your tests, no recomposition will occur and the UI will appear to be paused.
You can find them in the androidTest source set: This is the directory where you'll place the new tests. Feel free to take a look at the AnimatingCircleTests.kt file to learn what a Compose test looks like.
I didn't find this when searching before, but the problem is with Robolectric. Fortunately, there's a workaround.
See issue, comments with workaround for reference.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With