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.

Stubbing Fails for Methods with Non-constant Default Parameters

See original GitHub issue

Kotlin 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:closed
  • Created 6 years ago
  • Reactions:1
  • Comments:5

github_iconTop GitHub Comments

2reactions
nhaarmancommented, Jun 3, 2017

As an alternative, you can use anyOrNull():

verify(mockedPrinter).printSomething(any(), anyOrNull())
0reactions
danielgomezricocommented, Nov 4, 2019

@nhaarman maybe any() can check if the type is nullable and then use anyOrNull() inside of it based on that?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Stubbing Fails for Methods with Non-constant Default ...
Kotlin permits defining default parameters for methods that are data members of the class. However, stubbing such a method then verifying it ...
Read more >
kotlin - Test function with default non-constant arguments
However, I cannot mock it with Mockito , I think because in the code it is called without the default argument. classA.dummy() always...
Read more >
Stubbing and Mocking in Java with the Spock Testing ...
Learn how to create true Java unit tests by mocking all external dependencies in your unit tests with the Spock testing framework.
Read more >
A Unit Testing Practitioner's Guide to Everyday Mockito
By default, all methods of a mock return “uninitialized” or “empty” values, e.g., zeros for numeric types (both primitive and boxed), false for...
Read more >
Method Parameter Matchers - Phake - Read the Docs
Wildcards can also come in handy when stubbing or verifying methods with default parameters or variable parameters. In addition to Phake::anyParameters() ...
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