https://kotlinlang.org logo
#mockk
Title
# mockk
k

Klitos Kyriacou

03/03/2022, 5:51 PM
Hi, in this piece of code, I'm mocking a JDBC
ResultSet
, and therefore its
getLong()
method returns a JVM platform type (i.e. it could be
Long
or
Long?
):
Copy code
val id: Long? = <...>
every { resultSet.getLong("id") } returns id
Unfortunately, this results in the error message "Type mismatch: inferred type is Long? but Long was expected." Is this a bug, a known limitation, or something I'm doing wrong? I can work around it like this:
Copy code
every { resultSet.getLong("id") as Long? } returns id
but then IntelliJ IDEA warns me that
as Long?
is unnecessary.
s

Sam

03/03/2022, 6:01 PM
I think the problem may actually be the opposite of what you describe.
getLong
returns a primitive
long
, so it can never be null. Your id is defined as
Long?
, which is nullable, so it can't be used as a return value for
getLong
.
👍 1
k

Klitos Kyriacou

03/03/2022, 6:09 PM
Indeed, you're right, Sam. My mistake. Thanks.
2 Views