MockContentResolver at ServiceTestCase Null Pointers

I am trying to create a service in TDD-ish, and for this purpose I created the following test. The service basically polls the web service and puts the new information into the content provider. Since this is a service, I use the Content Provider to store information as the oracle of the test.

I think I want to make a MockContentResolver to achieve this, but there are no examples of it outside the ProviderTestCase2 class. When I run this script, however, it has null pointers on the addProvider line.

Does anyone have an example of creating / accessing a wanted content editor? In ServiceTestCase?

public class OnDemandPollingServiceTests extends ServiceTestCase<OnDemandJobFetchingService> { private MockContentResolver mContentResolver; public OnDemandPollingServiceTests() { super(OnDemandJobFetchingService.class); } protected void setUp() throws Exception { super.setUp(); mContext = getContext(); ContentProvider cp = new OnDemandJobInfoProvider(); mContentResolver.addProvider(OnDemandJobInfoProvider.AUTHORITY, cp); } protected void tearDown() throws Exception { super.tearDown(); } public void testJobInsertion() { Uri url = Jobs.JobsColumns.CONTENT_URI; Cursor cursor; cursor = mContentResolver.query(url, null, null, null, null); int before = cursor.getCount(); cursor.close(); Intent startIntent = new Intent(); startIntent.setClass(mContext, OnDemandJobFetchingService.class); startService(startIntent); cursor = mContentResolver.query(url, null, null, null, null); int after = cursor.getCount(); cursor.close(); assertTrue(before != after); } } 
+4
android mocking servicetestcase
source share
1 answer

It seems to me that you never created an instance of mContentResolver (you do not have a string like mContentResolver = new MockContentResolver();

+1
source share

All Articles