IdlingDispatcherProviderRule
A JUnit 4 TestRule which creates a new IdlingDispatcherProvider for each test, registering all IdlingDispatchers with IdlingRegistry before @Before
and unregistering them after @After
.
The rule takes an optional IdlingDispatcherProvider factory, in which case it only handles registration.
When doing Espresso testing, it's important that the same IdlingDispatchers are used throughout a codebase. For this reason, it's a good idea to use a dependency injection framework just as Dagger or Koin to provide CoroutineScopes.
If using the lifecycleScope
and viewModelScope
properties, be sure to use the versions from the dispatch-android-lifecycle
artifacts to make use of their settable factories.
Before the test:
IdlingDispatcherProvider.registerAllIdlingResources is called to register all dispatchers with IdlingRegistry.
After the test:
IdlingDispatcherProvider.unregisterAllIdlingResources is called to unregister all dispatchers with IdlingRegistry.
Requires JUnit 4.
dependencies {
testImplementation "junit:junit:4.12"
-- or --
testImplementation "org.junit.vintage:junit-vintage-engine:5.5.1"
}
Samples
import dispatch.android.espresso.IdlingDispatcherProvider
import dispatch.android.espresso.IdlingDispatcherProviderRule
import kotlinx.coroutines.runBlocking
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner
fun main() {
//sampleStart
@RunWith(RobolectricTestRunner::class)
class IdlingCoroutineScopeRuleSample {
// Retrieve the DispatcherProvider from a dependency graph,
// so that the same one is used throughout the codebase.
val customDispatcherProvider = testAppComponent.customDispatcherProvider
@JvmField
@Rule
val idlingRule = IdlingDispatcherProviderRule {
IdlingDispatcherProvider(customDispatcherProvider)
}
@Test
fun testThings() = runBlocking {
// Now any CoroutineScope which uses the DispatcherProvider
// in TestAppComponent will sync its "idle" state with Espresso
}
}
//sampleEnd
}
import dispatch.android.espresso.IdlingDispatcherProvider
import dispatch.android.espresso.IdlingDispatcherProviderRule
import dispatch.android.lifecycle.LifecycleScopeFactory
import dispatch.android.viewmodel.ViewModelScopeFactory
import dispatch.core.MainImmediateCoroutineScope
import kotlinx.coroutines.runBlocking
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner
fun main() {
//sampleStart
@RunWith(RobolectricTestRunner::class)
class IdlingCoroutineScopeRuleWithLifecycleSample {
// Retrieve the DispatcherProvider from a dependency graph,
// so that the same one is used throughout the codebase.
val customDispatcherProvider = testAppComponent.customDispatcherProvider
@JvmField
@Rule
val idlingRule = IdlingDispatcherProviderRule {
IdlingDispatcherProvider(customDispatcherProvider)
}
@Before
fun setUp() {
LifecycleScopeFactory.set { customDispatcherProvider }
ViewModelScopeFactory.set {
MainImmediateCoroutineScope(customDispatcherProvider)
}
// now all scopes use the same IdlingDispatcherProvider
}
@Test
fun testThings() = runBlocking {
// Now any CoroutineScope which uses the DispatcherProvider
// in TestAppComponent will sync its "idle" state with Espresso
}
}
//sampleEnd
}
See also
Constructors
Functions
Properties
The IdlingDispatcherProvider which is automatically registered with IdlingRegistry.