Creating a mock AlarmManager for testing

I want to check the code that adds pending intents to the alarm manager , but so far I can create my own mock context to return it from getSystemService() I cannot create my own subclass of the Alarm Manager due to the fact that it has private constructor.

Will I have another way (better?) So that I can verify that my code correctly (or not) adds alarms based on my testing preconditions?

+7
source share
1 answer

Two things I can think of can help:

You can use (from the article):

 @RunWith(RobolectricTestRunner.class) public class ResetAlarmTest { ShadowAlarmManager shadowAlarmManager; AlarmManager alarmManager; @Before public void setUp() { alarmManager = (AlarmManager) Robolectric.application.getSystemService(Context.ALARM_SERVICE); shadowAlarmManager = Robolectric.shadowOf(alarmManager); } @Test public void start_shouldSetRepeatedAlarmWithAlarmManager() { Assert.assertNull(shadowAlarmManager.getNextScheduledAlarm()); new ResetAlarm(Robolectric.application.getApplicationContext()); ScheduledAlarm repeatingAlarm = shadowAlarmManager.getNextScheduledAlarm(); Assert.assertNotNull(repeatingAlarm); } } 
+9
source

All Articles