Since I did not receive an answer to my question, I will answer my question. I decided to go with a different approach than using my question code example. The basic outline of my new approach:
I switched the base test class to ActivityInstrumentationTestCase2
I created the MockHttpClient class that I insert into my code, and this MockHttpClient returns a successful HttpResponse with a response object containing my JSON device data. The MockHttpClient class implements the HttpClient interface and returns null for all methods, but the execute() methods, which should return an HttpResponse object.
Because the ListFragment I am testing is registering a BroadcastReceiver to determine that the data retrieval service is finished, I am also registering BroadcastReceiver in my test. I block my test with CountDownLatch until a broadcast is received.
When the translation is received, I use Thread.sleep(500) so that my activity updates the ListView . After that, I launch my claims against ListView .
I annotated my test using FlakyTest(tolerance=5) , which runs the test up to 5 times when statements fail.
I'm not sure this is a good approach, so please feel free to leave comments. But for now, it works. To complete my answer, new code for my test:
TEST CLASS
public class TopscorersActivityTest extends ActivityInstrumentationTestCase2<TopscorersActivity> { public static final String JSON = "[" + "{\"position\": 1, \"name\": \"Bas Dost\", \"club\": \"sc Heerenveen\", \"goals\": \"16\" }," + "{\"position\": 2, \"name\": \"Dries Mertens\", \"club\": \"PSV\", \"goals\": \"13\"}," + "{\"position\": 3, \"name\": \"Luuk de Jong\", \"club\": \"FC Twente\", \"goals\": \"12\"}" + "]"; private TopscorersActivity mActivity; private ListView mListView; public TopscorersActivityTest() { super("com.example.package", TopscorersActivity.class); } @Override protected void setUp() throws Exception { super.setUp(); ConnectivityUtils.setHttpClient(MockHttpClient.createInstance(JSON)); mActivity = getActivity(); mListView = (ListView) getActivity().findViewById(android.R.id.list); } @Override protected void tearDown() throws Exception { super.tearDown(); ConnectivityUtils.setHttpClient(null); } @MediumTest public void testPreconditions() { assertNotNull(mActivity); assertNotNull(mListView); assertEquals(0, mListView.getFirstVisiblePosition()); } @FlakyTest(tolerance=5) @LargeTest public void testListItemsPopulatedCorrectly() throws InterruptedException { waitForBroadcast(mActivity, TopscorersService.BROADCAST_ACTION, Intent.CATEGORY_DEFAULT); assertEquals(3, mListView.getCount());
CLIENT CLASS MOCK
public class MockHttpClient implements HttpClient { private HttpResponse mHttpResponse; public static HttpClient createInstance(String response) throws UnsupportedEncodingException { return createInstance(200, "OK", response); } public static HttpClient createInstance(int statusCode, String reasonPhrase, String response) throws UnsupportedEncodingException { return createInstance(HttpVersion.HTTP_1_1, statusCode, reasonPhrase, response); } public static HttpClient createInstance(ProtocolVersion version, int statusCode, String reasonPhrase, String response) throws UnsupportedEncodingException { StatusLine statusLine = new BasicStatusLine(version, statusCode, reasonPhrase); HttpResponse httpResponse = new BasicHttpResponse(statusLine); HttpEntity httpEntity = new StringEntity(response); httpResponse.setEntity(httpEntity); return new MockHttpClient(httpResponse); } private MockHttpClient(HttpResponse httpResponse) { mHttpResponse = httpResponse; } public HttpResponse execute(HttpUriRequest request) { return mHttpResponse; } @Override public HttpResponse execute(HttpUriRequest request, HttpContext context) throws IOException, ClientProtocolException { return mHttpResponse; } @Override public HttpResponse execute(HttpHost target, HttpRequest request) throws IOException, ClientProtocolException { return mHttpResponse; } @Override public <T> T execute(HttpUriRequest arg0, ResponseHandler<? extends T> arg1) throws IOException, ClientProtocolException { return null; } @Override public HttpResponse execute(HttpHost target, HttpRequest request, HttpContext context) throws IOException, ClientProtocolException { return mHttpResponse; } @Override public <T> T execute(HttpUriRequest arg0, ResponseHandler<? extends T> arg1, HttpContext arg2) throws IOException, ClientProtocolException { return null; } @Override public <T> T execute(HttpHost arg0, HttpRequest arg1, ResponseHandler<? extends T> arg2) throws IOException, ClientProtocolException { return null; } @Override public <T> T execute(HttpHost arg0, HttpRequest arg1, ResponseHandler<? extends T> arg2, HttpContext arg3) throws IOException, ClientProtocolException { return null; } @Override public ClientConnectionManager getConnectionManager() { return null; } @Override public HttpParams getParams() { return null; } }