The Jersey client API was originally developed to aid the testing of the Jersey server-side, primarily to make it easier to write functional tests in conjunction with the JUnit framework for execution and reporting. It is used extensively and there are currently over 1000 tests.

Embedded servers, Grizzly and a special in-memory server, are utilized to deploy the test-based services. Many of the Jersey samples contain tests that utilize the client API to server both for testing and examples of how to use the API. The samples utilize Grizzly or embedded Glassfish to deploy the services.

The following code snippets are presented from the single unit test HelloWorldWebAppTest of the helloworld-webapp sample. The setUp method, called before a test is executed, creates an instance of the Glassfish server, deploys the application, and a WebResource instance that references the base resource:

@Override
protected void setUp() throws Exception {
super.setUp(); // Start Glassfish
glassfish = new GlassFish(BASE_URI.getPort()); // Deploy Glassfish referencing the web.xml
ScatteredWar war = new ScatteredWar(
BASE_URI.getRawPath(),
new File("src/main/webapp"),
new File("src/main/webapp/WEB-INF/web.xml"),
Collections.singleton(new File("target/classes").toURI().toURL()));
glassfish.deploy(war); Client c = Client.create();
r = c.resource(BASE_URI);
}

The tearDown method, called after a test is executed, stops the Glassfish server.

@Override
protected void tearDown() throws Exception {
super.tearDown();
glassfish.stop();
}

The testHelloWorld method tests that the response to a GET request to the Web resource returns “Hello World”:

public void testHelloWorld() throws Exception {
String responseMsg = r.path("helloworld").get(String.class);
assertEquals("Hello World", responseMsg);
}

Note the use of the path method on the WebResource to build from the base WebResource.

Jersey(1.19.1) - Client API, Testing services的更多相关文章

  1. Jersey(1.19.1) - Client API, Uniform Interface Constraint

    The Jersey client API is a high-level Java based API for interoperating with RESTful Web services. I ...

  2. Jersey(1.19.1) - Client API, Ease of use and reusing JAX-RS artifacts

    Since a resource is represented as a Java type it makes it easy to configure, pass around and inject ...

  3. Jersey(1.19.1) - Client API, Overview of the API

    To utilize the client API it is first necessary to create an instance of a Client, for example: Clie ...

  4. Jersey(1.19.1) - Client API, Using filters

    Filtering requests and responses can provide useful functionality that is hidden from the applicatio ...

  5. Jersey(1.19.1) - Client API, Security with Http(s)URLConnection

    With Http(s)URLConnection The support for security, specifically HTTP authentication and/or cookie m ...

  6. Jersey(1.19.1) - Client API, Proxy Configuration

    为 Jersey Client 设置代理,可以使用带有 ClientHandler 参数的构造方法创建 Client 实例. public static void main(String[] args ...

  7. docker报Error response from daemon: client is newer than server (client API version: 1.24, server API version: 1.19)

    docker version Client: Version: 17.05.0-ce API version: 1.24 (downgraded from 1.29) Go version: go1. ...

  8. Jersey(1.19.1) - XML Support

    As you probably already know, Jersey uses MessageBodyWriters and MessageBodyReaders to parse incomin ...

  9. Jersey(1.19.1) - JSON Support

    Jersey JSON support comes as a set of JAX-RS MessageBodyReader<T> and MessageBodyWriter<T&g ...

随机推荐

  1. AxWindowsMediaPlayer创建、添加播放列表(C#)

    // 创见打开对话框对象实例            OpenFileDialog openFileDialog = new OpenFileDialog(); //设置为可以打开多个文件        ...

  2. 创建类模式(三):创建者(Builder)

    定义 将一个复杂对象的构建与它的表示分离,使得同样的构建过程可以创建不同的表示.这使得构件算法和组装方式可以独立应对变化:复用同样的构建算法可以创建不同的表示,不同的构建过程可以复用相同的部件组装方式 ...

  3. Hibernate的配置

    配置Hibernate 1.在项目中引入Hibernate的Jar包 在 WebContent/WEB-INF/lib 目录下 导入jar包 2.在Java Resources 下创建 hiberna ...

  4. mysql和oracle日期和字符相互转换

    一.mysql日期和字符相互转换 1.1.日期——>字符   date_format(date,'%Y-%m-%d')     oracle中的to_char();    1.2.字符——> ...

  5. Python beautifulsoup模块

    BeautifulSoup中文文档:https://www.crummy.com/software/BeautifulSoup/bs4/doc.zh/ BeautifulSoup下载:http://w ...

  6. ThinkPad指纹验证在win7无法使用的解决方法

    原先本本装window7 64bit 专业版(正版),但用着用着觉得 很不爽 ,反应特慢.所以决定对本本来次大换血,换成windows server 2008 R2.最后在装指纹验证的时候,使用超级管 ...

  7. PL/pgSQL的RETURN QUERY例子

    我的例子: 数据准备: create table custinfo(custid integer,callingcnt integer); ,),(,),(,); 函数生成: CREATE OR RE ...

  8. Flex中 Array 的IndexOf 的作用

    Flex中 Array 的IndexOf 的作用 1.说明    indexOf用于在索引中从小到大查找,假设查得到就返回索引值,查不到就返回-1: 2.实例 (1)设计源代码 <?xml ve ...

  9. IntellijIdea中常用的快捷键

    快速查找类:Ctrl+N 提示:Ctrl+Space 提示:Ctrl+Shift+Space 查看documentation:Ctrl+Q 查找类.方法.变量的引用:Alt+F7 定位类.方法.变量的 ...

  10. 网络IPC:套接字之非阻塞和异步I/O

    通常,recv函数没有数据可用时会阻塞等待.同样地,当套接字输出队列没有足够空间来发送消息时函数send会阻塞.在套接字非阻塞模式下,行为会改变.在这种情况下,这些函数不会阻塞而是失败,设置errno ...