How do I create a PagedList of an object for tests?

后端 未结 3 490
佛祖请我去吃肉
佛祖请我去吃肉 2021-02-07 05:26

I have been working with the arch libraries from Google, but one thing that has made testing difficult is working with PagedList.

For this example, I am usi

3条回答
  •  甜味超标
    2021-02-07 05:47

    an easy way to achieve this, is to mock the PagedList. This fun will "convert" a list to a PagedList (in this case, we are not using the real PagedList rather just a mocked version, if you need other methods of PagedList to be implemented, add them in this fun)

     fun  mockPagedList(list: List): PagedList {
         val pagedList = Mockito.mock(PagedList::class.java) as PagedList
         Mockito.`when`(pagedList.get(ArgumentMatchers.anyInt())).then { invocation ->
            val index = invocation.arguments.first() as Int
            list[index]
         }
         Mockito.`when`(pagedList.size).thenReturn(list.size)
         return pagedList
     }
    

提交回复
热议问题