2011-07-28 39 views
4

我試圖以TDD-ish方式創建服務,爲此我創建了以下測試。該服務基本上輪詢Web服務並將新信息放入Content Provider中。由於它是一項服務,我正在使用Content Provider來存儲信息,作爲測試的預測。ServiceTestCase中的MockContentResolver空指針

我想我想要做的是創建一個MockContentResolver爲了實現這一點,但缺乏一個ProviderTestCase2類以外的例子。但是,當我運行此腳本時,它在addProvider行上爲空指針。

有沒有人有創建/訪問模擬出內容解析器的例子?在一個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); 
    } 
} 

回答

1

對我來說,好像你從來沒有實例化您的mContentResolver(你沒有像mContentResolver = new MockContentResolver();線。