This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class RedditRepository(private val dispatcherProvider: DispatcherProvider) { | |
suspend fun getPost(): State.Post { | |
return withContext(dispatcherProvider.io) { | |
// ...imaginary operation that will take a while | |
// .. | |
State.Post("I hope this post gets me karma points") // return on completion | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@ExperimentalCoroutinesApi | |
class CoroutineTestRule( | |
val testDispatcher: TestCoroutineDispatcher = TestCoroutineDispatcher() | |
) : TestWatcher() { | |
val testDispatcherProvider = object : DispatcherProvider { | |
override val io: CoroutineDispatcher = testDispatcher | |
override val ui: CoroutineDispatcher = testDispatcher | |
override val default: CoroutineDispatcher = testDispatcher | |
override val unconfined: CoroutineDispatcher = testDispatcher |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
dependencies { | |
// .. other dependencies | |
// .. | |
// testing dependencies | |
testImplementation 'androidx.test.ext:junit:1.1.2-alpha03' | |
testImplementation 'org.mockito:mockito-core:3.0.0' | |
testImplementation 'com.nhaarman.mockitokotlin2:mockito-kotlin:2.1.0' | |
testImplementation 'org.mockito:mockito-inline:3.0.0' | |
testImplementation 'org.amshove.kluent:kluent:1.51' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@ExperimentalCoroutinesApi | |
class RedditRepositoryTest { | |
@get:Rule | |
val coroutineTestRule = CoroutineTestRule() | |
private lateinit var redditRepository: RedditRepository | |
@Before | |
fun setUp() { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Test | |
fun `getPost actually returns the post I am expecting`() { | |
// run the test on the testDispatcher provided by the coroutineTestRule | |
coroutineTestRule.testDispatcher.runBlockingTest { | |
// Given | |
val expectedPost: State.Post = State.Post("I hope this post gets me karma points") | |
// When | |
val actualPost: State.Post = redditRepository.getPost() | |
// Then | |
assertEquals(expectedPost, actualPost) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@ExperimentalCoroutinesApi | |
fun CoroutineTestRule.runBlockingTest(block: suspend TestCoroutineScope.() -> Unit) { | |
testDispatcher.runBlockingTest(block) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
interface DispatcherProvider { | |
val io: CoroutineDispatcher | |
val ui: CoroutineDispatcher | |
val default: CoroutineDispatcher | |
val unconfined: CoroutineDispatcher | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class LifeCycleTestOwner : LifecycleOwner { | |
private val registry = LifecycleRegistry(this) | |
override fun getLifecycle(): Lifecycle { | |
return registry | |
} | |
fun onCreate() { | |
registry.handleLifecycleEvent(Lifecycle.Event.ON_CREATE) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@ExperimentalCoroutinesApi | |
class ViewModelTest { | |
@get:Rule | |
var coroutinesTestRule = TestCoroutineDispatcherRule() | |
@get:Rule | |
var rule: TestRule = InstantTaskExecutorRule() | |
private val stateObserver: Observer<State> = mock() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class PostViewModel(private val redditRepository: RedditRepository) : ViewModel() { | |
val stateData = MutableLiveData<State>() | |
fun getRedditPost() { | |
viewModelScope.launch { | |
stateData.value = State.Loading | |
val post: State.Post = redditRepository.getPost() | |
stateData.value = post | |
} |