当我在Roboelectric测试中检查运行服务时,Nullpointerexception

sr0*_*r09 6 java android android-testing

当我对onCreate()方法检查某个特定服务是否正在运行的活动运行测试时,我得到一个NPE.在测试的设置方法中,我调用:

ActivityController<MyActivity> ac = Robolectric.buildActivity(MyActivity.class).create();
    mActivity = ac.get();
Run Code Online (Sandbox Code Playgroud)

在活动的onCreate()方法中,我调用isServiceRunning()

ActivityManager manager = (ActivityManager) mContext.getSystemService(Context.ACTIVITY_SERVICE);
    if(manager != null) {
        for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
            if (service.service != null && "MyService".equals(service.service.getClassName())) {
                return true;
            }
        }
    }
    return false;
Run Code Online (Sandbox Code Playgroud)

执行此操作时,NPE失败:

for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE))
Run Code Online (Sandbox Code Playgroud)

manager不是null,但NPE在android库中的ActivityManager.getRunningServices(int)中抛出.

如果我需要这种方法通过,我该怎么办?我正在使用Roboelectric 2.2-20130612.001729-6-jar-with-dependencies.jar但我现在看到问题,即使是最新版本.

我使用Android SDK版本19,但我现在切换到18,我仍然看到问题.

有任何想法吗?

堆栈跟踪:

java.lang.NullPointerException  
at android.app.ActivityManager.getRunningServices(ActivityManager.java:1106)
at com.example.MyActivity.isServiceRunning(MyActivity.java:151)
at com.example.MyActivity.onCreate(MyActivity.java:43)
at android.app.Activity.performCreate(Activity.java:5133)
at org.fest.reflect.method.Invoker.invoke(Invoker.java:112)
at org.robolectric.util.ActivityController$1.run(ActivityController.java:116)
at org.robolectric.shadows.ShadowLooper.runPaused(ShadowLooper.java:257)
at org.robolectric.util.ActivityController.create(ActivityController.java:111)
at org.robolectric.util.ActivityController.create(ActivityController.java:123)
at com.example.test.MyActivityTest.setUp(MyActivityTest.java:44)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:27)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:30)
at org.robolectric.RobolectricTestRunner$2.evaluate(RobolectricTestRunner.java:233)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
at org.robolectric.RobolectricTestRunner$1.evaluate(RobolectricTestRunner.java:174)
at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Run Code Online (Sandbox Code Playgroud)

nap*_*ror 1

您需要更改 if 语句以使其更具包容性。来自该函数的android文档:
注意:此方法仅用于调试或实现服务管理类型的用户界面。

所以我建议您对 null checkS 非常小心(注意复数)。下面的顺序很重要,否则您可能会在if语句的条件中收到空指针。

  1. 确保经理不为空,然后
  2. 确保 List<> 不为空,然后
  3. 确保该值不为空。
    (空和空之间有很大区别,例如:我们不能在空列表上运行 .isEmpty() 函数)。

if(manager != null && manager.getRunningServices(MAX) != null && !manager.getRunningServices(MAX).isEmpty() != null)

旁注:在第 1 个检查之后,您将服务列表保存到一个变量中,然后对该变量有第二条语句, 然后在第二条语句中执行您的语句,这样会更容易阅读。iffor eachif