Skip to content

Instantly share code, notes, and snippets.

@shredderskelton
Created February 20, 2021 14:26
Show Gist options
  • Select an option

  • Save shredderskelton/1b193622cc7f7b07ba80568a5c627f22 to your computer and use it in GitHub Desktop.

Select an option

Save shredderskelton/1b193622cc7f7b07ba80568a5c627f22 to your computer and use it in GitHub Desktop.
TestCoroutineRule
class TestCoroutineRule(
val testDispatcher: TestCoroutineDispatcher = TestCoroutineDispatcher()
) : TestWatcher() {
override fun starting(description: Description?) {
super.starting(description)
Dispatchers.setMain(testDispatcher)
}
override fun finished(description: Description?) {
super.finished(description)
Dispatchers.resetMain()
testDispatcher.cleanupTestCoroutines()
}
}
// Usage:
class MyTestClass{
@get:Rule
val testCoroutineRule = TestCoroutineRule()
...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment