Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java服务器端单元测试GCM';s结果_Java_Junit_Mockito_Google Cloud Messaging - Fatal编程技术网

Java服务器端单元测试GCM';s结果

Java服务器端单元测试GCM';s结果,java,junit,mockito,google-cloud-messaging,Java,Junit,Mockito,Google Cloud Messaging,我正在使用GCM(谷歌云消息)向Android应用程序发送通知。我的服务器使用的是谷歌提供的gcmserver.jar,我遵循的是。我能够向设备发送无问题通知 现在,我正在尝试基于从PushNotificationDaoGcmImpl的私有方法sendNotificationToServer返回的()对业务逻辑进行单元测试 我知道,Result不能被模拟,因为它是一个final类,简单地将其实例化为newresult()将不起作用,因为没有公共构造函数。ResultinternalBuilder

我正在使用GCM(谷歌云消息)向Android应用程序发送通知。我的服务器使用的是谷歌提供的
gcmserver.jar
,我遵循的是。我能够向设备发送无问题通知

现在,我正在尝试基于从
PushNotificationDaoGcmImpl
的私有方法
sendNotificationToServer
返回的()对业务逻辑进行单元测试

我知道,
Result
不能被模拟,因为它是一个
final
类,简单地将其实例化为
newresult()
将不起作用,因为没有公共构造函数。
Result
internal
Builder
类在
包com.google.android.gcm.server之外不可访问因此我无法以这种方式构建对象

我没有找到一个好方法来创建一个
结果
,它是从()返回的结果

我的问题是如何进行单元测试,使
PushNotificationDaoGcmImpl
根据
结果处理特定条件

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
}

我找到的解决方案是使用反射创建一个
结果
对象。我不确定这是否是一种很好的方法,但我能够基于不同的
结果
错误代码测试业务逻辑

@Test
public void testSendNotificationToServer () throws Exception {
    String successIndicator = null;
    Result result = buildFauxResult("messageId", "deviceToken", successIndicator);

    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
}

public Result buildFauxResult (String messageId, String canonicalRegistrationId, String errorCode) throws Exception {
           Class <?> builderClass = Class.forName("com.google.android.gcm.server.Result$Builder");
           Constructor <?> builderConstructor = builderClass.getDeclaredConstructors()[0];
           ReflectionUtils.makeAccessible(builderConstructor);
           Object builderObject = builderConstructor.newInstance();

           Method canonicalRegistrationIdMethod = builderClass.getMethod("canonicalRegistrationId", String.class);
           ReflectionUtils.makeAccessible(canonicalRegistrationIdMethod);
           builderObject = ReflectionUtils.invokeMethod(canonicalRegistrationIdMethod, builderObject, canonicalRegistrationId);

           Method messageIdMethod = builderClass.getMethod("messageId", String.class);
           ReflectionUtils.makeAccessible(messageIdMethod);
           builderObject = ReflectionUtils.invokeMethod(messageIdMethod, builderObject, messageId);

           Method errorCodeMethod = builderClass.getMethod("errorCode", String.class);
           ReflectionUtils.makeAccessible(errorCodeMethod);
           builderObject = ReflectionUtils.invokeMethod(errorCodeMethod, builderObject, errorCode);

           Method buildMethod = builderClass.getMethod("build");
           ReflectionUtils.makeAccessible(buildMethod);

           return (Result) ReflectionUtils.invokeMethod(buildMethod, builderObject);
    }
@测试
public void testSendNotificationToServer()引发异常{
字符串successIndicator=null;
结果=buildFauxResult(“messageId”、“deviceToken”、successIndicator);
PushNotificationMessage=新的PushNotificationMessage(“123”、“测试”、“deviceToken”、“Android”);
//在这里有关于如何处理结果的问题
doReturn(result).when(gcmpusnotificationdaospy).sendNotificationToServer(any(Message.class),anyString());
PushNotificationResult=gcmPushNotificationDaoSpy.sendPushNotification(消息);
//根据结果验证业务逻辑是否正确
}
公共结果buildFauxResult(字符串messageId、字符串canonicalRegistrationId、字符串errorCode)引发异常{
Class builderClass=Class.forName(“com.google.android.gcm.server.Result$Builder”);
构造函数builderConstructor=builderClass.getDeclaredConstructors()[0];
ReflectionUtils.makeAccessible(builderConstructor);
Object builderObject=builderConstructor.newInstance();
方法canonicalRegistrationIdMethod=builderClass.getMethod(“canonicalRegistrationId”,String.class);
ReflectionUtils.MakeAccessable(canonicalRegistrationIdMethod);
builderObject=ReflectionUtils.invokeMethod(canonicalRegistrationId方法、builderObject、canonicalRegistrationId);
方法messageIdMethod=builderClass.getMethod(“messageId”,String.class);
ReflectionUtils.makeAccessible(messageIdMethod);
builderObject=ReflectionUtils.invokeMethod(messageId方法、builderObject、messageId);
方法errorCodeMethod=builderClass.getMethod(“errorCode”,String.class);
ReflectionUtils.MakeAccessable(errorCodeMethod);
builderObject=ReflectionUtils.invokeMethod(errorCodeMethod,builderObject,errorCode);
方法buildMethod=builderClass.getMethod(“build”);
ReflectionUtils.makeAccessible(buildMethod);
返回(结果)ReflectionUtils.invokeMethod(buildMethod,builderObject);
}

您还可以在com.google.android.gcm.server包中创建一个MockResult类,该类将允许您访问生成器

package com.google.android.gcm.server;

class MockResult {

    public static Result mockResult(...) {
      // Use Builder here to construct Result
    }

}
对我来说,这比处理反射更容易管理