MainDispatcherExtension
class MainDispatcherExtension(val dispatcher: TestDispatcher = StandardTestDispatcher()) : BeforeEachCallback, AfterEachCallback(source)
A JUnit 5 extension that replaces Dispatchers.Main with dispatcher for each test and always restores it afterward.
Register it as a field so the test can share its dispatcher with runTest:
@JvmField @RegisterExtension val mainDispatcher = MainDispatcherExtension()
@Test fun loads() = runTest(mainDispatcher.dispatcher) { ... }Content copied to clipboard
This artifact declares JUnit Jupiter as compileOnly; projects using the extension already depend on it.