Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/mercurial/2.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单元测试中模拟Resources类以获得scaledDensity?_Android_Mocking_Mockito_Robolectric - Fatal编程技术网

如何在android单元测试中模拟Resources类以获得scaledDensity?

如何在android单元测试中模拟Resources类以获得scaledDensity?,android,mocking,mockito,robolectric,Android,Mocking,Mockito,Robolectric,我在android项目中使用了“ResourceResources.getSystem().getDisplayMetrics().scaledDensity”语句,以获得缩放密度。当我编写单元测试时,它说应该模拟资源类。但当我尝试模拟时,android会向我抛出一个错误:参考资料中的getSystem()方法未被模拟。太晚了,无法回答,但我将把它留在这里:) 我这么做是为了 Resources.getSystem().getStrings(); 我认为这可以根据具体情况加以调整 Resour

我在android项目中使用了“ResourceResources.getSystem().getDisplayMetrics().scaledDensity”语句,以获得缩放密度。当我编写单元测试时,它说应该模拟资源类。但当我尝试模拟时,android会向我抛出一个错误:参考资料中的getSystem()方法未被模拟。

太晚了,无法回答,但我将把它留在这里:)

我这么做是为了

Resources.getSystem().getStrings();
我认为这可以根据具体情况加以调整

ResourceResources.getSystem().getDisplayMetrics().scaledDensity
我也被这件事所困扰我这样做希望能有所帮助

 PowerMockito.mockStatic(Resources.class,RETURNS_MOCKS);
 Resources res=Mockito.mock(Resources.getSystem().getClass());
 PowerMockito.when(Resources.getSystem()).thenReturn(res);
 Mockito.when(res.getString(Mockito.anyInt())).thenReturn("any_message");
选中,您可以配置大小限定