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

mockConstruction doesn't work with coroutines #483

Open
catsuperberg opened this issue Jul 1, 2023 · 0 comments
Open

mockConstruction doesn't work with coroutines #483

catsuperberg opened this issue Jul 1, 2023 · 0 comments

Comments

@catsuperberg
Copy link

I'm in a situation where I'd rather test it how it is (object being instantiated inside a class) instead of refactoring object creation to be injected and found that i could use mockConstruction for this. The problem i have is i create this object inside a coroutine and as soon as you are in a coroutine you can't get the mock constructor, it just uses the default one.
Documentation reads

it is possible to generate mocks on constructor invocations within the current thread and a user-defined scope

I tried to get thread IDs from outside and inside coroutine while running test and it seems to be the same.

Here is simple example where only instance outside the coroutine is created as a mock and code inside the mock tries to create the object using class constructor:

    @Test
    fun testMockConstructor() = runTest {
        val board = mockRetriever.getBoard(any(), any(), any())
        mockConstruction(TaskBoard::class.java).use { mock ->
            val outsideCoroutine = TaskBoard(board)
            println(outsideCoroutine.toString())
            this.launch {
                val insideCoroutine = TaskBoard(board)
                println(insideCoroutine.toString())
            }
            assertEquals(mock.constructed().size, 2)
        }
     }
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