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.

Mocking unsigned Kotlin types ends in `java.lang.ClassCastException`

See original GitHub issue

I’m mocking simple function returning UInt but after that method is executed in the test it crashes with java.lang.ClassCastException.

every { calculator.compute(any()) } returns 23859762u

Expected Behavior

No crash, mocked function returns unsigned number result.

Current Behavior

Exception java.lang.ClassCastException: kotlin.UInt cannot be cast to java.lang.Integer is thrown in case of UInt return type.

Steps to Reproduce

  1. Write mockk every function for a method returning unsigned number like UInt
  2. Call this function in the test
  3. Exception is thrown

Context

  • MockK version: 1.10.0
  • OS: macOS Catalina
  • Kotlin version: 1.3.72
  • JUnit version: 4.12
  • Type of test: unit test

Minimal reproducible code (the gist of this issue)

    every { calculator.compute(any()) } returns 23859762u
    assertEquals(23859762u, calculator.compute(42))

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:2
  • Comments:7

github_iconTop GitHub Comments

1reaction
Raibazcommented, Apr 6, 2021

Unsigned integer support in Kotlin is still in beta, so I guess @d35h is right.

0reactions
migueltorchacommented, Jan 12, 2022

Same issue here trying to mock a UByte:

interface MyInterface {
  suspend fun myMethod(): UByte?
}

....

coEvery { myInterface.myMethod() } returns Random.nextInt().toUByte()

As result, I have the next: class java.lang.Byte cannot be cast to class kotlin.UByte (java.lang.Byte is in module java.base of loader 'bootstrap'; kotlin.UByte is in unnamed module of loader 'app') java.lang.ClassCastException: class java.lang.Byte cannot be cast to class kotlin.UByte (java.lang.Byte is in module java.base of loader 'bootstrap'; kotlin.UByte is in unnamed module of loader 'app')

Read more comments on GitHub >

github_iconTop Results From Across the Web

Mocking unsigned Kotlin types ends in `java.lang.ClassCastException`
I'm mocking simple function returning UInt but after that method is executed in the test it crashes with java.lang.ClassCastException .
Read more >
Mocking a generic method in Kotlin causes CCE
I am trying to mock a method call with a type argument in Kotlin ... Method.invoke(Method.java:566) at org.junit.platform.commons.util.
Read more >
Integer - Android Developers
Returns the unsigned quotient of dividing the first argument by the second where each argument and the result is interpreted as an unsigned...
Read more >
Groovy Language Documentation
SAM type coercion; Differences with Java 8 default methods ... A consequence of slash escaping is that a slashy string can't end with...
Read more >
The jOOQ Release Note History
#11560, java.lang.ClassCastException: class org.jooq.impl.ResultImpl cannot be cast to class java.sql.ResultSet when mocking Oracle SYS_REFCURSOR procedure.
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