mockserver中已经提供了HTTP/HESSIAN协议的mock,性能测试方提出来没有实现httpinvoker协议的mock粗略了解了httpinvoker 的服务暴露及调用方式,简单实现如下:
1、在mockserver端暴露一个servlet 用于替换原httpinvoker调用地址
2、封装HttpInvokerServiceExt继承HttpInvokerServiceExporter
主要重新实现invokeAndCreateResult()方法
try {
String reqUrl = request.getPathInfo().replaceFirst("/httpinvoke/", "/");
RemoteInvocation invocation = readRemoteInvocation(request); //从request 中读取序列化对象
RemoteInvocationResult result = invokeAndCreateResult(invocation, reqUrl);
writeRemoteInvocationResult(request, response, result);
} catch (ClassNotFoundException ex) {
throw new NestedServletException("Class not found during deserialization", ex);
}
3、invokeAndCreateResult中主要调用自己mock规则进行结果匹配替换
// 查询mock信息是否存在
String methodName = invocation.getMethodName();
Class<?>[] paramClasses = invocation.getParameterTypes();
Object value = null;
try {
value = mockService.mockInvoke(reqUrl, methodName, paramClasses);
} catch (ClassNotFoundException e) {
e.printStackTrace();
return null;
}catch (NoSuchMethodException nse){
nse.printStackTrace();
return null;
}
return new RemoteInvocationResult(value);
}
经性能测试妹子测试基本可用