3

I am facing an issue to test the alarms that set on alarm manager. I want to check the alarm manager using unit test or Instrumented test.

val alarmManager1 = context.getSystemService(Context.ALARM_SERVICE) as AlarmManager

val alarmIntent1 = Intent(context, AlarmReceiver::class.java)

val requestCode1 = 1
val pendingIntent1 = PendingIntent.getBroadcast(context, requestCode1, alarmIntent1, 0)

val calendar: Calendar = Calendar.getInstance()
val currentDateTime = LocalDateTime.now()

calendar.set(
        currentDateTime.year,
        currentDateTime.monthValue - 1,
        currentDateTime.dayOfMonth.plus(1),
        1,
        2,
        0
    )

alarmManager1.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, calendar.timeInMillis, pendingIntent1)

1 Answers1

0

I believe you don't need to check if the alarm manager will be called on time or not as that is a functionality provided by Android OS and is already well tested.

Instead you should focus on what your business logic is i.e. the piece of code that will be triggered when alarm goes off.

You can always extract this code in a function that can then be tested by a regular JUnit / Mockito combination.

Taranmeet Singh
  • 1,199
  • 1
  • 11
  • 14