Is there any configuration we have to do when usin...
# coroutines
j
Is there any configuration we have to do when using
mockito
and
suspend fun
? I remember when I did unit testing with
rxJava
I had to create a
Rule
and things like that, do I have to do something special to make it work? Note: I'm using MVP not MVVM I don't know if it's a good point
t
You can mock and verify suspending functions with Mockito just like you'd do with regular functions. Note that it is only supported as of Mockito 2.23.
j
Where I can find an example? It's my first time mocking suspend functions and coroutines, do you know any?
t
Can't find blog posts on the subject, but I wrote some tests using Mockito myself : https://github.com/thibseisel/android-odeon/blob/746beead1ff3bda710e7862bd0d05fbbc74c3e29/media/src/test/java/fr/nihilus/music/media/actions/DeleteActionTest.kt#L126 From that test, you can see how to - create a mock for a dependency of the object under test (
@Mock
annotation with
MockitoRule
), - stub a suspending method of that mock, so that it returns an expected result (
given(...).willReturn(...)
), - verify that a suspend method of that mock has been called (
verify(...)
).
j
And what's that? :
private val dispatcher = TestCoroutineDispatcher()
t
That's a CoroutineDispatcher designed for testing. Most of the time, you only need
runBlockingTest
. All of those are available in
kotlinx-coroutines-test
: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-test/