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) { ... }

This artifact declares JUnit Jupiter as compileOnly; projects using the extension already depend on it.

Constructors

Link copied to clipboard
constructor(dispatcher: TestDispatcher = StandardTestDispatcher())

Properties

Link copied to clipboard
val dispatcher: TestDispatcher

The dispatcher installed as Dispatchers.Main.

Functions

Link copied to clipboard
open override fun afterEach(context: ExtensionContext)
Link copied to clipboard
open override fun beforeEach(context: ExtensionContext)