【发布时间】:2013-11-23 21:13:38
【问题描述】:
我有一个服务可以监听 GCM 消息并解析 JSON。最终(取决于消息内容)它会写入 SharedPreferences,因此它需要getApplicationContext()。
如果我“作为常规对象”对服务进行测试(创建服务,然后执行该方法),getApplicationContext() 在运行测试时以 null 结束
如果我使用 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
}
我仍然得到同样的空异常
【问题讨论】:
-
你在哪里使用
Context?
标签: java android testing junit