java - 在 Roboelectric 测试中检查正在运行的服务时出现 Nullpointerexception

标签 java android android-testing

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

ActivityController<MyActivity> ac = Robolectric.buildActivity(MyActivity.class).create();
    mActivity = ac.get();

在 Activity 的 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;

执行此操作时失败并返回 NPE:

for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE))

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

需要这个方法通过怎么办?我使用的是 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)

最佳答案

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

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

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

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

旁注:检查数字 1 后,将服务列表保存到变量并只有第二个 if 会更容易阅读> 声明该变量,然后在第二个 if 语句中执行您的 for each 语句。

关于java - 在 Roboelectric 测试中检查正在运行的服务时出现 Nullpointerexception,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22057333/

相关文章:

android - 找不到 Gradle DSL 方法 : 'androidTestImplementation()'

Java工厂模式-如何防止匿名子类

javascript - 从 vaadin 中的 java 类调用 javascript 函数

java - 将文本文件编码为 utf-8 以进行 xml 导入

android - TabLayout - 选定的选项卡具有不同的字体

java - 我的 android 项目测试设置中缺少任何内容吗?

java - 有没有 API 可以帮助我模仿我已经在 Excel 中做的笔记?

android - CalendarView 无法将日期设置为当天?

android - 如何以编程方式在android中随机生成id?

android-testing - 库模块 'com.android.library' 中的 Espresso 测试