Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/204.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/amazon-web-services/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android:在JUnit中传递上下文并使用共享首选项_Android_Amazon Web Services_Junit - Fatal编程技术网

Android:在JUnit中传递上下文并使用共享首选项

Android:在JUnit中传递上下文并使用共享首选项,android,amazon-web-services,junit,Android,Amazon Web Services,Junit,在android上,我使用JUnit测试AmazonWeb服务器cognito,它需要一个上下文来访问共享首选项。我试图模拟上下文,并使用android工具,但两种方法都不起作用。正在接收上下文,但我无法访问共享首选项,因为我得到了一个空指针 有什么建议吗? 谢谢。如果你想模仿,你可以尝试模仿。你需要找到所有需要嘲笑的电话 final Context mockContext = Mockito.mock(Context.class); final SharedPreference

在android上,我使用JUnit测试AmazonWeb服务器cognito,它需要一个上下文来访问共享首选项。我试图模拟上下文,并使用android工具,但两种方法都不起作用。正在接收上下文,但我无法访问共享首选项,因为我得到了一个空指针

有什么建议吗?
谢谢。

如果你想模仿,你可以尝试模仿。你需要找到所有需要嘲笑的电话

    final Context mockContext = Mockito.mock(Context.class);
    final SharedPreferences preferences = Mockito.mock(SharedPreferences.class);
    Mockito.when(preferences.getString("<identitypool>.<key>", null)).thenReturn("someValue");
final Context mockContext=Mockito.mock(Context.class);
最终SharedReferences首选项=Mockito.mock(SharedReferences.class);
Mockito.when(preferences.getString(“.”,null)).thenReturn(“someValue”);