Jersey客户端API vs Jersey测试框架

编程入门 行业动态 更新时间:2024-10-19 23:26:41
本文介绍了Jersey客户端API vs Jersey测试框架的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我是网络服务的新手,想知道Jersey客户端API和球衣测试框架有什么区别?

I am new to web services and would like to know what is the difference between Jersey client API and jersey test framework?

我想测试一下我的球衣REST网络服务端点。哪个是正确的?

I would like to test my jersey REST web-services end points. Which is the the right one to use?

推荐答案

那里有很多HTTP客户端API(例如 Apache HttpClient )。您 需要一个人进行客户端测试。我们需要以某种方式通过HTTP访问我们的服务,因此单元测试需要其中一个API。由于您已经在使用Jersey, Jersey Client API 是一个不错的选择。示例可能类似于

There are many HTTP client APIs out there (for example Apache HttpClient). You will need one to do client side testing. We will need to somehow access our services through HTTP, so one of these APIs will be needed for unit testing. Since you're already using Jersey, the Jersey Client API is a good choice. An example might look something like

final String url = "stackoverflow/questions/27160440/" + "jersey-client-api-vs-jersey-test-framework"; Client client = ClientBuilder.newClient(); WebTarget target = client.target(url); Response response = target.request().accept(MediaType..get(); String html = response.readEntity(String.class); System.out.println(html); response.close();

正如您所看到的,客户端API没有调用我们的服务。它只是HTTP调用的接口,具有Rest功能。如果我们想运行自己的服务,我们首先需要将它们部署到容器中,无论是否满吹制服务器/容器,或一些嵌入式变体。没有框架,完整的测试可能看起来像

As you can see the client API does not have to be calling our services. It is just interface to HTTP calls, with "Rest" features. If we wanted to run our own services, we would first need to deploy them to a container, whether a full blown server/container, or some embedded variant. Without a framework, a full test might look something like

<dependencies> <dependency> <groupId>org.glassfish.jersey.containers</groupId> <artifactId>jersey-container-grizzly2-http</artifactId> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.9</version> <scope>test</scope> </dependency> </dependencies> public class SimpleTest { static final String BASE_URI = "localhost:8080/myapp/"; static HttpServer server; static Client client; private static HttpServer startServer() { final ResourceConfig resourceConfig = new ResourceConfig() .packages("where.my.resources.are") .register(HelloResource.class); // normally the resource class would not be in the unit test class // and would be in the `where.my.resources.are` package pr sub package return GrizzlyHttpServerFactory.createHttpServer(URI.create(BASE_URI), resourceConfig); } @Path("hello") public static class HelloResource { @GET public String getHello() { return "Hello World!"; } } @BeforeClass public static void setUpClass() { server = startServer(); client = ClientBuilder.newClient(); } @AfterClass public static void tearDownClass() { server.shutdown(); } @Test public void test() { WebTarget target = client.target(BASE_URI); Response response = target.path("hello").request().get(); String hello = response.readEntity(String.class); assertEquals("Hello World!", hello); response.close(); } }

Jersey测试框架允许我们更轻松地进行半集成/集成测试,并提供更复杂的容器部署选项。可以将服务启动到轻量级嵌入式容器(也是不同类型)中,这些容器将在运行单元测试时自动启动和停止。该框架实际上依赖于Jersey Client API,因此如果您使用该框架,则可以在测试用例中使用Client API。一个例子

The Jersey Test Framework allows us to do semi-integration/integration testing more easily, with options for more complex container deployments. The services can be launched into a lightweight embedded container (also different types) which will auto start and stop on running the unit tests. The framework is actually dependent on the Jersey Client API, so if you are using the framework, then you can use the Client API in your test cases. An example

<dependencies> <dependency> <groupId>org.glassfish.jersey.test-framework.providers</groupId> <artifactId>jersey-test-framework-provider-grizzly2</artifactId> <version>2.13</version> </dependency> </dependencies> public class SimpleTest extends JerseyTest { @Path("hello") public static class HelloResource { @GET public String getHello() { return "Hello World!"; } } @Override protected Application configure() { return new ResourceConfig(HelloResource.class); } @Test public void test() { Response response = target("hello").request().get(); String hello = response.readEntity(String.class); assertEquals("Hello World!", hello); response.close(); } }

你可以看到相似之处, @测试。那是因为我们正在使用客户端API。我们不需要显式配置客户端,因为框架为我们做了这个。

You can see similarities, the @Test. That's because, we are making use of the client API. We don't need to explicitly configure the Client, as the framework does this for us.

所以选择真的归结为你是否想要使用Test框架。无论哪种方式,您都应该知道如何使用Jersey Client API,因为您将以任何一种方式使用它(除非您决定使用其他HTTTP客户端API,如HttpClient)

So the choice really comes down to whether or not you want to use the Test framework. Either way you should know how to use the Jersey Client API, as you will be using it either way (that is unless you decide to just go with another HTTTP client API like HttpClient)

了解更多

  • 客户端API
  • 泽西岛测试框架
  • Client API
  • Jersey Test Framework

更多推荐

Jersey客户端API vs Jersey测试框架

本文发布于:2023-10-31 06:14:04,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1545255.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:客户端   框架   测试   Jersey   API

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!