0

我有一个具有 MVP 架构的应用程序,其中包括这两种方法: 在 Presenter 类中:

override fun callSetRecyclerAdapter() {
    view.setRecyclerAdapter()
    view.setRefreshingFalse()
}

在模型类中

override fun handleApiResponse(result : Result) {
    articleList = result.articles
    presenter.callSetRecyclerAdapter()
}

关键是我想做一个测试来检查articleListinhandleApiResponse是否为 null 它不能进一步编码

我试着用这个测试类来做:

lateinit var newsModel: NewsModel
@Mock
lateinit var newsPresenter : NewsPresenter

@Before
fun setUp() {
    MockitoAnnotations.initMocks(this)
    newsModel = NewsModel(newsPresenter)
}

@Test
    fun makeRequestReturnNull() {
        newsModel.handleApiResponse(Result(ArgumentMatchers.anyList()))
        verify(newsPresenter, never()).callSetRecyclerAdapter()
    }

但启动后,我在运行屏幕中收到此错误消息:

Misplaced or misused argument matcher detected here:

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(anyObject());
    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.
4

1 回答 1

1

您正在使用anyList()作为被测方法的参数 -

newsModel.handleApiResponse(Result(ArgumentMatchers.anyList()))

anyXAPI 应该用于模拟/验证对模拟实例的调用。它显然不是一个“真正的”对象,因此不能在 Mockito 范围之外的调用中使用。您需要使用实际参数调用此方法并使用 Mockito 来控制任何依赖行为,以确保您只测试您的代码

于 2019-11-03T13:09:08.637 回答