是否可以在Android中模拟系统服务

Wen*_*hao 7 android mockito

所有.我是新手使用Mockito :)

Context mockContext = mock(Context.class);
TelephonyManager mockTelephonyManager = mock(TelephonyManager.class);
when(mockTelephonyManager.getNetworkCountryIso()).thenReturn("CN");

when(mockContext.getSystemService(Context.TELEPHONY_SERVICE)).thenReturn(mockTelephonyManager);
Run Code Online (Sandbox Code Playgroud)

当espresso测试尝试使用电话时,上述方法无效,它仍然返回系统网络国家ISO.有什么想法吗?谢谢!

Wen*_*hao 0

似乎 any(Context.class) 解决了我的问题。将上下文作为参数传递时。不知道为什么模拟上下文不起作用。