【发布时间】:2018-03-15 11:18:52
【问题描述】:
我正在使用 android studio 3.0 和 Robolectric 3.3.2
在BaseActivity的onCreate()中调用如下方法
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
.................
setScreenBrightness();
}
setScreenBrightness() 添加在 oncreate() 方法中。这里我想忽略单元测试用例中的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();
}
在运行单元测试用例时,在每个测试用例中,所有测试用例都会出现空指针异常。
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)
建议我解决这个问题。
【问题讨论】:
-
看起来像an issue in Robolectric,尽管它很少受到关注。还有another unanswered question 与这个几乎相同。
标签: android unit-testing junit4 robolectric