2013-04-06 52 views
0

我想在我的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

回答

0

考慮使用工廠創建ClientRequest,然後模擬工廠返回ClientRequest的模擬。

1

而不是嘲諷的RESTEasy客戶端,我建議嘲諷使用WireMock服務器(免責聲明 - 我寫的): http://wiremock.org/

它通過流利的Java API的可配置從JUnit的範圍內,並運行了一個嵌入式Web服務器提供存根響應,並允許您驗證從您的應用程序發送的請求。

我已經寫的理由在這裏稍微詳細地不是嘲笑HTTP客戶端: Introducing WireMock