Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mocking calls to kotlin suspend functions with matchers #261

Open
josephlbarnett opened this issue Sep 11, 2020 · 0 comments
Open

Mocking calls to kotlin suspend functions with matchers #261

josephlbarnett opened this issue Sep 11, 2020 · 0 comments

Comments

@josephlbarnett
Copy link

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?)?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant