Stubbing returns null with named argument mixed ordering
See original GitHub issueI’m not sure whether it’s mockito-kotlin as it seems that something is wrong on boundary of kotlin/mockito, however it might be a thing that mockito-kotlin can solve somehow.
It seems like that using named arguments that are ordered in a different way compared to method signature breaks stubs so they return nulls only. Once I managed to get a class cast exception as Mockito was trying to cast my Arg2 into Arg underneath, however now I’m not able to reproduce it.
Here are couple of tests showing this behaviour
@Test // failing
fun `reverse order stub`() {
val arg = Arg()
val arg2 = Arg2()
val pointFactory: PointFactory = mock()
whenever(pointFactory.newInstance(y = any(), x = any())).thenReturn(mock())
val result = pointFactory.newInstance(arg, arg2)
Truth.assertThat(result).isNotNull()
}
@Test // failing
fun `reverse order stub, reverse invocation`() {
val arg = Arg()
val arg2 = Arg2()
val pointFactory: PointFactory = mock()
whenever(pointFactory.newInstance(y = any(), x = any())).thenReturn(mock())
val result = pointFactory.newInstance(y = arg2, x = arg)
Truth.assertThat(result).isNotNull()
}
@Test
fun `regular order stub, regular invocation`() {
val arg = Arg()
val arg2 = Arg2()
val pointFactory: PointFactory = mock()
whenever(pointFactory.newInstance(x = any(), y = any())).thenReturn(mock())
val result = pointFactory.newInstance(arg, arg2)
Truth.assertThat(result).isNotNull()
}
@Test
fun `regular order stub, reverse invocation`() {
val arg = Arg()
val arg2 = Arg2()
val pointFactory: PointFactory = mock()
whenever(pointFactory.newInstance(x = any(), y = any())).thenReturn(mock())
val result = pointFactory.newInstance(y = arg2, x = arg)
Truth.assertThat(result).isNotNull()
}
class Arg
class Arg2
class Point
class PointFactory {
fun newInstance(x: Arg, y: Arg2): Point {
return Point()
}
}
Also on mockito issue tracker: https://github.com/mockito/mockito/issues/1413
Issue Analytics
- State:
- Created 5 years ago
- Reactions:7
- Comments:9
Top Results From Across the Web
Stubbing returns null with named argument mixed ordering
It seems like that using named arguments that are ordered in a different way compared to method signature breaks stubs so they return...
Read more >Stubbing and Mocking in Java with the Spock Testing ...
This line means: “when the find() method is called with these arguments, then return null”. In true TDD fashion, we have created the...
Read more >mocked method is returning Null - java
I am using the when().thenReturn() and I feel like I mocked the return variable correctly. Many thanks in advance. I'm new to JUnit...
Read more >Spock Mocking and Stubbing (Examples with Video ...
An important point to note here is that you can always do mix and match for what arguments are known and what is...
Read more >Mockito (Mockito 4.10.0 API) - javadoc.io
Last stubbing is more important - when you stubbed the same method with the same arguments many times. Other words: the order of...
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
We’re software engineers. If our job was easy anybody could do it 😉
Is there any plan to fix this?