igor.wojda
08/30/2022, 1:52 PMclass ExampleUnitTest {
private val repository = mockk<AlbumRepository>()
private val cut = GetAlbumUseCase(repository)
@Test
fun `when execute return success`() {
// given
val albumName = "Thriller"
val artistName = "Michael Jackson"
val mbId = "123"
// when
val actual = runBlocking { cut.execute(albumName, artistName, mbId) }
// then
}
}
no answer found for: AlbumRepository(#1).getAlbumInfo(Thriller, Michael Jackson, 123, continuation {})
io.mockk.MockKException: no answer found for: AlbumRepository(#1).getAlbumInfo(Thriller, Michael Jackson, 123, continuation {})
at app//io.mockk.impl.stub.MockKStub.defaultAnswer(MockKStub.kt:93)
The answer can't be found (most likely due to continuation {}
param).
I have crafted a sample project to reproduce this. Steps:
1. Open project using Android Studio
2. Run ExampleUnitTest
tests
Can someone take a look? Is there any workaround?christophsturm
08/30/2022, 1:56 PMchristophsturm
08/30/2022, 2:03 PMigor.wojda
08/30/2022, 2:03 PMclass ExampleUnitTest {
private val repository = mockk<AlbumRepository>()
private val cut = GetAlbumUseCase(repository)
@Test
fun `when execute return success`() {
// given
val albumName = "Thriller"
val artistName = "Michael Jackson"
val mbId = "123"
coEvery { repository.getAlbumInfo(artistName, albumName, mbId) } answers { mockk() }
// when
val actual = runBlocking { cut.execute(albumName, artistName, mbId) }
// then
}
}
igor.wojda
08/30/2022, 2:04 PMinternal class GetAlbumUseCase(
private val albumRepository: AlbumRepository,
) {
sealed interface Result {
data class Success(val data: String) : Result
data class Error(val e: Throwable) : Result
}
suspend fun execute(
artistName: String,
albumName: String,
mbId: String?,
): Result {
val data = albumRepository.getAlbumInfo(artistName, albumName, mbId)
return Result.Success(data)
}
}
christophsturm
08/30/2022, 2:06 PM.returns(mockk())
igor.wojda
08/30/2022, 2:09 PMcoEvery { repository.getAlbumInfo(artistName, albumName, mbId) } returns "ssasa"
igor.wojda
08/30/2022, 2:11 PMprivate val repository = mockk<AlbumRepository>(relaxed = true)
but relaxing mock only because coroutines are used seems a bit offigor.wojda
08/30/2022, 2:18 PMclass ExampleUnitTest {
private val repository = mockk<AlbumRepository>(relaxed = true)
private val cut = GetAlbumUseCase(repository)
@Test
fun `when execute return success`() {
// given
val albumName = "Thriller"
val artistName = "Michael Jackson"
val mbId = "123"
coEvery { repository.getAlbumInfo(artistName, albumName, mbId) } returns "ssasa"
// when
val actual = runBlocking { cut.execute(albumName, artistName, mbId) }
// then
assertEquals(GetAlbumUseCase.Result.Success("ssasa"), actual)
}
}
Result
Expected :Success(data=ssasa)
Actual :Success(data=)
<Click to see difference>
Tested class (no changes)igor.wojda
08/30/2022, 2:19 PMchristophsturm
08/30/2022, 2:19 PMchristophsturm
08/30/2022, 2:27 PMNikunj Yadav
09/21/2022, 6:36 PMigor.wojda
09/21/2022, 6:40 PMigor.wojda
10/28/2022, 6:17 PM