user1892364
user1892364

Reputation: 269

Writing Unit Tests for Fragment

I have the following data class which holds the resource id and the onClickListener for a view:

data class Item(val name: Int, val onClick: View.OnClickListener)

Later I populate a Layouts children with (children extends ViewGroup to get a list of child views):

fun loadItems(list: List<Item>, viewGroup: ViewGroup){
    viewGroup.children.zip(list) { view, item ->
        (view as TextView).text = resources.getString(item.name)
        view.setOnClickListener(item.onClick)
    }
}

I want to test if a child of the ViewGroup

This code is part of a fragment. What is the easiest way to do this? I want this to be a local test.

Upvotes: 0

Views: 352

Answers (1)

Shayne3000
Shayne3000

Reputation: 747

Your best bet for a local test that involves some Android components as Activities or Fragments would be to use Robolectric as it performs tests using the JVM.

That being said, however, I assume that you're dealing with testing a list and the content of its items. In this case, you would probably be better off using Espresso particularly with its onData() calls. Worthy of note is that for such UI-related tests, you will need a device or emulator.

Upvotes: 2

Related Questions