是否有RESTEasy的客户端模拟框架? [英] Is there a client-side mock framework for RESTEasy?

查看:104
本文介绍了是否有RESTEasy的客户端模拟框架?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

RESTEasy提供服务器端模拟框架用于模拟服务器请求。是否有相应的单元测试客户端框架

RESTEasy provides the Server-side Mock Framework for mocking server requests. Is there an equivalent for unit testing the client framework?

InMemoryClientExecutor ?我无法找到文档以及如何使用这个类的示例。

Is InMemoryClientExecutor intended for this purpose? I'm having trouble finding documentation and examples of how this class should be used.

推荐答案

看起来像 InMemoryClientExecutor 可用于客户端模拟。查看 source ,它在内部使用与服务器端模拟框架相同的类,即 MockHttpRequest MockHttpResponse

Looks like InMemoryClientExecutor may be used for client-side mocking. Looking in the source, it internally uses the same classes as the server-side mock framework, namely, MockHttpRequest and MockHttpResponse.

InMemoryClientExecutor 为您提供能够覆盖 createResponse 用于模拟响应,并且还有一个构造函数,它需要一个 Dispatcher ,如果你想自定义和拦截通过这种方式调用。

InMemoryClientExecutor gives you the ability to override createResponse for mocking responses and also has a constructor which takes a Dispatcher, if you want to customize and intercept calls that way.

这是一个利用客户端框架示例,

import javax.ws.rs.*;
import javax.ws.rs.core.Response.*;

import org.jboss.resteasy.client.*;
import org.jboss.resteasy.client.core.*;
import org.jboss.resteasy.client.core.executors.*;
import org.jboss.resteasy.mock.*;
import org.jboss.resteasy.plugins.providers.*;
import org.jboss.resteasy.spi.*;

public class InMemoryClientExecutorExample {
    public interface SimpleClient {
       @GET
       @Path("basic")
       @Produces("text/plain")
       String getBasic();

       @PUT
       @Path("basic")
       @Consumes("text/plain")
       void putBasic(String body);

       @GET
       @Path("queryParam")
       @Produces("text/plain")
       String getQueryParam(@QueryParam("param")String param);

       @GET
       @Path("matrixParam")
       @Produces("text/plain")
       String getMatrixParam(@MatrixParam("param")String param);

       @GET
       @Path("uriParam/{param}")
       @Produces("text/plain")
       int getUriParam(@PathParam("param")int param);
    }  

    public static void main(String[] args) {
        RegisterBuiltin.register(ResteasyProviderFactory.getInstance());

        ClientExecutor executor = new InMemoryClientExecutor() {
            @Override
            protected BaseClientResponse<?> createResponse(ClientRequest request, MockHttpResponse mockResponse) {
                try {                    
                    System.out.println("Client requesting " + request.getHttpMethod() + " on " + request.getUri());
                }
                catch(Exception ex) {
                    ex.printStackTrace();
                }
                mockResponse.setStatus(Status.OK.getStatusCode());
                return super.createResponse(request, mockResponse);
            }
        };

        SimpleClient client = ProxyFactory.create(SimpleClient.class, "http://localhost:8081", executor);
        client.putBasic("hello world");
    }
}

这篇关于是否有RESTEasy的客户端模拟框架?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆