Issue#67 Extract JUnit5 MainDispatcher
This commit is contained in:
parent
3b96a5d9eb
commit
7e019973e8
11 changed files with 112 additions and 57 deletions
|
|
@ -2,7 +2,7 @@ package org.fnives.test.showcase.di
|
|||
|
||||
import android.content.Context
|
||||
import org.fnives.test.showcase.model.network.BaseUrl
|
||||
import org.fnives.test.showcase.testutils.TestMainDispatcher
|
||||
import org.fnives.test.showcase.android.testutil.StandardTestMainDispatcher
|
||||
import org.fnives.test.showcase.ui.auth.AuthViewModel
|
||||
import org.fnives.test.showcase.ui.home.MainViewModel
|
||||
import org.fnives.test.showcase.ui.splash.SplashViewModel
|
||||
|
|
@ -20,7 +20,7 @@ import org.mockito.kotlin.doReturn
|
|||
import org.mockito.kotlin.mock
|
||||
import org.mockito.kotlin.whenever
|
||||
|
||||
@ExtendWith(TestMainDispatcher::class)
|
||||
@ExtendWith(StandardTestMainDispatcher::class)
|
||||
class DITest : KoinTest {
|
||||
|
||||
private val authViewModel by inject<AuthViewModel>()
|
||||
|
|
|
|||
|
|
@ -1,39 +0,0 @@
|
|||
package org.fnives.test.showcase.testutils
|
||||
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.ExperimentalCoroutinesApi
|
||||
import kotlinx.coroutines.test.StandardTestDispatcher
|
||||
import kotlinx.coroutines.test.TestDispatcher
|
||||
import kotlinx.coroutines.test.resetMain
|
||||
import kotlinx.coroutines.test.setMain
|
||||
import org.fnives.test.showcase.testutils.TestMainDispatcher.Companion.testDispatcher
|
||||
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]
|
||||
*
|
||||
* One can access the test dispatcher via [testDispatcher] static getter.
|
||||
*/
|
||||
@OptIn(ExperimentalCoroutinesApi::class)
|
||||
class TestMainDispatcher : BeforeEachCallback, AfterEachCallback {
|
||||
|
||||
override fun beforeEach(context: ExtensionContext?) {
|
||||
val testDispatcher = StandardTestDispatcher()
|
||||
privateTestDispatcher = testDispatcher
|
||||
Dispatchers.setMain(testDispatcher)
|
||||
}
|
||||
|
||||
override fun afterEach(context: ExtensionContext?) {
|
||||
Dispatchers.resetMain()
|
||||
privateTestDispatcher = null
|
||||
}
|
||||
|
||||
companion object {
|
||||
private var privateTestDispatcher: TestDispatcher? = null
|
||||
val testDispatcher: TestDispatcher
|
||||
get() = privateTestDispatcher
|
||||
?: throw IllegalStateException("TestMainDispatcher is in afterEach State")
|
||||
}
|
||||
}
|
||||
|
|
@ -8,7 +8,7 @@ import org.fnives.test.showcase.core.login.LoginUseCase
|
|||
import org.fnives.test.showcase.model.auth.LoginCredentials
|
||||
import org.fnives.test.showcase.model.auth.LoginStatus
|
||||
import org.fnives.test.showcase.model.shared.Answer
|
||||
import org.fnives.test.showcase.testutils.TestMainDispatcher
|
||||
import org.fnives.test.showcase.android.testutil.StandardTestMainDispatcher
|
||||
import org.fnives.test.showcase.ui.shared.Event
|
||||
import org.junit.jupiter.api.BeforeEach
|
||||
import org.junit.jupiter.api.DisplayName
|
||||
|
|
@ -27,13 +27,13 @@ import org.mockito.kotlin.whenever
|
|||
import java.util.stream.Stream
|
||||
|
||||
@Suppress("TestFunctionName")
|
||||
@ExtendWith(InstantExecutorExtension::class, TestMainDispatcher::class)
|
||||
@ExtendWith(InstantExecutorExtension::class, StandardTestMainDispatcher::class)
|
||||
@OptIn(ExperimentalCoroutinesApi::class)
|
||||
internal class AuthViewModelTest {
|
||||
|
||||
private lateinit var sut: AuthViewModel
|
||||
private lateinit var mockLoginUseCase: LoginUseCase
|
||||
private val testScheduler get() = TestMainDispatcher.testDispatcher.scheduler
|
||||
private val testScheduler get() = StandardTestMainDispatcher.testDispatcher.scheduler
|
||||
|
||||
@BeforeEach
|
||||
fun setUp() {
|
||||
|
|
|
|||
|
|
@ -3,20 +3,20 @@ package org.fnives.test.showcase.ui.auth
|
|||
import kotlinx.coroutines.ExperimentalCoroutinesApi
|
||||
import org.fnives.test.showcase.android.testutil.InstantExecutorExtension
|
||||
import org.fnives.test.showcase.core.login.LoginUseCase
|
||||
import org.fnives.test.showcase.testutils.TestMainDispatcher
|
||||
import org.fnives.test.showcase.android.testutil.StandardTestMainDispatcher
|
||||
import org.junit.jupiter.api.BeforeEach
|
||||
import org.junit.jupiter.api.DisplayName
|
||||
import org.junit.jupiter.api.Test
|
||||
import org.junit.jupiter.api.extension.ExtendWith
|
||||
import org.mockito.kotlin.mock
|
||||
|
||||
@ExtendWith(InstantExecutorExtension::class, TestMainDispatcher::class)
|
||||
@ExtendWith(InstantExecutorExtension::class, StandardTestMainDispatcher::class)
|
||||
@OptIn(ExperimentalCoroutinesApi::class)
|
||||
class CodeKataAuthViewModel {
|
||||
|
||||
private lateinit var sut: AuthViewModel
|
||||
private lateinit var mockLoginUseCase: LoginUseCase
|
||||
private val testScheduler get() = TestMainDispatcher.testDispatcher.scheduler
|
||||
private val testScheduler get() = StandardTestMainDispatcher.testDispatcher.scheduler
|
||||
|
||||
@BeforeEach
|
||||
fun setUp() {
|
||||
|
|
|
|||
|
|
@ -15,7 +15,7 @@ import org.fnives.test.showcase.model.content.ContentId
|
|||
import org.fnives.test.showcase.model.content.FavouriteContent
|
||||
import org.fnives.test.showcase.model.content.ImageUrl
|
||||
import org.fnives.test.showcase.model.shared.Resource
|
||||
import org.fnives.test.showcase.testutils.TestMainDispatcher
|
||||
import org.fnives.test.showcase.android.testutil.StandardTestMainDispatcher
|
||||
import org.junit.jupiter.api.BeforeEach
|
||||
import org.junit.jupiter.api.DisplayName
|
||||
import org.junit.jupiter.api.Test
|
||||
|
|
@ -29,7 +29,7 @@ import org.mockito.kotlin.verifyNoMoreInteractions
|
|||
import org.mockito.kotlin.whenever
|
||||
|
||||
@Suppress("TestFunctionName")
|
||||
@ExtendWith(InstantExecutorExtension::class, TestMainDispatcher::class)
|
||||
@ExtendWith(InstantExecutorExtension::class, StandardTestMainDispatcher::class)
|
||||
@OptIn(ExperimentalCoroutinesApi::class)
|
||||
internal class MainViewModelTest {
|
||||
|
||||
|
|
@ -39,7 +39,7 @@ internal class MainViewModelTest {
|
|||
private lateinit var mockFetchContentUseCase: FetchContentUseCase
|
||||
private lateinit var mockAddContentToFavouriteUseCase: AddContentToFavouriteUseCase
|
||||
private lateinit var mockRemoveContentFromFavouritesUseCase: RemoveContentFromFavouritesUseCase
|
||||
private val testScheduler get() = TestMainDispatcher.testDispatcher.scheduler
|
||||
private val testScheduler get() = StandardTestMainDispatcher.testDispatcher.scheduler
|
||||
|
||||
@BeforeEach
|
||||
fun setUp() {
|
||||
|
|
|
|||
|
|
@ -4,7 +4,7 @@ import com.jraska.livedata.test
|
|||
import kotlinx.coroutines.ExperimentalCoroutinesApi
|
||||
import org.fnives.test.showcase.android.testutil.InstantExecutorExtension
|
||||
import org.fnives.test.showcase.core.login.IsUserLoggedInUseCase
|
||||
import org.fnives.test.showcase.testutils.TestMainDispatcher
|
||||
import org.fnives.test.showcase.android.testutil.StandardTestMainDispatcher
|
||||
import org.fnives.test.showcase.ui.shared.Event
|
||||
import org.junit.jupiter.api.BeforeEach
|
||||
import org.junit.jupiter.api.DisplayName
|
||||
|
|
@ -14,13 +14,13 @@ import org.mockito.kotlin.doReturn
|
|||
import org.mockito.kotlin.mock
|
||||
import org.mockito.kotlin.whenever
|
||||
|
||||
@ExtendWith(InstantExecutorExtension::class, TestMainDispatcher::class)
|
||||
@ExtendWith(InstantExecutorExtension::class, StandardTestMainDispatcher::class)
|
||||
@OptIn(ExperimentalCoroutinesApi::class)
|
||||
internal class SplashViewModelTest {
|
||||
|
||||
private lateinit var mockIsUserLoggedInUseCase: IsUserLoggedInUseCase
|
||||
private lateinit var sut: SplashViewModel
|
||||
private val testScheduler get() = TestMainDispatcher.testDispatcher.scheduler
|
||||
private val testScheduler get() = StandardTestMainDispatcher.testDispatcher.scheduler
|
||||
|
||||
@BeforeEach
|
||||
fun setUp() {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue