单元测试接收器类

时间:2012-03-21 06:14:56

标签: android unit-testing junit broadcastreceiver

这是我的广播接收器类实现报警。我想对这个类进行单元测试。

public class AlarmReceive extends BroadcastReceiver{
//DeviceIntelligence di;
Registration rs;
ServerUpload su;

@Override
public void onReceive(Context context, Intent intent) {
    rs = new Registration();
    su = new ServerUpload();

    if (intent.getAction().equals("APP_LIST_INSERT_SIGNAL")) {

    }

    Log.d("operator", "alarm action: " + intent.getAction());
    if(intent.getAction().equals("SERVER_UPLOAD_SIGNAL_PER_HOUR")){
        rs.uploadServerData(context);
    }

    if(intent.getAction().equals("SERVER_UPLOAD_SIGNAL_PER_DAY")){
        rs.uploadServerData(context);
        if(MainService.dh.selectAll().size() != 0){
            MainService.dh.deleteAll("TABLE_NAME");
        }

        if(Registration.ud.select().size() != 0){
            Registration.ud.deleteAll();
        }

        if(Registration.wid.selectAll().size() != 0){
            Registration.wid.deleteAll();
        }

        if(Registration.dch.allSelect().size() != 0){
            Registration.dch.deleteAll();
        }
    }
}

}

如何实现我的测试用例来对这个接收器类进行单元测试。我想测试我的接收器类是否正常工作。谢谢你的帮助。

1 个答案:

答案 0 :(得分:0)

您可以使用MockContext“模拟”广播。有关详情,请参阅此处:Unit testing a broadcast receiver?