(Unit)Testing of ArrayAdapter

I have extensive use of ArrayAdapter in my app because most Activities are holding a ListView and I need some custom stuff in them.

I took a look at the test classes in the android developer documentation but wasn't able to find some examples or a proper testclass...

1) Are there any best practices for (unit)-testing ArrayAdapter in Android?

2) May I have chosen the wrong approach (with the adapters) and killed testability this way?


You can write the test extending AndroidTestCase It will looks something like this:

public class ContactsAdapterTest extends AndroidTestCase {
    private ContactsAdapter mAdapter;

    private Contact mJohn;
    private Contact mJane;

    public ContactsAdapterTest() {

    protected void setUp() throws Exception {
        ArrayList<Contact> data = new ArrayList<Contact>();

        mJohn = new Contact("John", "+34123456789", "uri");
        mJane = new Contact("Jane", "+34111222333", "uri");
        mAdapter = new ContactsAdapter(getContext(), data);

    public void testGetItem() {
        assertEquals("John was expected.", mJohn.getName(),
                ((Contact) mAdapter.getItem(0)).getName());

    public void testGetItemId() {
        assertEquals("Wrong ID.", 0, mAdapter.getItemId(0));

    public void testGetCount() {
        assertEquals("Contacts amount incorrect.", 2, mAdapter.getCount());

    // I have 3 views on my adapter, name, number and photo
    public void testGetView() {
        View view = mAdapter.getView(0, null, null);

        TextView name = (TextView) view

        TextView number = (TextView) view

        ImageView photo = (ImageView) view

        //On this part you will have to test it with your own views/data
        assertNotNull("View is null. ", view);
        assertNotNull("Name TextView is null. ", name);
        assertNotNull("Number TextView is null. ", number);
        assertNotNull("Photo ImageView is null. ", photo);

        assertEquals("Names doesn't match.", mJohn.getName(), name.getText());
        assertEquals("Numbers doesn't match.", mJohn.getNumber(),

Probably you will have to test getView several times with different arguments, to test all scenarios.

Need Your Help

Programmatically Add Image to TableView Cell

objective-c iphone xcode swift

I have a UITableView that gets information from a server and publishes the data into a table view. Inside of each cell is the information from the server.