Testing ListView with Robolectric

I'm currently trying to test Android code with Robolectric, but I'm having some problems with my ListView. When I try to access a child view, the ListView always returns null due to an empty list.

The application implementation looks like this and creates a simple list view:

private ListView listView; private ArrayAdapter<String> adapter; private static String values[] = new String[] {"Android", "Apple", "Windows" }; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_overview); initialize(); } private void initialize() { listView = (ListView) findViewById(R.id.tweet_list); adapter = new ArrayAdapter<String>( getApplicationContext(), android.R.layout.simple_list_item_1, values); listView.setAdapter(adapter); } 

However, when I try to access my ListView, as shown below. Robolectric TestRunner always returns null when I access the ArrayAdapter via getChildAt ()

 private OverviewActivity activity; private ListView listView; @Before public void setUp() throws Exception { activity = new OverviewActivity(); activity.onCreate(null); listView = (ListView) activity.findViewById(R.id.tweet_list); } @Test public void shouldFindListView() throws Exception { if (listView.getChildCount() > 0) { assertThat( "Android", equalTo(listView.getChildAt(0).toString())); } else { fail("no child views are avaliable"); } } 
+4
source share
2 answers

If people like me still visit the link to decide how to check the list with Roboelectric 3.0. This is my MAinActivityTest file.

 private MainActivity mainActivity; private ListView lstView; @Before public void setup() throws Exception{ mainActivity= Robolectric.setupActivity(MainActivity.class); assertNotNull("Mainactivity not intsantiated",mainActivity); lstView=(ListView)mainActivity.findViewById(R.id.list);//getting the list layout xml ShadowLog.stream = System.out; //This is for printing log messages in console } @Test public void shouldFindListView()throws Exception{ assertNotNull("ListView not found ", lstView); ShadowListView shadowListView = Shadows.shadowOf(lstView); //we need to shadow the list view shadowListView.populateItems();// will populate the adapter ShadowLog.d("Checking the first country name in adapter " , ((RowModel)lstView.getAdapter().getItem(0)).getCountry()); assertTrue("Country Japan doesnt exist", "Japan".equals(((RowModel) lstView.getAdapter().getItem(0)).getCountry())); assertTrue(3==lstView.getChildCount()); } 

RowModel is a simple POJO for fields displayed in a list.

+6
source

I see that you are using an old version of Robolectric. We have no problems with the similar code that you have with Robolectric 1.1.

So add these lines to your pom file:

 <dependency> <groupId>com.pivotallabs</groupId> <artifactId>robolectric</artifactId> <version>1.1</version> <scope>test</scope> </dependency> 

Or download and connect it to your project in your idea from here http://mvnrepository.com/artifact/com.pivotallabs/robolectric/1.1

0
source

Source: https://habr.com/ru/post/1447742/


All Articles