我正在尝试在 Jersey 测试框架中为身份验证服务编写一个测试用例,如下所示:
服务代码:
@Path("/users")
@Produces(MediaType.APPLICATION_JSON)
public class AuthenticationServices {
@POST
@Path("/login")
public Response login(LoginRequest loginRequest, @Context HttpServletRequest request) {
......
}
杰里西测试代码:
import java.net.URISyntaxException;
import javax.ws.rs.client.Entity;
import javax.ws.rs.core.MediaType;
import org.json.JSONException;
import org.junit.Test;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.test.framework.AppDescriptor;
import com.sun.jersey.test.framework.JerseyTest;
import com.sun.jersey.test.framework.WebAppDescriptor;
import com.vs.ant.sensordata.request.LoginRequest;
public class AuthenticationTest extends JerseyTest{
@Override
protected AppDescriptor configure() {
return new WebAppDescriptor.Builder("com.vs.ant.sensordata.services")
.contextPath("anttailbigdata")
.build();
}
@Test
@Consumes("application/json")
public void testLogin() throws JSONException,URISyntaxException {
WebResource webResource = client().resource("http://localhost:8082/");
String path = "SensorData/a/users/login";
LoginRequest loginReq = new LoginRequest();
loginReq.setUserId("admin");
loginReq.setPassword("a");
Entity<LoginRequest> loginEntity = Entity.entity(loginReq, MediaType.APPLICATION_JSON);
ClientResponse resp = webResource.path(path).post(ClientResponse.class, loginEntity);
}
}
尝试执行上述测试时,出现以下异常:
com.sun.jersey.api.client.ClientHandlerException:
com.sun.jersey.api.client.ClientHandlerException: A message body writer for Java type, class javax.ws.rs.client.Entity, and MIME media type, application/octet-stream, was not found
at com.sun.jersey.client.urlconnection.URLConnectionClientHandler.handle(URLConnectionClientHandler.java:149)
at com.sun.jersey.api.client.Client.handle(Client.java:648)
at com.sun.jersey.api.client.WebResource.handle(WebResource.java:670)
at com.sun.jersey.api.client.WebResource.post(WebResource.java:251)
at com.vs.ant.sensordata.services.AuthenticationTest.testLogin(AuthenticationTest.java:37)
我是球衣测试的新手。不确定这是否正在发生。请帮忙。
编辑:在方法上方添加了 @consumes 注释。