创建用于测试的模拟AlarmManager [英] Creating a mock AlarmManager for testing

查看:224
本文介绍了创建用于测试的模拟AlarmManager的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够测试一些code,增加了挂起的意图报警管理系统,但同时我可以创建自己的模拟背景从<$ C归还$ C> getSystemService()我无法创建我自己的子类报警经理由于有一个私人的构造吧。

I want to be able to test some code that adds pending intents to the Alarm Manager but while I can create my own mock context to return it from getSystemService() I can't create my own sub class of Alarm Manager due to it having a private constructor.

会不会有另一个(更好吗?)的方式对我来说,能测试,基于我的测试pre条件我的code正确是加入(或不)报警?

Would there be another (better?) way for me to be able to test that my code correctly is adding (or not) alarms based on my test pre conditions?

推荐答案

有两件事情我能想到的,可以帮助:

Two things I can think of that might help:


  1. 检查报警被手动计划

  1. for checking the alarm has been scheduled manually

亚行外壳dumpsys报警器| grep的com.your.package

检查没有在$ C $集C可以使用Robolectric报警阴影。下面是它正在使用的例子:的http://www.multunus.com/blog/2014/03/tdd-android-using-robolectric-part-3/

for checking there is an alarm set in code you can use Robolectric shadows. Here's an example of it being used: http://www.multunus.com/blog/2014/03/tdd-android-using-robolectric-part-3/

您可以使用了(文章):

You could 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);
    }
}

这篇关于创建用于测试的模拟AlarmManager的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆