我有这些GWT服务器类:
AbstractServiceImpl.class
package org.foxclient.gwt.server;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.api.client.config.ClientConfig;
import com.sun.jersey.api.client.config.DefaultClientConfig;
import com.sun.jersey.api.json.JSONConfiguration;
import org.foxclient.gwt.client.answer.DataEvent;
import org.foxclient.gwt.client.service.GenericService;
import javax.ws.rs.core.MediaType;
public abstract class AbstractServiceImpl<T, F> extends RemoteServiceServlet implements GenericService<T, F> {
public abstract String getRequestURL();
public abstract Integer getId(T object);
public DataEvent delete(T object) {
DataEvent event = new DataEvent();
if ((object != null) && (getId(object) != null)) {
Client client = Client.create();
WebResource webResource = client.resource(getRequestURL() + "/" + getId(object));
ClientResponse response = webResource.delete(ClientResponse.class);
event.setStatus(response.getStatus());
event.setMessage("Success");
} else {
event.setStatus(404);
event.setMessage("Not found");
}
return event;
}
@Override
public DataEvent update(T object) {
DataEvent event = new DataEvent();
if ((object != null) && (getId(object) != null)) {
ClientConfig clientConfig = new DefaultClientConfig();
clientConfig.getFeatures().put(JSONConfiguration.FEATURE_POJO_MAPPING, Boolean.TRUE);
Client client = Client.create(clientConfig);
WebResource webResource = client.resource(getRequestURL() + "/" + getId(object));
ClientResponse response = webResource.type(MediaType.APPLICATION_JSON).
put(ClientResponse.class, object);
event.setStatus(response.getStatus());
event.setMessage("Success");
} else {
event.setStatus(404);
event.setMessage("Not found");
}
return event;
}
@Override
public DataEvent add(T object) {
DataEvent event = new DataEvent();
if (object != null) {
ClientConfig clientConfig = new DefaultClientConfig();
clientConfig.getFeatures().put(JSONConfiguration.FEATURE_POJO_MAPPING, Boolean.TRUE);
Client client = Client.create(clientConfig);
WebResource webResource = client.resource(getRequestURL());
ClientResponse response = webResource.type(MediaType.APPLICATION_JSON).
post(ClientResponse.class, object);
if ((response.getStatus() == 200)) {
event.setStatus(200);
event.setMessage("Success");
} else {
event.setStatus(response.getStatus());
event.setMessage("Some error");
}
} else {
event.setStatus(400);
event.setMessage("No data");
}
return event;
}
}
DepartmentServiceImpl.java
package org.foxclient.gwt.server;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.GenericType;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.api.client.config.ClientConfig;
import com.sun.jersey.api.client.config.DefaultClientConfig;
import com.sun.jersey.api.json.JSONConfiguration;
import org.foxclient.gwt.client.answer.DepartmentListEvent;
import org.foxclient.gwt.client.answer.DataEvent;
import org.foxclient.gwt.client.entity.Department;
import org.foxclient.gwt.client.entity.Filter;
import org.foxclient.gwt.client.service.DepartmentService;
import java.util.List;
public class DepartmentServiceImpl extends AbstractServiceImpl<Department, Filter> implements DepartmentService {
public String getRequestURL() {
return "http://localhost:8080/foxrestful/rest/departments";
}
@Override
public Integer getId(Department object) {
return object.getId();
}
public DataEvent getAll() {
ClientConfig clientConfig = new DefaultClientConfig();
clientConfig.getFeatures().put(JSONConfiguration.FEATURE_POJO_MAPPING, Boolean.TRUE);
Client client = Client.create(clientConfig);
WebResource resource = client.resource(getRequestURL());
ClientResponse response = resource.get(ClientResponse.class);
DepartmentListEvent event = new DepartmentListEvent();
if ((response.getStatus() == 200) && response.hasEntity()) {
event.setStatus(200);
event.setDepartments(response.getEntity(new GenericType<List<Department>>() { }));
} else {
event.setStatus(response.getStatus());
event.setMessage("Some error");
}
return event;
}
@Override
public DataEvent getFiltered(Filter filter) {
return null;
}
}
我想要测试方法 getAll(),删除(T对象)等等。但是他们调用远程REST服务,我无法拦截此调用。另外,我认为,我无法模拟WebResource,因为它是我想要测试的方法的内在逻辑。也许我可以拿起REST服务器并重定向到它?
有什么想法可以测试这些方法吗?
我使用GWT 2.7.0作为远程REST服务的客户端(由Jersey 1.19编写)。另外,正如您所看到的,我使用Jersey客户端来获取服务响应。
谢谢!
答案 0 :(得分:1)
getAll(), delete(T object)
这两种方法都是网络相关的方法,所以你需要实现async
操作来测试网络相关的操作
理想情况下,它应该用于需要几秒钟的操作。有些任务会使线程长时间运行,因此在这种情况下,建议使用java.util.concurrent包,如Executor,ThreadPoolExecutor和FutureTask。