如何在客户端模拟REST服务响应?

如何在客户端模拟REST服务响应?

问题描述:

我想在我的JUnit测试中用预定义xml文件中的内容响应body来模拟RESTEasy客户端响应。考虑下面的人服务客户端API和Person实体:如何在客户端模拟REST服务响应?

package my.company.com; 

import java.net.URI; 

import javax.xml.bind.annotation.XmlAccessType; 
import javax.xml.bind.annotation.XmlAccessorType; 
import javax.xml.bind.annotation.XmlRootElement; 

import org.apache.http.auth.AuthScope; 
import org.apache.http.auth.Credentials; 
import org.apache.http.auth.UsernamePasswordCredentials; 
import org.apache.http.client.CookieStore; 
import org.apache.http.client.protocol.ClientContext; 
import org.apache.http.impl.client.BasicCookieStore; 
import org.apache.http.impl.client.DefaultHttpClient; 
import org.apache.http.protocol.BasicHttpContext; 
import org.apache.http.protocol.HttpContext; 
import org.jboss.resteasy.client.ClientRequest; 
import org.jboss.resteasy.client.ClientResponse; 
import org.jboss.resteasy.client.core.executors.ApacheHttpClient4Executor; 

public class PersonServiceClient { 
    private final DefaultHttpClient httpClient; 

public PersonServiceClient(String username, String password) { 
    Credentials credentials = new UsernamePasswordCredentials(username, password); 
    httpClient = new DefaultHttpClient(); 
    httpClient.getCredentialsProvider().setCredentials(AuthScope.ANY, credentials); 
} 

public Person[] getPersons() throws Exception 
{ 
    URI url = new URI("http://www.mycompany.com/persons/"); 
    Person[] persons = getByRest(url, Person[].class); 
    return persons; 
} 

private <T> T getByRest(URI url, Class<T> returnType) throws Exception { 
    ClientRequest client = createClientRequest(url.toString()); 
    ClientResponse<T> response = client.get(returnType); 
    return response.getEntity(); 
} 

private ClientRequest createClientRequest(String url) { 
    // Storing cookie to avoid creating new client for every call 
    CookieStore cookieStore = new BasicCookieStore(); 
    HttpContext httpContext = new BasicHttpContext(); 
    httpContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore); 
    ApacheHttpClient4Executor clientExecutor = new ApacheHttpClient4Executor(httpClient, httpContext); 

    ClientRequest clientRequest = new ClientRequest(url, clientExecutor); 
    return clientRequest; 
} 

@XmlRootElement(name = "resource") 
@XmlAccessorType(XmlAccessType.FIELD) 
public class Person { 
    private String type; 
    private String name; 
    private String addres; 
    private String phone; 

    public String getType() { 
     return type; 
    } 

    public void setType(String type) { 
     this.type= type; 
    } 

    public String getName() { 
     return name; 
    } 

    public void setName(String name) { 
     this.name = name; 
    } 

    public String getAddres() { 
     return addres; 
    } 

    public void setAddres(String addres) { 
     this.addres = addres; 
    } 

    public String getPhone() { 
     return phone; 
    } 

    public void setPhone(String phone) { 
     this.phone = phone; 
    } 

    public Person() { 
    } 
} 
} 

和响应test1.xml的内容:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?> 
<collection> 
    <resource> 
     <type>Peson</type> 
     <name>Christopher Monroe</name> 
     <addres>Wall Street 2</addres> 
     <phone>12345678</<phone> 
    </resource> 
    <resource> 
     <type>Person</type> 
     <name>John Dee</name> 
     <addres>Down town 2</addres> 
     <phone>2997562123</phone> 
    </resource> 
</collection> 

我怎么能嘲笑响应的主体在JUnit测试下面的响应内容-test.xml文件上面?

@Test 
public void testGetPersons() throws Exception{ 
    PersonServiceClient client = new PersonServiceClient("joe", "doe"); 
    Person[] persons = client.getPersons(); 
} 

我试图按照在这个岗位Is there a client-side mock framework for RESTEasy?的例子,但它并不显示究竟如何选择响应体。

+0

你有没有敲定任何解决方案还ISMAR? – 2013-10-01 15:46:06

考虑使用工厂创建ClientRequest,然后模拟工厂返回ClientRequest的模拟。

而不是嘲讽的RESTEasy客户端,我建议嘲讽使用WireMock服务器(免责声明 - 我写的): http://wiremock.org/

它通过流利的Java API的可配置从JUnit的范围内,并运行了一个嵌入式Web服务器提供存根响应,并允许您验证从您的应用程序发送的请求。

我已经写的理由在这里稍微详细地不是嘲笑HTTP客户端: Introducing WireMock