以下是一組簡單的test case:
- JSR311 interface
- Implementation class
- Test case
代碼:
package com.gss.gmo.cao.restclient;
import java.util.Date;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
@Path("test")
public interface TestResource {
@GET
@Path("getDate")
@Produces(MediaType.APPLICATION_JSON)
Date getDate();
}
import java.util.Date;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
@Path("test")
public interface TestResource {
@GET
@Path("getDate")
@Produces(MediaType.APPLICATION_JSON)
Date getDate();
}
代碼:
package com.gss.gmo.cao.restclient.provider;
import java.util.Date;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import org.joda.time.format.ISODateTimeFormat;
import com.gss.gmo.cao.restclient.TestResource;
@Path("test")
public class TestResourceImpl implements TestResource {
@Override
@GET
@Path("getDate")
public Date getDate() {
return ISODateTimeFormat.dateTime().parseDateTime("2012-12-14T09:21:00.123+08:00").toDate();
}
}
import java.util.Date;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import org.joda.time.format.ISODateTimeFormat;
import com.gss.gmo.cao.restclient.TestResource;
@Path("test")
public class TestResourceImpl implements TestResource {
@Override
@GET
@Path("getDate")
public Date getDate() {
return ISODateTimeFormat.dateTime().parseDateTime("2012-12-14T09:21:00.123+08:00").toDate();
}
}
代碼:
package com.gss.gmo.cao.restclient.provider;
import static junit.framework.Assert.assertEquals;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.apache.cxf.jaxrs.client.JAXRSClientFactory;
import org.joda.time.format.ISODateTimeFormat;
import org.junit.Before;
import org.junit.Test;
import com.gss.gmo.cao.restclient.TestResource;
import com.sun.jersey.test.framework.AppDescriptor;
import com.sun.jersey.test.framework.JerseyTest;
import com.sun.jersey.test.framework.WebAppDescriptor;
public class ResponseExceptionMapperTest extends JerseyTest {
private TestResource testResource;
@Override
protected AppDescriptor configure() {
return new WebAppDescriptor.Builder().initParam("com.sun.jersey.config.property.packages", "com.gss.gmo.cao.restclient.provider").build();
}
@Override
@Before
public void setUp() throws Exception {
super.setUp();
List<Object> providers = new ArrayList<Object>();
providers.add(new GsonMessageBodyProvider());
testResource = JAXRSClientFactory.create("http://localhost:9998", TestResource.class, providers);
}
@Test
public void testGetDate() {
Date date = testResource.getDate();
assertEquals(ISODateTimeFormat.dateTime().parseDateTime("2012-12-14T09:21:00.123+08:00").toDate(), date);
}
}
import static junit.framework.Assert.assertEquals;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.apache.cxf.jaxrs.client.JAXRSClientFactory;
import org.joda.time.format.ISODateTimeFormat;
import org.junit.Before;
import org.junit.Test;
import com.gss.gmo.cao.restclient.TestResource;
import com.sun.jersey.test.framework.AppDescriptor;
import com.sun.jersey.test.framework.JerseyTest;
import com.sun.jersey.test.framework.WebAppDescriptor;
public class ResponseExceptionMapperTest extends JerseyTest {
private TestResource testResource;
@Override
protected AppDescriptor configure() {
return new WebAppDescriptor.Builder().initParam("com.sun.jersey.config.property.packages", "com.gss.gmo.cao.restclient.provider").build();
}
@Override
@Before
public void setUp() throws Exception {
super.setUp();
List<Object> providers = new ArrayList<Object>();
providers.add(new GsonMessageBodyProvider());
testResource = JAXRSClientFactory.create("http://localhost:9998", TestResource.class, providers);
}
@Test
public void testGetDate() {
Date date = testResource.getDate();
assertEquals(ISODateTimeFormat.dateTime().parseDateTime("2012-12-14T09:21:00.123+08:00").toDate(), date);
}
}