找到你要的答案

Q:Is it possible to mock a system service in Android

Q:是否有可能在Android中模拟系统服务

all. I'm new to use Mockito:)

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

when(mockContext.getSystemService(Context.TELEPHONY_SERVICE)).thenReturn(mockTelephonyManager);

the above won't work when espresso test trying to use the telephony, it still return the system network country ISO. any thoughts? thanks!

所有。我是新来的Mockito:)

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

when(mockContext.getSystemService(Context.TELEPHONY_SERVICE)).thenReturn(mockTelephonyManager);

以上将无法工作时,意大利浓咖啡测试试图使用电话,它仍然返回系统网络国家ISO。有什么想法吗?谢谢!

answer1: 回答1:

Seems like any(Context.class) solved my issue. When passing context as parameters. Not sure why mocking context won't work.

似乎任何(上下文类)解决了我的问题。将上下文作为参数传递时。不知道为什么嘲讽上下文不起作用。

android  mockito