Attempting to check Settings.System.canWrite(context) result

让人想犯罪 __ 提交于 2019-12-11 13:43:04

问题


I'm using Robolectric 3.5 and the latest from Android Studio 3.0, and I'm attempting a piece of code, which needs to verify Settings.System.canWrite(Context context) was called and also do something with the true/false result.

I've been checking around, and I cannot figure out how to set up this scenario with Robolectric.

How do you approach it?

//the settings write verifier class...

...
public SettingsWriteVerifier(ctxt){
   context = ctxt;
}
public boolean canWrite() {
    return Settings.System.canWrite(context);
}

// the test

...
private Context context;

@Before
public void Setup() throws Exception {
    context = RuntimeEnvironment.application.getApplicationContext();
}

@Test
@Config(sdk = Build.VERSION_CODES.N)
public void AndIsTrue() throws Exception {
    SettingsWriteVerifier s = new SettingsWriteVerifier(context);
    assertEquals(s.canWrite(), true);
}

it blows up with the following:

java.lang.NullPointerException
at android.provider.Settings$System.canWrite(Settings.java:4055)
at SettingsWriteVerifierTests.AndCanWrite.AndIsTrue(AndCanWrite.java:41)
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.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.robolectric.RobolectricTestRunner$HelperTestRunner$1.evaluate(RobolectricTestRunner.java:523)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.robolectric.internal.SandboxTestRunner$2.evaluate(SandboxTestRunner.java:226)
at org.robolectric.internal.SandboxTestRunner.runChild(SandboxTestRunner.java:108)
at org.robolectric.internal.SandboxTestRunner.runChild(SandboxTestRunner.java:35)
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:62)
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)


Process finished with exit code -1

There doesn't seem to be any way to set up the canWrite method result, but honestly, I'm still really new to this.

Thanks, Kelly

来源:https://stackoverflow.com/questions/46941378/attempting-to-check-settings-system-canwritecontext-result

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!