mockito / mockito-kotlin

Using Mockito with Kotlin
MIT License
3.11k stars 202 forks source link

Nested KStubbing doesn't work #327

Closed guelo closed 5 years ago

guelo commented 5 years ago

    val stringMock = mock<List<List<String>>> {
        on { get(1) } doReturn mock {
            on { get(1) } doReturn ""
        }
    }

output:
-------

org.mockito.exceptions.misusing.UnfinishedStubbingException: 
Unfinished stubbing detected here:
-> at com.nhaarman.mockitokotlin2.KStubbing.on(KStubbing.kt:70)

E.g. thenReturn() may be missing.
Examples of correct stubbing:
    when(mock.isOk()).thenReturn(true);
    when(mock.isOk()).thenThrow(exception);
    doThrow(exception).when(mock).someVoidMethod();
Hints:
 1. missing thenReturn()
 2. you are trying to stub a final method, which is not supported
 3: you are stubbing the behaviour of another mock inside before 'thenReturn' instruction if completed
siviae commented 5 years ago

Makes me cry too. But unfortunately, seems like nothing could be done with it: https://stackoverflow.com/questions/26318569/unfinished-stubbing-detected-in-mockito/26319364

nhaarman commented 5 years ago

This is due to the way Mockito works internally, relying on perfect ordering of various static method calls. Unfortunately, this is a limitation of working with Mockito.