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 a function with matchers that has default arguments throws InvalidUseOfMatchersException

See original GitHub issue
class UnderTest {
  fun doStuff(string1: String, string2: String = "hello world") = "do stuff"
}

@Test
fun testIt() {
  val underTest = mock(UnderTest::class.java)
  whenever(underTest.doStuff(anyString())).thenReturn("stuff done")
}

InvalidUseOfMatchersException “This exception may occur if matchers are combined with raw values”

Theoretically adding @JvmOverloads to doStuff should fix it but it didn’t. Even if it did, it would be nice if mockito-kotlin could figure out how to generate a matcher that matches the default value. Alternatively, automatically generate an any() matcher.

Issue Analytics

  • State:open
  • Created 6 years ago
  • Reactions:26
  • Comments:6

github_iconTop GitHub Comments

12reactions
zwiebackcommented, Sep 28, 2018

To solve this issue you can try the following code:

class UnderTest {
  fun doStuff(string1: String, string2: String) = "do stuff"
  fun doStuff(string1: String) = doStuff(string1, "hello world")
}

I know it’s not the true Kotlin way, but it’s only a workaround.

2reactions
guelocommented, Jan 2, 2019

I wonder if it would be possible to use the advanced Mockito APIs to intercept the call and add the extra matchers automatically. https://static.javadoc.io/org.mockito/mockito-core/2.23.4/org/mockito/Mockito.html#framework_integrations_api

Read more comments on GitHub >

github_iconTop Results From Across the Web

Mocking a function with matchers that has default ... - GitHub
I created a Java class with static methods to do my verify calls when I have default arg, and call this from my...
Read more >
Mockito: InvalidUseOfMatchersException - java - Stack Overflow
Mocked method must be of mocked class, created with ... InvalidUseOfMatchersException: Invalid use of argument matchers! 1 matchers expected ...
Read more >
Mockito argument matchers should be used on all parameters
Mockito provides argument matchers and argument captors for flexibly ... have matchers as well, otherwise an InvalidUseOfMatchersException will be thrown.
Read more >
Mockito ArgumentMatchers - Baeldung
If a method has more than one argument, we can't just use ... will detect the misplaced argument and throw an InvalidUseOfMatchersException.
Read more >
A Unit Testing Practitioner's Guide to Everyday Mockito - Toptal
With Mockito, creating a mock is as easy as calling a static method ... detect the misplaced argument matcher and throw an InvalidUseOfMatchersException...
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