question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

MockKException: Class cast exception happened while mocking generic class

See original GitHub issue

Expected 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.

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

https://scans.gradle.com/s/5e26rzx5n36zs/tests/:app:testDevelopDebugUnitTest/com.example.myapplication.UnitTest2/test1#1

Minimal reproducible code (the gist of this issue)

https://github.com/ganadist/VersionCodeDemo/blob/mockk_cast_exception_robolectric_4.4/app/src/test/java/com/example/myapplication/UnitTest2.kt#L28-L30

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:8
  • Comments:15

github_iconTop GitHub Comments

7reactions
alixwarcommented, Nov 19, 2020

This is still an issue and I argue that it’s an important one since it affects all modern Android development projects

6reactions
SnexusGcommented, Apr 14, 2021

since the issue is not resolved yet, could someone tell me what might be the best workaround for this?

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found