微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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),@R_502_4406@.
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构建项目)

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐