2013-11-23 107 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

回答

相关问题