【问题标题】:Mockito - MissingMethodInvocationExceptionMockito - MissingMethodInvocationException
【发布时间】:2016-04-26 15:12:46
【问题描述】:

我正在尝试测试 API 调用是否安排在正确的调度程序上并在主线程上观察。

@RunWith(PowerMockRunner.class)
@PrepareForTest({Observable.class, AndroidSchedulers.class})
public class ProductsPresenterTest {

    private  ProductsPresenter presenter;
    @Before
    public void setUp() throws Exception{
        presenter = spy(new ProductsPresenter(mock(SoajsRxRestService.class)));
    }


    @Test
    public void testShouldScheduleApiCall(){
        Observable productsObservable = mock(Observable.class);
        CatalogSearchInput catalogSearchInput = mock(CatalogSearchInput.class);
        when(presenter.soajs.getProducts(catalogSearchInput)).thenReturn(productsObservable);

        /* error here*/ 
        when(productsObservable.subscribeOn(Schedulers.io())).thenReturn(productsObservable);
        when(productsObservable.observeOn(AndroidSchedulers.mainThread())).thenReturn(productsObservable);
        presenter.loadProducts(catalogSearchInput);

        //verify if all methods in the chain are called with correct arguments
        verify(presenter.soajs).getProducts(catalogSearchInput);
        verify(productsObservable).subscribeOn(Schedulers.io());
        verify(productsObservable).observeOn(AndroidSchedulers.mainThread());
        verify(productsObservable).subscribe(Matchers.<Subscriber<Result<Catalog<SoajsProductPreview>>>>any());
    }
}

线

when(productsObservable.subscribeOn(Schedulers.io())).thenReturn(productsObservable);

抛出以下异常,我不明白为什么,因为 productObservable 是一个模拟。有什么想法或类似经验吗?

org.mockito.exceptions.misusing.MissingMethodInvocationException: 
when() requires an argument which has to be 'a method call on a mock'.
For example:
    when(mock.getArticles()).thenReturn(articles);

Also, this error might show up because:
1. you stub either of: final/private/equals()/hashCode() methods.
   Those methods *cannot* be stubbed/verified.
   Mocking methods declared on non-public parent classes is not supported.
2. inside when() you don't call method on mock but on some other object.

【问题讨论】:

  • 您是否尝试过 Matcher,即 when(productsObservable.subscribeOn(any(Scheduler.class))),或者 Captor?
  • 是的,我也遇到了同样的错误
  • 你可以试试doReturn().when() 语法,有时它可以解决像你这样的问题吗?

标签: android mockito retrofit2 rx-android


【解决方案1】:

问题在于 Observable::subscribeOn 是一个最终方法,Mockito can't mock。 一种可能的解决方案是使用 Powermock:

@RunWith(PowerMockRunner.class)
@PrepareForTest(Observable.class)
public class MockTest {
    @Test
    public void test() {
        Observable productsObservable = PowerMockito.mock(Observable.class);

        when(productsObservable.subscribeOn(null)).thenReturn(productsObservable);
        productsObservable.subscribeOn(null);
        verify(productsObservable).subscribeOn(null);
    }
}

【讨论】:

  • 谢谢。我的错误是使用 Mockito.mock 尽管我使用了 PowerMockRunner
猜你喜欢
  • 1970-01-01
  • 2019-01-04
  • 2020-08-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-21
相关资源
最近更新 更多