简体   繁体   中英

robotium, how to test listview

how can I test listview by robotium? I just want to check if oncreate event the items goes into it.

my Activity has a method:

private void initListView() {
    Adapter adapter = 
            new Adapter(this, myRepository.findAll());
    listView.setAdapter(adapter);
}

MyRepository returns List. In the ActivityInstrumentationTestCase2 I want to put some items into respository and then test if listView contains elements.

public void testListView_IsNotEmpty() {
    Item i = new Item();
    i.setSomething("item1");
    getActivity().getMyRepository().insert(i);
    assertTrue(solo.searchText("item1"));
}

Is it via robotium possible do to that?

best regards

This is clear case for mocking framework. I recomment jMockit as it is most advanced and suitable to use against stubbed out android libraries. As you do not like to test classes provided by android itself ( you implicitely trust that they do right thing ) , you only have to test that:

  • your repositry was asked for value list
  • this value list was used to create an adapter
  • this adapter was passed to list view

Test case would look like this:

@Test
public void testThatListInitializedProperly(@Mocked final ListView listView,
                                            @Mocked final YourRepository repository,
                                            @Mocked(methods = {"initListView"}, inverse=true) final YourActivity activity, 
                                            @Mocked final Adapter adapter
 ) {
    new Expectations() {
        {
               repository.findAll(); returns(someList);

               new Adapter(activity, someList); returns(adapter);

               listView.setAdapter(adapter);

        }
    };

    activity.initListView(listView);

}

(note that I adjusted interface for easier mockability )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM