我想在我的 JUnit 测试中使用来自预定义 xml 文件中内容的响应主体来模拟 RESTEasy 客户端响应。考虑以下 Person 服务客户端 API 和 Person 实体:
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() {
}
}
}
以及 response-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>
如何使用上面的 response-test.xml 文件中的内容模拟下面的 JUnit 测试中的响应主体?
@Test
public void testGetPersons() throws Exception{
PersonServiceClient client = new PersonServiceClient("joe", "doe");
Person[] persons = client.getPersons();
}
我尝试按照这篇文章中的示例进行操作。RESTEasy 是否有客户端模拟框架?但它并没有准确显示如何选择响应正文。