MockKException: Class cast exception happened while mocking generic class
See original GitHub issueExpected Behavior
mock class
Current Behavior
every
block raise io.mockk.MockKException: Class cast exception happened.
Failure Information (for bugs)
Steps to Reproduce
Preparing
$ git clone https://github.com/ganadist/VersionCodeDemo test -b mockk_cast_exception_robolectric_4.4
$ cd test
On java 8, it has no problem
$ /usr/lib/jvm/java-8-openjdk/bin/java -version
openjdk version "1.8.0_265"
OpenJDK Runtime Environment (build 1.8.0_265-b01)
OpenJDK 64-Bit Server VM (build 25.265-b01, mixed mode)
$ git checkout origin/mockk_cast_exception_robolectric_4.4~2
### pass with java8 + robolectric 4.3.1 + mockk 1.10.0
$ ./gradlew clean
$ env JAVA_HOME=/usr/lib/jvm/java-8-openjdk ./gradlew :app:testDevelopDebugUnitTest --no-build-cache
$ git checkout origin/mockk_cast_exception_robolectric_4.4~1
### pass with java8 + robolectric 4.4 + mockk 1.10.0
$ ./gradlew clean
$ env JAVA_HOME=/usr/lib/jvm/java-8-openjdk ./gradlew :app:testDevelopDebugUnitTest --no-build-cache
$ git checkout origin/mockk_cast_exception_robolectric_4.4
### pass with java8 + robolectric 4.4 + mockk 1.10.1-SNAPSHOT
$ ./gradlew clean
$ env JAVA_HOME=/usr/lib/jvm/java-8-openjdk ./gradlew :app:testDevelopDebugUnitTest --no-build-cache
On java 11 + Robolectric 4.4, mockk raise io.mockk.MockKException: Class cast exception happened.
$ /usr/lib/jvm/java-11-openjdk/bin/java -version
openjdk version "11.0.8" 2020-07-14
OpenJDK Runtime Environment (build 11.0.8+10)
OpenJDK 64-Bit Server VM (build 11.0.8+10, mixed mode)
$ git checkout origin/mockk_cast_exception_robolectric_4.4~2
### pass with java11 + robolectric 4.3.1 + mockk 1.10.0
$ ./gradlew clean
$ env JAVA_HOME=/usr/lib/jvm/java-11-openjdk ./gradlew :app:testDevelopDebugUnitTest --no-build-cache
$ git checkout origin/mockk_cast_exception_robolectric_4.4~1
### fail with java11 + robolectric 4.4 + mockk 1.10.0
$ ./gradlew clean
$ env JAVA_HOME=/usr/lib/jvm/java-11-openjdk ./gradlew :app:testDevelopDebugUnitTest --no-build-cache
$ git checkout origin/mockk_cast_exception_robolectric_4.4
### fail with java11 + robolectric 4.4 + mockk 1.10.1-SNAPSHOT
$ ./gradlew clean
$ env JAVA_HOME=/usr/lib/jvm/java-11-openjdk ./gradlew :app:testDevelopDebugUnitTest --no-build-cache
Context
Please provide any relevant information about your setup. This is important in case the issue is not reproducible except for under certain conditions.
- MockK version: 1.10.0, 1.10.1-SNAPSHOT (https://github.com/mockk/mockk/commit/10f17de9645f71245e8f3e11e25f9424ab911f0d)
- OS: Linux
- Kotlin version: 1.3.72
- JDK version: OpenJDK 11 (build 11.0.8+10)
- JUnit version: 4.12
- Type of test: unit test OR android instrumented test : Unittest with robolectric 4.4
It seems similar with #321, but this issue is not about chained call.
Failure Logs
https://scans.gradle.com/s/5e26rzx5n36zs/console-log#L38
Stack trace
Minimal reproducible code (the gist of this issue)
Issue Analytics
- State:
- Created 3 years ago
- Reactions:8
- Comments:15
Top Results From Across the Web
Mockk - ClassCastException when mocking final class that ...
The problem here is that the template argument type is erased and not possible to recover. The only solution is to specify mock...
Read more >ClassCastException when returning a value class instance ...
ClassCastException when returning a value class instance from a mocked service. What steps will reproduce the issue? 1. Create a project with Kotlin...
Read more >Mocking generic interfaces - Google Groups
I want the mock objet to have the generic type ChildAction. I would like to have something like this, but this will throw...
Read more >MockK | mocking library for Kotlin
A class cast exception is usually thrown in this case. Opt for stubbing manually in the case of a generic return type. Workaround:...
Read more >mockk-io/Lobby - Gitter
I'm stubbing, or trying to stub, a call to a mock which one of its parameters is a vararg of Any ... ClassCastException:...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
This is still an issue and I argue that it’s an important one since it affects all modern Android development projects
since the issue is not resolved yet, could someone tell me what might be the best workaround for this?