Hi all,
Today, i have new a post How to write Unit Test for method GET() and POST() using JARX-RS technical.
Especially, POST one PayLoad with JSON type and validate this PayLoad is correct or incorrect.
NOTE: you have to extend JerseyTest
package com.sample.common.endpoint;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import org.apache.log4j.Logger;
import org.junit.Assert;
import org.junit.Test;
import org.springframework.web.context.ContextLoaderListener;
import org.springframework.web.context.request.RequestContextListener;
import com.sample.common.domain.Error;
import com.sample.common.domain.Users;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.spi.spring.container.servlet.SpringServlet;
import com.sun.jersey.test.framework.JerseyTest;
import com.sun.jersey.test.framework.WebAppDescriptor;
public class UserEndPointTest extends JerseyTest {
private final Logger log = Logger.getLogger(this.getClass());
public UserEndPointTest() throws Exception {
super(
new WebAppDescriptor.Builder(
"com.sun.jersey.samples.springannotations.resources.jerseymanaged")
.contextPath("spring-jersey-validation-payload")
.contextParam("contextConfigLocation",
"classpath:applicationContext_TEST.xml")
.servletClass(SpringServlet.class)
.contextListenerClass(ContextLoaderListener.class)
.requestListenerClass(RequestContextListener.class)
.initParam("com.sun.jersey.config.feature.Trace",
"true")
.initParam("jersey.config.server.provider.classnames",
"org.glassfish.jersey.filter.LoggingFilter")
.initParam(
"com.sun.jersey.spi.container.ContainerRequestFilters",
"com.sun.jersey.api.container.filter.LoggingFilter")
.initParam(
"com.sun.jersey.spi.container.ContainerResponseFilters",
"com.sun.jersey.api.container.filter.LoggingFilter")
.initParam("com.sun.jersey.config.property.packages",
" com.fasterxml.jackson.jaxrs.json")
.initParam(
"com.sun.jersey.api.json.POJOMappingFeature",
"true").build());
log.debug("In UserEndPointTest constructor.");
}
@Test
public void testGetUserJSON() throws Exception {
WebResource webResource = resource();
ClientResponse returnJSON = webResource.path("service")
.type(MediaType.APPLICATION_JSON).get(ClientResponse.class);
Users users = returnJSON.getEntity(Users.class);
Assert.assertEquals(2, users.getUser().size());
}
@Test
public void testGetUserXML() throws Exception {
WebResource webResource = resource();
ClientResponse returnXML = webResource.path("service")
.type(MediaType.APPLICATION_XML).get(ClientResponse.class);
Users users = returnXML.getEntity(Users.class);
Assert.assertEquals(2, users.getUser().size());
}
@Test
public void testAddUserJSONSuccess() throws Exception {
WebResource webResource = resource();
String jsonPayload = "{\"user\":[{\"userId\":\"userid1\",\"birthDate\":1393405118626}]}";
ClientResponse returnJSON = webResource.path("service")
.type(MediaType.APPLICATION_JSON)
.post(ClientResponse.class, jsonPayload);
Assert.assertEquals(Response.Status.OK.getStatusCode(),
returnJSON.getStatus());
}
@Test
public void testAddUserJSONAHalf() throws Exception {
WebResource webResource = resource();
String jsonPayload = "{\"user\":[{\"userId\":\"userid1\",\"birthDate\":1393409642708},{\"userId\":null,\"birthDate\":null}]}";
ClientResponse returnJSON = webResource.path("service")
.type(MediaType.APPLICATION_JSON)
.post(ClientResponse.class, jsonPayload);
com.sample.common.domain.Error error = returnJSON
.getEntity(Error.class);
Assert.assertEquals("Add one user Fail", 1, error.getFailureCount());
Assert.assertEquals("Add one user Success", 1, error.getSuccessCount());
Assert.assertEquals("Add a half user", error.getMessage());
}
@Test
public void testAddUserJSONFailCompletely() throws Exception {
WebResource webResource = resource();
String jsonPayload = "{\"user\":[{\"userId\":null,\"birthDate\":null}]}";
ClientResponse returnJSON = webResource.path("service")
.type(MediaType.APPLICATION_JSON)
.post(ClientResponse.class, jsonPayload);
com.sample.common.domain.Error error = returnJSON
.getEntity(Error.class);
Assert.assertEquals("Add one user Fail", 1, error.getFailureCount());
Assert.assertEquals("Add User Failure Completely", error.getMessage());
}
}
Source Code