Robolectric返回Settings.system.canWrite(context)的NullPointer异常

时间:2018-03-15 11:18:53

标签: android unit-testing junit4 robolectric

我正在使用 android studio 3.0 Robolectric 3.3.2

在BaseActivity中的onCreate()中调用以下方法

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    .................
    setScreenBrightness();
}

在oncreate()方法中添加了setScreenBrightness()。在这里,我想忽略setScreenBrightness()或Shadows单元测试用例中的设置。

public void setScreenBrightness() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        if (!Settings.System.canWrite(this)) {
            showAlertForPermissions();
        }
    }
}

在Robolectric测试课

@Before
 public void setUp() throws Exception{
    context = ShadowApplication.getInstance().getApplicationContext();
    activityController = Robolectric.buildActivity(BaseActivity.class).create().start();
    baseActivity = activityController.get();
}

运行单元测试用例时,在每个测试用例中,我得到所有测试用例的Null指针异常。

WARNING: unknown service appops

java.lang.NullPointerException
at android.provider.Settings.isCallingPackageAllowedToPerformAppOpsProtectedOperation(Settings.java:8413)
at android.provider.Settings.isCallingPackageAllowedToWriteSettings(Settings.java:8317)
at android.provider.Settings$System.canWrite(Settings.java:3722)
at com.esco.commissionicd.activities.BaseActivity.setScreenBrightness(BaseActivity.java:399)
at com.esco.commissionicd.activities.BaseActivity.onCreate(BaseActivity.java:224)
at android.app.Activity.performCreate(Activity.java:6251)
at org.robolectric.util.ReflectionHelpers.callInstanceMethod(ReflectionHelpers.java:231)
at org.robolectric.android.controller.ActivityController$1.run(ActivityController.java:140)
at org.robolectric.shadows.ShadowLooper.runPaused(ShadowLooper.java:362)
at org.robolectric.shadows.CoreShadowsAdapter$2.runPaused(CoreShadowsAdapter.java:40)
at org.robolectric.android.controller.ActivityController.create(ActivityController.java:137)
at org.robolectric.android.controller.ActivityController.create(ActivityController.java:147)
at org.robolectric.android.controller.ActivityController.setup(ActivityController.java:245)
at org.robolectric.Robolectric.setupActivity(Robolectric.java:97)
at org.robolectric.shadows.support.v4.SupportFragmentTestUtil.buildSupportFragmentManager(SupportFragmentTestUtil.java:36)
at org.robolectric.shadows.support.v4.SupportFragmentTestUtil.startFragment(SupportFragmentTestUtil.java:21)
at com.esco.commissionicd.fragments.AvailableSensorsFragmentTest.setUp(AvailableSensorsFragmentTest.java:73)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:24)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
at org.robolectric.internal.SandboxTestRunner$2.evaluate(SandboxTestRunner.java:209)
at org.robolectric.internal.SandboxTestRunner.runChild(SandboxTestRunner.java:109)
at org.robolectric.internal.SandboxTestRunner.runChild(SandboxTestRunner.java:36)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.robolectric.internal.SandboxTestRunner$1.evaluate(SandboxTestRunner.java:63)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runners.Suite.runChild(Suite.java:128)
at org.junit.runners.Suite.runChild(Suite.java:27)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:51)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
at com.intellij.rt.execution.application.AppMainV2.main(AppMainV2.java:131)

建议我解决此问题。

1 个答案:

答案 0 :(得分:0)

似乎其他开发者have come across this issue,但似乎不会很快就会出现问题。一个可能的解决方法是将您的System.Settings.canWrite()调用包含在自定义对象中,以后可以使用Mockito进行模拟以进行单元测试:

public class SettingsWrapper {
    public boolean canWrite(Context context) {
        return System.Settings.canWrite(context);
    }
}

请注意,我没有将此方法设为静态,因为Mockito仍然倾向于使用模拟静态方法。在正常情况下,我会将此方法设为静态。

在你的测试中你现在有这样的东西:

public class MyTest {
    @Mock SettingsWrapper wrapper;
    ...

    @Before public void setUp() {
        MockAnnotations.initMocks(this);

        // Change this return value based on what you're testing
        when(wrapper.canWrite(any(Context.class))).thenReturn(false);
        ...
    }

    ...
}

在您的代码中:

...
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
    SettingsWrapper wrapper = new SettingsWrapper();
    if (!wrapper.canWrite(this)) {
        showAlertForPermissions();
    }
}
...

同样,不理想,但鉴于目前Robolectric和Mockito的局限性,我能从头脑中想到最好的。