簡體   English   中英

Jersey客戶端API vs Jersey測試框架

[英]Jersey client API vs Jersey test framework

我是Web服務的新手,想知道Jersey客戶端API和球衣測試框架有什么區別?

我想測試我的球衣REST網絡服務端點。 哪個是正確的?

有許多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();

如您所見,客戶端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();
    }
}

Jersey測試框架允許我們更輕松地進行半集成/集成測試,並提供更復雜的容器部署選項。 可以將服務啟動到輕量級嵌入式容器(也是不同類型)中,這些容器將在運行單元測試時自動啟動和停止。 該框架實際上依賴於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();
    }
}

你可以看到@Test相似之處。 那是因為我們正在使用客戶端API。 我們不需要顯式配置Client ,因為框架為我們這樣做。

因此,選擇實際上取決於您是否要使用Test框架。 無論哪種方式,您都應該知道如何使用Jersey客戶端API,因為您將以任何一種方式使用它(除非您決定使用其他HTTTP客戶端API,如HttpClient)


閱讀更多

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM