您好我正在尝试在我的单元测试中添加一个模拟版本的Power Manager,但似乎无法在robolectric 3.2.2的文档中找到关于如何使用自定义阴影上下文的信息?
这是我的代码:
activityController = Robolectric.buildActivity(activityClass).withIntent(intent).attach();
activity = activityController.create().get();
PowerManager mockPowerManager = Mockito.mock(PowerManager.class);
Mockito.when(mockPowerManager.isScreenOn()).thenReturn(false);
ShadowContextImpl shadowContext = new ShadowContextImpl();
shadowContext.setSystemService(Context.POWER_SERVICE, mockPowerManager);
如何将shadowContext注入或添加到我正在测试的活动中?
答案 0 :(得分:0)
您可以使用ShadowPowerManager而不是Mockito
PowerManager powerManager = (PowerManager) RuntimeEnvironment.application.getSystemService(Context.POWER_SERVICE);
ShadowPowerManager shadowPowerManager = shadowOf(powerManager);
shadowPowerManager.setIsScreenOn(false);