Mocking a function with matchers that has default arguments throws InvalidUseOfMatchersException
See original GitHub issueclass 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:
- Created 6 years ago
- Reactions:26
- Comments:6
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
To solve this issue you can try the following code:
I know it’s not the true Kotlin way, but it’s only a workaround.
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