I have an application with MVP architecture which includes these two methods : In Presenter class :
override fun callSetRecyclerAdapter() {
view.setRecyclerAdapter()
view.setRefreshingFalse()
}
And in Model class
override fun handleApiResponse(result : Result) {
articleList = result.articles
presenter.callSetRecyclerAdapter()
}
The point is that I want to make a test which checks if articleList
in handleApiResponse
is null it couldn't go code further
I tried doing it with this test class :
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()
}
But after launching I get this error message in run screen :
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.