所以我有这个处理 RPC 请求的 GWT 代码维护状态(准备好、等待、错误等)。而且我想检查类是否在每次调用后正确更改其状态,设置响应变量等。
现在我应该如何在不向服务器发出实际请求的情况下进行测试(这可能会在它自己的服务器中遇到错误)。
我想我可以以某种方式模拟请求回调类,但它对测试是不可见的。
我迷路了,救命!
下面的代码示例(我稍后会发布整个内容以防万一有人想要)。
public class RPCHandler
{
public RPCHandler(String method,String[] argumentsName,
String[][] argumentsValues)
{
this.method = method;
this.argumentsName = argumentsName;
this.argumentsValues = argumentsValues;
}
/**
* Method that creates a RPC request using JSON in a POST
*
*/
public void rpcRequest(){
if(currentState == HandlerState.WAITING_RESPONSE)return;
currentState = HandlerState.WAITING_RESPONSE;
// Append watch list stock symbols to query URL.
url = URL.encode(url);
url += "action=";
url += method;
// Send request to server and catch any errors.
RequestBuilder builder = new RequestBuilder(RequestBuilder.POST, url);
String requestData = parseToJSON(argumentsName, argumentsValues);
try{
Request request = builder.sendRequest(requestData, new RequestCallback()
{
public void onError(Request request, Throwable exception)
{
setRPCException(new Exception("Error while saving. Action="+method));
setCurrentState(HandlerState.ON_ERROR);
}
//Few other error, response received hander methods after this point.
}
}