Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Robolectric tests are supported for JetPack Compose now – but not working

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:

  • compose 1.0.0
  • kotlin 1.5.10
  • espresso 3.4.0
  • robolectric 4.6.1

It did not matter whether I ran the test in AS or on command line.

like image 741
David Pisoni Avatar asked Jul 29 '21 01:07

David Pisoni


People also ask

Is Robolectric deprecated?

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.

Is jetpack compose stable now?

Today, we're releasing version 1.2 of Jetpack Compose, Android's modern, native UI toolkit, continuing to build out our roadmap.

Does Espresso work with jetpack compose?

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.

What do you learn in the jetpack compose Codelab?

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.

What is the compose-codelabs Repo?

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.

What happens if I don't synchronize my tests in compose?

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.

Where can I find compose tests in androidtest?

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.


Video Answer


1 Answers

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.

like image 97
David Pisoni Avatar answered Oct 20 '22 10:10

David Pisoni