在 Java 类中进行模拟

发布于 2024-08-01 15:38:12 字数 1417 浏览 12 评论 0原文

所以我有这个 GWT 代码来处理 RPC 请求并维护状态(就绪、等待、错误等)。 我想检查该类在每次调用后是否正确更改其状态,设置响应变量等。

现在我应该如何继续测试而不向服务器发出实际请求(这可能会在服务器本身中遇到错误)。

我想我可以以某种方式模拟请求回调类,但它对测试来说是不可见的。

我迷路了,救命!

下面的代码示例(如果有人需要,我稍后会发布整个内容)。

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.
  }

}

So I have this GWT code that handles RPC requests maintain states(ready, waiting, error etc).
And I would like to check if the class change its states correctly after each call, set response variables etc.

Now how should I proceed to test that without making actual requests to the server(that could run into errors in the server it self).

I think I could mock the request callback class somehow but it is invisible to the test.

I'm lost, help!

Sample of the code below(I'll post the whole thing later in case anyone wants).

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.
  }

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

捂风挽笑 2024-08-08 15:38:12

看起来您正在尝试模拟实际的传输,因此您应该构建 RequestBuilder 类的模拟。 在 JMockit 中,您可以这样写:

public class MockRequestBuilder
{
   public void $init( int method, String url)
   {
     /* check values and/or store for later */
   }

   public Request sendRequest( String data, RequestCallback callback )
   {
     /* check values and/or store for later */
   }
}

您需要填写您希望模拟执行的操作的详细信息。 此外,如果将回调移动到外部类内部的命名类实例,则可以隔离回调测试:

public class MyGWTClass
{
   protected static class RpcCallback extends RequestCallback
   {
      public void onError(...) { ... }
   }
}

通过将回调对象移动到类中并使用工厂方法,您可以创建仅检查回调的测试。

It looks like you're trying to mock out the actual transport so you should build a mock of the RequestBuilder class. In JMockit, you could write:

public class MockRequestBuilder
{
   public void $init( int method, String url)
   {
     /* check values and/or store for later */
   }

   public Request sendRequest( String data, RequestCallback callback )
   {
     /* check values and/or store for later */
   }
}

You'll need to fill in the details of the what you want the mock to do. Also, you can isolate the callback testing if you moved the callback to a named class instance inside of your outer class:

public class MyGWTClass
{
   protected static class RpcCallback extends RequestCallback
   {
      public void onError(...) { ... }
   }
}

By moving the callback object into a class and using a factory method, you can create tests that only check the callback.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文