What is the correct way to check the data from a PagingData object in Android Unit Tests What is the correct way to check the data from a PagingData object in Android Unit Tests android android

What is the correct way to check the data from a PagingData object in Android Unit Tests


I've had the same problem with the Paging3 library, and there not a lot discussions about this library online yet, but as I digging through some the docs, I may found a solution. The scenario I'm facing is trying to determine whether a data list is empty or not within PagingData, then I'll manipulate the UI base on that.

Here's what I found in the doc, there are two apis in PagingDataAdapter that have been added in version 3.0.0-alpha04 which is peek(), and snapshot(), peek() gives us a specific list object based on index, whereas snapshot() gives us the whole list.

So here's what I've done:

lifecycleScope.launch {    //Your PagingData flow submits the data to your RecyclerView adapter    viewModel.allConversations.collectLatest {        adapter.submitData(it)    }}lifecycleScope.launch {    //Your adapter's loadStateFlow here    adapter.loadStateFlow.        distinctUntilChangedBy {            it.refresh        }.collect {            //you get all the data here            val list = adapter.snapshot()            ...        }    }

Since I just get my hands on the Paging library and Flow recently, there might be flaws with this approach, let me know if there are better ways!


I agree with Zijian Wang the only way to test it, is through snapshot enter image description here


This only works when there is an active internet connection.