我有一个具有 MVP 架构的应用程序,其中包括这两种方法: 在 Presenter 类中:
override fun callSetRecyclerAdapter() {
view.setRecyclerAdapter()
view.setRefreshingFalse()
}
在模型类中
override fun handleApiResponse(result : Result) {
articleList = result.articles
presenter.callSetRecyclerAdapter()
}
关键是我想做一个测试来检查articleList
inhandleApiResponse
是否为 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.