-
Notifications
You must be signed in to change notification settings - Fork 206
Open
Description
When stubbing a suspendable builder method to return the mock itselfs, this works fine for sync methods, but fails for suspend functions.
The failure can be demonstrated by this simple test:
package test
import com.nhaarman.expect.expect
import kotlinx.coroutines.test.runTest
import org.junit.Test
import org.mockito.Mockito
import org.mockito.kotlin.mock
interface Builder {
suspend fun builderMethod(): Builder
}
class SimpleTest {
@Test
fun `should mock builder method returning mock itself via defaultAnswer`() = runTest {
/* Given */
val mock = mock<Builder>(defaultAnswer = Mockito.RETURNS_SELF)
/* When */
val result = mock.builderMethod()
/* Then */
expect(result).toBeTheSameAs(mock)
}
@Test
fun `should mock builder method returning mock itself via answer`() = runTest {
/* Given */
val mock = mock<Builder> {
onBlocking { builderMethod() } doAnswer Mockito.RETURNS_SELF
}
/* When */
val result = mock.builderMethod()
/* Then */
expect(result).toBeTheSameAs(mock)
}
}
Metadata
Metadata
Assignees
Labels
No labels