Jersey客户端API vs Jersey测试框架 [英] Jersey client API vs Jersey test framework

查看:174
本文介绍了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 = "http://stackoverflow.com/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 = "http://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)

了解更多

  • Client API
  • Jersey Test Framework

这篇关于Jersey客户端API vs Jersey测试框架的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆