You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In Kotlin, when mocking objects with suspend member functions (ie coroutine methods), setting up test expectations for those suspend functions can be a bit cumbersome when they have matcher arguments:
interface TestSusp {
suspend fun testWithArg(s: String?): String
suspend fun testWithoutArg(): String
}
class TestSuspTest {
@Test
fun mockTest() = runBlocking {
val t = EasyMock.mock<TestSusp>(TestSusp::class.java)
EasyMock.expect(
t.testWithArg(
Pair(EasyMock.anyString(), EasyMock.anyObject<Continuation<*>>()).first
)
)
.andReturn("abc")
EasyMock.expect(t.testWithoutArg()).andReturn("def")
EasyMock.replay(t)
Assert.assertEquals(t.testWithArg("a"), "abc")
Assert.assertEquals(t.testWithoutArg(), "def")
EasyMock.verify(t)
}
}
Trying the naive EasyMock.expect(t.testWithArg(EasyMock.anyString())).andReturn("abc") results in: java.lang.IllegalStateException: 2 matchers expected, 1 recorded.It does work with value expectations:EasyMock.expect(t.testWithArg("a")).andReturn("abc"), and with no-arg functions for the same reason (I assume it"s matching a compiler inserted continuation value).
Not sure if there"s a way to implement some way to automatically include a Continuation matcher as the last argument for suspend functions (or if there"d only be a way to do so by adding potentially unwanted kotlin dependencies?)?
The text was updated successfully, but these errors were encountered:
In Kotlin, when mocking objects with
suspend
member functions (ie coroutine methods), setting up test expectations for those suspend functions can be a bit cumbersome when they have matcher arguments:Trying the naive
EasyMock.expect(t.testWithArg(EasyMock.anyString())).andReturn("abc") results in:
java.lang.IllegalStateException: 2 matchers expected, 1 recorded.It does work with value expectations:
EasyMock.expect(t.testWithArg("a")).andReturn("abc"), and with no-arg functions for the same reason (I assume it"s matching a compiler inserted continuation value).Not sure if there"s a way to implement some way to automatically include a Continuation matcher as the last argument for suspend functions (or if there"d only be a way to do so by adding potentially unwanted kotlin dependencies?)?
The text was updated successfully, but these errors were encountered: