You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I checked to make sure that this issue has not already been filed
Expected Behavior
The mocked suspending function with default arguments returns the Result<T>.
Current Behavior
The mocked function returns T instead of Result<T> and throws a java.lang.ClassCastException.
Failure Information (for bugs)
The unwrapping of the Result only happens if we use the default arguments and omit some of them when calling the function. If we specify all the arguments when calling the function then the mock behaves correctly and returns the Result without unwrapping. Additionally, this only happens when the mocked function is a suspend fun, for regular ones it works as expected.
Steps to Reproduce
First, we'll need an interface with a method that is a suspend fun with default arguments. This method has to return a value wrapped in a Result, here we'll use Result<Unit>. Below is an interface with a method that has one required argument name and one optional, default argument optionalFlag.
Then, we need to mock the method to always return Result.success(Unit). Just in case we're mocking both versions of the method: with and without the default argument. Then we try to call the method and verify that the result matches the one we've provided to the mock.
After that instead of receiving a Result<Unit> from the myInterface.getResult("name") call we receive a ClassCastException because the method returns a Unit instead of a Result<Unit>.
java.lang.ClassCastException: class kotlin.Unit cannot be cast to class kotlin.Result (kotlin.Unit and kotlin.Result are in unnamed module of loader 'app')
Context
MockK version: 1.12.2
OS: Ubuntu 20.04.3 LTS
Kotlin version: 1.5.10
JDK version: 11.0.13
JUnit version: 4.13.1
Type of test: unit test
Minimal reproducible code (the gist of this issue)
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. If you are sure that this issue is important and should not be marked as stale just ask to put an important label.
Prerequisites
Expected Behavior
The mocked suspending function with default arguments returns the
Result<T>
.Current Behavior
The mocked function returns
T
instead ofResult<T>
and throws ajava.lang.ClassCastException
.Failure Information (for bugs)
The unwrapping of the
Result
only happens if we use the default arguments and omit some of them when calling the function. If we specify all the arguments when calling the function then the mock behaves correctly and returns theResult
without unwrapping. Additionally, this only happens when the mocked function is asuspend fun
, for regular ones it works as expected.Steps to Reproduce
First, we'll need an interface with a method that is a
suspend fun
with default arguments. This method has to return a value wrapped in aResult
, here we'll useResult<Unit>
. Below is an interface with a method that has one required argumentname
and one optional, default argumentoptionalFlag
.Then, we need to mock the method to always return
Result.success(Unit)
. Just in case we're mocking both versions of the method: with and without the default argument. Then we try to call the method and verify that the result matches the one we've provided to the mock.After that instead of receiving a
Result<Unit>
from themyInterface.getResult("name")
call we receive aClassCastException
because the method returns aUnit
instead of aResult<Unit>
.Context
Minimal reproducible code (the gist of this issue)
The text was updated successfully, but these errors were encountered: