Reputation: 83
I am trying to write a test case in Jersey Test Framework for an authentication service as shown below:
Service code:
@Path("/users")
@Produces(MediaType.APPLICATION_JSON)
public class AuthenticationServices {
@POST
@Path("/login")
public Response login(LoginRequest loginRequest, @Context HttpServletRequest request) {
......
}
Jerysey Test code:
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);
}
}
When trying to execute the above test, i am getting the below exception:
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)
I am new to jersey test. Not sure y this is happening. Please help.
EDIT: added @consumes annotation above the method.
Upvotes: 2
Views: 2266
Reputation: 208994
You're using Jersey 1.x test, but Entity
was only introduced in JAX-RS 2.x, i.e. Jersey 2.x. Jersey 1.x has no idea how to handle it.
Instead just post the object itself, without any wrapping Entity
, and set it's content type through the WebResource#type(..)
method.
LoginRequest loginReq = new LoginRequest();
loginReq.setUserId("admin");
loginReq.setPassword("a");
ClientResponse resp = webResource.path(path)
.type(MediaType.APPLICATION_JSON)
.post(ClientResponse.class, loginReq);
As an aside, if you are using Jersey 1.x, then you should get rid of any JAX-RS 2.0 dependencies you have, so you don't get confused in what you're using.
Upvotes: 2