Stubbing Fails for Methods with Non-constant Default Parameters
See original GitHub issueKotlin permits defining default parameters for methods that are data members of the class. However, stubbing such a method then verifying it causes a failure because stubbing will convert the default parameter to a null-value and verify
assumes the default parameter is non-null. When the original type of the default parameter is non-null, there is no way for me to verify using a pattern that supports null.
To make it more concrete, I’ve attached a test case (including build script – test.tar.gz) that demonstrates the issue. Executing this test case, results in
Argument(s) are different! Wanted:
classToMock.printSomething(
<any java.lang.String>,
<any java.lang.String>
);
-> at ClassToMock.printSomething(Test.kt:11)
Actual invocation has different arguments:
classToMock.printSomething(
"TEST",
null
);
-> at ClassToMock.printSomething$default(Test.kt:10)
at ClassToMock.printSomething(Test.kt:11)
at DefaultParameterFail.default parameter will cause verifier failure(Test.kt:29)
Here, the stubbed method was fun printSomething(message: String, suffix: String = defaultValue)
. Notice that the second parameter (the default parameter) becomes null upon stubbing, so verifying that it was invoked fails.
Issue Analytics
- State:
- Created 6 years ago
- Reactions:1
- Comments:5
Top GitHub Comments
As an alternative, you can use
anyOrNull()
:@nhaarman maybe
any()
can check if the type is nullable and then useanyOrNull()
inside of it based on that?