gpt4 book ai didi

java - 如何测试调用 REST 服务的 GWT 服务器方法?

转载 作者:行者123 更新时间:2023-11-30 11:05:16 24 4
gpt4 key购买 nike

我有这些 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()、delete(T object) 等。但是它们调用远程 REST 服务,我无法拦截此调用。另外,我认为,我不能模拟 WebResource,因为它是我想要测试的方法的内部逻辑。也许我可以选择 REST 服务器并重定向到它?

有什么想法可以测试这些方法吗?

我使用 GWT 2.7.0 作为远程 REST 服务的客户端(由 Jersey 1.19 编写)。另外,如您所见,我使用 Jersey 客户端从服务获取响应。

谢谢!

最佳答案

getAll(), delete(T object)这两个方法是网络相关的方法所以你需要实现async操作来测试网络相关操作

理想情况下,它应该用于需要几秒钟的操作。有些任务会保持线程长时间运行,因此在这种情况下,建议使用 java.util.concurrent 包,例如 Executor、ThreadPoolExecutor 和 FutureTask。

关于java - 如何测试调用 REST 服务的 GWT 服务器方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29705140/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com