Webservice Jersey Test Framework

前言:您的应用程序是否拥有RESTful Web服务?你想确保这些服务是正常工作在一个广泛的容器——无论是重量轻和重的?你有没有觉得需要一个基础设施的设置,您可以使用它来测试你的服务对所有这些容器,而不必担心诸如部署描述符,等等.

I 怎样使用Jersey Test Framework?

要简单的使用框架您也许需要以下几个步骤:
1、添加项目依赖的pom.xml内容

<dependency>
       <groupId>com.sun.jersey.test.framework</groupId>
       <artifactId>jersey-test-framework</artifactId>
       <version>1.0.3</version>
       <scope>test</scope>
 </dependency>
2、新建一个Java类并继承 com.sun.jersey.test.framework.JerseyTest
3、通过super调用父类方法为 JerseyTest设置下面标出的一个或多个参数
super(String rootResourcePackage),super(String contextPath, String servletPath, String resourcePackageName),super().
4、添加测试类的 org.junit.Test annotation注解.
5、从JerseyTest 类处理 com.sun.jersey.api.client.Client 和com.sun.jersey.api.client.WebResource 的实例,创建测试方法的URIs和HTTP请求.
6、将项目部署到web容器.
7、启动测试类需要测试的方法.

II 测试实例Java代码

package com.hascode.tutorial.rest;
 
import static org.junit.Assert.assertEquals;
import java.net.URISyntaxException;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;
import org.junit.Test;
import com.sun.jersey.api.client.UniformInterfaceException;
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;
 
public class UserServiceTestUsingJerseyTestFramework extends JerseyTest {
	@Override
	protected AppDescriptor configure() {
		return new WebAppDescriptor.Builder().build();
	}
 
	@Test
	public void testUserFetchesSuccess() throws JSONException,
			URISyntaxException {
		WebResource webResource = client().resource("http://localhost:8080/");
		JSONObject json = webResource.path("/rest-test-tutorial/user/id/12")
				.get(JSONObject.class);
		assertEquals("12", json.get("id"));
		assertEquals("Tim", json.get("firstName"));
		assertEquals("Tester", json.get("lastName"));
		assertEquals("1970-01-16T07:56:49.871+01:00", json.get("birthday"));
	}
 
	@Test(expected = UniformInterfaceException.class)
	public void testUserNotFound() {
		WebResource webResource = client().resource("http://localhost:8080/");
		JSONObject json = webResource.path("/rest-test-tutorial/user/id/666").get(JSONObject.class);
	}
}
III 你使用该框架做过任何简单的实例吗?
以下是由此框架改变出来的项目:

From:https://blogs.oracle.com/naresh/entry/jersey_test_framework_makes_it (Oracle sun blog上资料)

http://www.hascode.com/2011/09/rest-assured-vs-jersey-test-framework-testing-your-restful-web-services/ (该链接包含一个完整的REST webservice测试示例,并使用Maven构建项目)


转载于:https://my.oschina.net/boonya/blog/135186

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值