Skip to content

Conversation

@m-koops
Copy link
Contributor

@m-koops m-koops commented Jan 14, 2026

Fixes issue #570

Due to the code changes runBlocking is needed in every use of Mockito-kotlin, also for projects without coroutines.
That's why Mockito-kotlin should declare 'kotlinx-coroutines-core' as a transitive runtime dependency.

This PR is superseded by PR #575

@TimvdLippe
Copy link
Contributor

Two things:

  1. Please add a subproject regression suite based on 6.2.0 regression stubbing broken for non-coroutines project #570 (comment)
  2. I agree that we should keep this optional if possible. Can we update the code to handle the case where classes don't exist? We use that pattern in mockito-core as well: https://github.com/mockito/mockito/blob/d16fcfc274d7ca03a2b4bdc22dd7c3ec6dac8690/mockito-core/src/main/java/org/mockito/internal/junit/ExceptionFactory.java#L23

@m-koops
Copy link
Contributor Author

m-koops commented Jan 15, 2026 via email

@m-koops
Copy link
Contributor Author

m-koops commented Jan 15, 2026

  1. I agree that we should keep this optional if possible. Can we update the code to handle the case where classes don't exist? We use that pattern in mockito-core as well: https://github.com/mockito/mockito/blob/d16fcfc274d7ca03a2b4bdc22dd7c3ec6dac8690/mockito-core/src/main/java/org/mockito/internal/junit/ExceptionFactory.java#L23

This have looked in a solution like this myself. But the issue is that the argument of the whenever function is now marked as suspend function/lambda. One can pass either a regular lambda or a suspend function into this function. But once marked suspending, there is no way to unmark it as suspending. And the passed in function therefor can only be invoked in a coroutine scope. Hence runBlocking is needed.

It's a pity that the Kotlin compiler does not yet allow to specify 2 overloads of the whenever function, 1 for regular lambdas, the other for suspend lambdas. Once that option is provided by the compiler it is way easier to support the non-coroutine without the need for a dependency to kotlinx-coroutines-core.

Tomorrow morning I will dive further into the 2 regressions reported. And I will do my best to come with the least intrusive fixes to them.

@m-koops
Copy link
Contributor Author

m-koops commented Jan 16, 2026

Two things:

  1. Please add a subproject regression suite based on 6.2.0 regression stubbing broken for non-coroutines project #570 (comment)
  2. I agree that we should keep this optional if possible. Can we update the code to handle the case where classes don't exist? We use that pattern in mockito-core as well: https://github.com/mockito/mockito/blob/d16fcfc274d7ca03a2b4bdc22dd7c3ec6dac8690/mockito-core/src/main/java/org/mockito/internal/junit/ExceptionFactory.java#L23

Please see PR #575, that provides a fix without adding the new dependency.
Also this PR introduces a new module that tests stubbing a mock in absence of the kotlinx-coroutines-core library.

@m-koops m-koops reopened this Jan 16, 2026
@m-koops m-koops closed this Jan 16, 2026
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

Successfully merging this pull request may close these issues.

2 participants