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 returns null with named argument mixed ordering

See original GitHub issue

I’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:open
  • Created 5 years ago
  • Reactions:7
  • Comments:9

github_iconTop GitHub Comments

4reactions
StormeHawkecommented, Mar 19, 2021

We’re software engineers. If our job was easy anybody could do it 😉

3reactions
tianyanzcommented, Feb 17, 2022

Is there any plan to fix this?

Read more comments on GitHub >

github_iconTop 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 >

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