为 gcm 编写单元测试

Writing a unit test for gcm

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

我通过创建 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);
    }

}