0

I have the following setup:

    @Mock
    lateinit var dialogEventObserver: Observer<DialogEvent>

    @Mock
    lateinit var detailsObserver: Observer<DetailsEvent>


private fun setup() {
    MockitoAnnotations.openMocks(this)

    viewModel = MyViewModel(
       interactor = interactor
    )

    viewModel.dialogEvent.observeForever(dialogEventObserver)
    viewModel.details.observeForever(detailsObserver)
}

@Test
fun `Test that is failed`() {
    setup()
    whenever(interactor.downloadStuff("random_id_string"))
        .thenReturn(Observable.error(IOException()))

    viewModel.downloadStuff("random_id_string")

    verify(interactor, times(1)).downloadStuff(eq("random_id_string"))
    verify(detailsObserver, never()).onChanged(any())
    verify(dialogEventObserver, never()).onChanged(any())
}

This test is causing the following error:

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: 
Misplaced or misused argument matcher detected here:

-> at com.nhaarman.mockito_kotlin.KArgumentCaptor.capture(ArgumentCaptor.kt:71)

You cannot use argument matchers outside of verification or stubbing.
Examples of correct usage of argument matchers:
    when(mock.get(anyInt())).thenReturn(null);
    doThrow(new RuntimeException()).when(mock).someVoidMethod(any());
    verify(mock).someMethod(contains("foo"))

This message may appear after an NullPointerException if the last matcher is returning an object 
like any() but the stubbed method signature expect a primitive argument, in this case,
use primitive alternatives.
    when(mock.get(any())); // bad use, will raise NPE
    when(mock.get(anyInt())); // correct usage use

Also, this error might show up because you use argument matchers with methods that cannot be mocked.
Following methods *cannot* be stubbed/verified: final/private/equals()/hashCode().
Mocking methods declared on non-public parent classes is not supported.

However, the line it points to is this one: MockitoAnnotations.openMocks(this) If I remove this test, the error will be thrown in other tests. Always pointing to the initialization line. Why would this be ?

nightfixed
  • 871
  • 1
  • 12
  • 24

0 Answers0