IdlingDispatcherProviderRule

class IdlingDispatcherProviderRule(factory: () -> IdlingDispatcherProvider) : TestWatcher

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:

After the test:

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

org.junit.rules.TestRule
androidx.test.espresso.IdlingRegistry

Constructors

Link copied to clipboard
fun IdlingDispatcherProviderRule(factory: () -> IdlingDispatcherProvider)

Functions

Link copied to clipboard
open override fun apply(p0: Statement, p1: Description): Statement
Link copied to clipboard
open fun failed(p0: Throwable, p1: Description)
Link copied to clipboard
open fun skipped(p0: AssumptionViolatedException, p1: Description)
open fun skipped(p0: AssumptionViolatedException, p1: Description)
Link copied to clipboard
open fun succeeded(p0: Description)

Properties

Link copied to clipboard
lateinit var dispatcherProvider: IdlingDispatcherProvider

The IdlingDispatcherProvider which is automatically registered with IdlingRegistry.

Sources

Link copied to clipboard