1. Android Test Orchestrator.
Android Test Orchestrator resolves your issue with IdlingResources
test fails.
dependencies {
androidTestUtil 'androidx.test:orchestrator:1.3.0'
}
Configure your build.gradle
:
android {
defaultConfig {
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
testInstrumentationRunnerArguments clearPackageData: 'true'
}
testOptions {
execution 'ANDROIDX_TEST_ORCHESTRATOR'
}
}
Then run your tests:
./gradlew connectedCheck
2. Refactor the code
The issue IdlingResources
cause tests to fail. Refactoring solves such problems.
- Use
Thread.sleep()
instead ofIdlingResources
, I personally don’t recommend in High Level Programming but it is fixes the problem. - Modify the code with
Callbacks
&Hooks
. - Simplify the code without many and complex dependencies.
3. Use Deprecated Methods.
registerIdlingResources
and unregisterIdlingResources
Deprecated Methods are working in this context. Note that these methods need updates in the future versions of Espresso
.
4. Use Third-party Testing Library.
I would give a try also with Robolectric
or Appium
. They can give good guidance to handle IdlingResources
, along with performance benefits around testing environments.
5. Upgrade to latest Espresso
library:
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
6. Define IdlingResource
:
private val idlingResource = CountingIdlingResource("idlingResource")
7. Register IdlingResource
:
IdlingRegistry.getInstance().register(idlingResource)
8. Unregister IdlingResource
:
IdlingRegistry.getInstance().unregister(idlingResource)
9. Synchronize the Looper
:
Handler(Looper ... ()).Delayed({
// Your code
}, ...)