经过三天试图通过这个网站和其他网站找到这个,我真的需要你的帮助。
我想在一个类中测试一个方法。此方法使用活动类上下文来调用意图。当我从测试方法调用它时,我得到一个 NullPointerException。我怎样才能做到这一点?(请添加示例代码)。
附件是 ActivityClass。
对接类中的方法:
public boolean powerConnected() {
boolean res = false;
IntentFilter filter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
Context cont = Accessories.context;
Intent intent = cont.registerReceiver(null, filter); --Throws the exception
int plugged = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);
if(plugged == BatteryManager.BATTERY_PLUGGED_AC){
Log.d(TAG, "AC "+plugged);
res = true;
}else if (plugged == BatteryManager.BATTERY_PLUGGED_USB){
Log.d(TAG, "USB "+plugged);
res = false;
}
return res;
}
测试方法:
@Test
public void testPowerConnected_AssertParamConnected_ReturnTrue() {
Docking docking = new Docking();
boolean result = docking.powerConnected();
assertTrue(result);
}
太感谢了。