2013-11-23 35 views
2

我得到了一個服務來監聽GCM消息並解析JSON。最終(取決於消息內容),它寫入SharedPreferences,因此它需要getApplicationContext()有沒有辦法測試Android服務中的公共方法?

如果我做的服務「作爲一個有規則的」測試(創建服務,則執行該法)getApplicationContext()在空結束了運行測試

時,如果我使用ServiceTestCase

public void testHandleAuth() { 

       // MyClass is tested 
       Intent intent=new Intent("com.duniap.ptp.GcmIntentService"); 
       this.startService(intent); 
       GcmIntentService tester = new GcmIntentService(); 
       // a dummy json encoded in base64 
       dummyAuth64="eyJkZXZpY2UiOnsiaWQiOjU2LCJzZWNyZXQiOiJGc3VCcWZsdVFcL2l5bit5TlZadW5uZz09In0sImFjdGlvbiI6ImF1dGhvcml6ZSJ9"; 
       byte[] bytes = Base64.decode(dummyAuth64, Base64.DEFAULT); 
       String dummyAuth = new String(bytes); 
       tester.handleGCMmessage(dummyAuth); 
        //null pointer, if I debug I trace it to getApplicationContext() within this method 

    } 

我仍然得到相同的空例外

+0

當你使用'Context'? – Sajmon

回答

相關問題