Issue#67 Extract JUnit5 MainDispatcher
This commit is contained in:
parent
3b96a5d9eb
commit
7e019973e8
11 changed files with 112 additions and 57 deletions
|
|
@ -32,4 +32,6 @@ android {
|
|||
dependencies {
|
||||
implementation "org.junit.jupiter:junit-jupiter-engine:$junit5_version"
|
||||
implementation "androidx.arch.core:core-runtime:$arch_core_version"
|
||||
|
||||
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-test:$coroutines_version"
|
||||
}
|
||||
|
|
@ -0,0 +1,30 @@
|
|||
package org.fnives.test.showcase.android.testutil
|
||||
|
||||
import kotlinx.coroutines.ExperimentalCoroutinesApi
|
||||
import kotlinx.coroutines.test.StandardTestDispatcher
|
||||
import kotlinx.coroutines.test.TestDispatcher
|
||||
import org.fnives.test.showcase.android.testutil.StandardTestMainDispatcher.Companion.testDispatcher
|
||||
|
||||
/**
|
||||
* Custom Junit5 Extension which replaces the main dispatcher with a Standard [TestDispatcher]
|
||||
*
|
||||
* One can access the test dispatcher via [testDispatcher] static getter.
|
||||
*/
|
||||
@OptIn(ExperimentalCoroutinesApi::class)
|
||||
class StandardTestMainDispatcher : TestMainDispatcher() {
|
||||
|
||||
override var createdTestDispatcher: TestDispatcher?
|
||||
get() = privateTestDispatcher
|
||||
set(value) {
|
||||
privateTestDispatcher = value
|
||||
}
|
||||
|
||||
override fun createDispatcher(): TestDispatcher = StandardTestDispatcher()
|
||||
|
||||
companion object {
|
||||
private var privateTestDispatcher: TestDispatcher? = null
|
||||
val testDispatcher: TestDispatcher
|
||||
get() = privateTestDispatcher
|
||||
?: throw IllegalStateException("StandardTestMainDispatcher is in afterEach State")
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,32 @@
|
|||
package org.fnives.test.showcase.android.testutil
|
||||
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.ExperimentalCoroutinesApi
|
||||
import kotlinx.coroutines.test.TestDispatcher
|
||||
import kotlinx.coroutines.test.resetMain
|
||||
import kotlinx.coroutines.test.setMain
|
||||
import org.junit.jupiter.api.extension.AfterEachCallback
|
||||
import org.junit.jupiter.api.extension.BeforeEachCallback
|
||||
import org.junit.jupiter.api.extension.ExtensionContext
|
||||
|
||||
/**
|
||||
* Custom Junit5 Extension which replaces the main dispatcher with a [TestDispatcher]
|
||||
*/
|
||||
@OptIn(ExperimentalCoroutinesApi::class)
|
||||
abstract class TestMainDispatcher : BeforeEachCallback, AfterEachCallback {
|
||||
|
||||
protected abstract var createdTestDispatcher: TestDispatcher?
|
||||
|
||||
abstract fun createDispatcher() : TestDispatcher
|
||||
|
||||
final override fun beforeEach(context: ExtensionContext?) {
|
||||
val testDispatcher = createDispatcher()
|
||||
createdTestDispatcher = testDispatcher
|
||||
Dispatchers.setMain(testDispatcher)
|
||||
}
|
||||
|
||||
final override fun afterEach(context: ExtensionContext?) {
|
||||
Dispatchers.resetMain()
|
||||
createdTestDispatcher = null
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,30 @@
|
|||
package org.fnives.test.showcase.android.testutil
|
||||
|
||||
import kotlinx.coroutines.ExperimentalCoroutinesApi
|
||||
import kotlinx.coroutines.test.TestDispatcher
|
||||
import kotlinx.coroutines.test.UnconfinedTestDispatcher
|
||||
import org.fnives.test.showcase.android.testutil.UnconfinedTestMainDispatcher.Companion.testDispatcher
|
||||
|
||||
/**
|
||||
* Custom Junit5 Extension which replaces the main dispatcher with a Unconfined [TestDispatcher]
|
||||
*
|
||||
* One can access the test dispatcher via [testDispatcher] static getter.
|
||||
*/
|
||||
@OptIn(ExperimentalCoroutinesApi::class)
|
||||
class UnconfinedTestMainDispatcher : TestMainDispatcher() {
|
||||
|
||||
override var createdTestDispatcher: TestDispatcher?
|
||||
get() = privateTestDispatcher
|
||||
set(value) {
|
||||
privateTestDispatcher = value
|
||||
}
|
||||
|
||||
override fun createDispatcher(): TestDispatcher = UnconfinedTestDispatcher()
|
||||
|
||||
companion object {
|
||||
private var privateTestDispatcher: TestDispatcher? = null
|
||||
val testDispatcher: TestDispatcher
|
||||
get() = privateTestDispatcher
|
||||
?: throw IllegalStateException("StandardTestMainDispatcher is in afterEach State")
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue