Hi i am trying to add a mocked version of Power manager in my unit tests but cant seem to find in the documentations in robolectric 3.2.2 on how to use a custom shadow context?
Here is my code:
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);
How do i inject or add the shadowContext into the activity i am testing on?
You could use the ShadowPowerManager instead of Mockito
PowerManager powerManager = (PowerManager) RuntimeEnvironment.application.getSystemService(Context.POWER_SERVICE);
ShadowPowerManager shadowPowerManager = shadowOf(powerManager);
shadowPowerManager.setIsScreenOn(false);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.