mockk: Tests failing after coroutines update
Prerequisites
- I am running the latest version
- I checked the documentation and found no answer
- I checked to make sure that this issue has not already been filed
Expected Behavior
The tests should pass and irrelevant Mockk exceptions should be ignored as it used to be (?)
Current Behavior
Multiple tests failing
Failure Information (for bugs)
The errors I am seeing in the console are a UncaughtExceptionsBeforeTest
followed by a
Suppressed: io.mockk.MockKException: no answer found for MyMockedClass
[...]
Caused by: io.mockk.MockKException: no answer found for MyMockedClass
Steps to Reproduce
- Have a project with some tests that use Mockk
- Have coroutines 1.6.4 and verify tests are passing
- Update coroutines to 1.7.1 and verify tests are failing
Note: what I also noticed is that usually is only one test failing, and if I try to delete that one just to make sure that’s the problem, another one fails. And so on. I am pretty sure this is related to this coroutines change of behaviour in the new version, and this one as well https://github.com/Kotlin/kotlinx.coroutines/issues/3738
Note 2: this happens only when running all the tests together, or if multiple tests are run within a single file. This never happens if the tests are run singularly
Context
- MockK version: 1.13.5
- OS: Mac Ventura 13.4 - Android Studio
- Kotlin version: 1.8.21
- JDK version: 17
- JUnit version: jUnit5 - 5.9.2
- Type of test: unit test
Failure Logs
Stack trace
Suppressed: io.mockk.MockKException: no answer found for MyMockedClass
[...]
Caused by: io.mockk.MockKException: no answer found for MyMockedClass
About this issue
- Original URL
- State: closed
- Created a year ago
- Reactions: 52
- Comments: 17 (5 by maintainers)
We’re also affected by this. Running a single test works, but running multiple tests (say multiple test classes) throws
UncaughtExceptionsBeforeTest
.We’re seeing the same thing: multiple failures with previously-working tests after updating to Kotlin 1.8.22, which updates coroutines from 1.6.4 to 1.7.1. The failures are
kotlinx.coroutines.test.UncaughtExceptionsBeforeTest
followed bySuppressed: io.mockk.MockKException: no answer found for: ...
, as noted above.For now, the workaround we use is to use “runBlocking” instead of “runTest”.
What if the exceptions are thrown by a third party library, cannot be fixed and are not blocking ? How are we supposed to fix this please ?
Seeing the same issue. Running single test class works, but running a suite or running all of it in a module on CI breaks it.
Great discovery! Mystery solved.
By the way it is more convenient to observe stack traces in the generated HTML report than in the terminal. It seems like they can get truncated in the terminal.
EDIT: So it looks like
runTest
is checking for previous exceptions before executing the provided block and this could explain why a test won’t fail when run individually, but it can indeed fail when multiple tests are run and at least one of them threw a suppressed exception. Furthermore this suggest that the sameTestScope
is used for a single test run, regardless of the number of tests involved.Thank you for sharing this! So if this is the case I think adding this to the build file might help finding the culprit:
Interesting! How does this explain the fact that the tests are successful when run individually?