java Jersey 客户端 API 与 Jersey 测试框架

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27160440/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 11:20:14  来源:igfitidea点击:

Jersey client API vs Jersey test framework

javarestjerseyjersey-clientjersey-test-framework

提问by user1247412

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

我是 Web 服务的新手,想知道 Jersey 客户端 API 和 jersey 测试框架之间有什么区别?

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

我想测试我的 jersey REST web 服务端点。哪个是正确使用的?

回答by Paul Samsotha

There are many HTTP client APIs out there (for example Apache HttpClient). You willneed 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 APIis a good choice. An example might look something like

有许多 HTTP 客户端 API(例如Apache HttpClient)。您需要一个来进行客户端测试。我们需要以某种方式通过 HTTP 访问我们的服务,因此单元测试将需要这些 API 之一。由于您已经在使用 Jersey,因此Jersey Client API是一个不错的选择。一个例子可能看起来像

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();

As you can see the client API does not have to be calling ourservices. 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

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

<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();
    }
}

The Jersey Test Frameworkallows 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

新泽西测试框架允许我们做半整合/集成测试更容易,以用于更复杂的容器的部署选项。这些服务可以启动到一个轻量级的嵌入式容器(也有不同的类型)中,它会在运行单元测试时自动启动和停止。该框架实际上依赖于 Jersey Client API,因此如果您正在使用该框架,那么您可以在您的测试用例中使用 Client API。一个例子

<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();
    }
}

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. 那是因为,我们正在使用客户端 API。我们不需要显式配置Client,因为框架会为我们做这件事。

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)

因此,选择实际上取决于您是否要使用测试框架。无论哪种方式,您都应该知道如何使用 Jersey 客户端 API,因为您将以任何一种方式使用它(除非您决定只使用另一个 HTTTP 客户端 API,如 HttpClient)



Read More

阅读更多