Webservice Jersey Test Framework
发布时间:2020-12-17 00:17:40 所属栏目:安全 来源:网络整理
导读:前言:您的应用程序是否拥有RESTful Web服务?你想确保这些服务是正常工作在一个广泛的容器——无论是重量轻和重的? 你有没有觉得需要一个基础设施的设置,您可以使用它来测试你的服务对所有这些容器,而不必担心诸如部署描述符,等等. I 怎样使用Jersey Test Fr
前言:您的应用程序是否拥有RESTful Web服务?你想确保这些服务是正常工作在一个广泛的容器——无论是重量轻和重的?你有没有觉得需要一个基础设施的设置,您可以使用它来测试你的服务对所有这些容器,而不必担心诸如部署描述符,等等. <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构建项目) (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |