为gcm编写单元测试

时间:2016-05-08 06:42:28

标签: android google-cloud-messaging android-espresso

我想编写一个单元测试,在我的GcmListenerService收到邮件时开始测试,我尝试绑定到GcmListenerService,但是因为GcmListenerService而不可能覆盖onBind,以便无法覆盖该方法。 从我的测试中启动服务没有意义,因为我希望在从服务器收到消息时调用它。 有任何想法吗 ?

1 个答案:

答案 0 :(得分:2)

我通过创建GCMListenerService对象找到了解决方案,我希望这是正确的方法。

public class mytest{


    private GCMListenerService mReceiver;
    private Context context;

    @Before
    public void init() {
        context = InstrumentationRegistry.getTargetContext();
        mReceiver=new GCMListenerService(context);
    }

    @Test
    public void testGPS() {

        Bundle bundle = new Bundle();
        bundle.putString("message", Constants.GET_LOCATION);
        mReceiver.onMessageReceived("maxim",bundle);
    }

}
相关问题