我正在使用GCM(Google Cloud Messaging)向Android应用发送通知。我的服务器使用由Google提供的gcm-server.jar
,并遵循文档。我能够无问题地向设备发送通知。
现在,我正在尝试基于来自PushNotificationDaoGcmImpl
的私有方法sendNotificationToServer
返回的Result
(来源)进行业务逻辑的单元测试。
我知道Result
不能被Mock,因为它是一个final
类,而简单实例化new Result()
也不起作用,因为没有公共构造函数。 Result
内部的Builder
类无法在包外访问,因此我无法以这种方式构建对象。
Result
,这是从Sender
(source)返回的内容。我的问题是,我该如何对
PushNotificationDaoGcmImpl
进行单元测试,以确保它能够处理基于Result
的某些条件?public class PushNotificationDaoGcmImpl{
//method I'm trying to test
public void sendPushNotification(){
// builds message to send
Result result = this.sendNotificationToServer(gcmMessage, deviceToken)
//handle result's error conditions
}
//method call I'm trying to mock
private Result sendNotificationToServer(Message gcmMessage, String deviceToken){
return gcmSender.send(gcmMessage, deviceToken, 1);
}
}
//test snipet
@Test
public void testSendNotificationToServer () throws Exception {
Result result = new Result();
PushNotificationMessage message = new PushNotificationMessage("123", "Test", "deviceToken", "Android");
//Having issue with how to handle Result here
doReturn(result).when(gcmPushNotificationDaoSpy).sendNotificationToServer(any(Message.class), anyString());
PushNotificationResult result = gcmPushNotificationDaoSpy.sendPushNotification(message);
//verify business logic was correct based on Result
}