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. BNU 51276 - 道路修建 Small (并查集)

    题目链接:http://www.bnuoj.com/v3/problem_show.php?pid=51276 具体题意不描述了,一眼看过去就是并查集,关键是添加边以后更新答案.我是开个二维的数组an ...

  2. Java学习笔记(一):数据类型与变量

    数据类型 Java中存在2种数据类型,下面我们来详解一下: 基本数据类型: 引用数据类型: 可以用一张表来记录: 基本数据类型 整型 byte:1个字节8位,取值范围为:[-128, 127],直接写 ...

  3. java 方法调用绑定--《java编程思想》学习笔记

    将一个方法调用同一个方法主体关联起来,就是绑定. 绑定分两种 :前期绑定 和 后期绑定 . 绑定------------- | -----前期绑定-------编译期绑定 { static , fin ...

  4. 从hello-world 开始 <contiki学习之四>

    按照contiki 官方给出的example下的例子之hello world来说,所有的工程里面都有一个唯一的Makefile.然后这个Makefile会去调用其他makefile文件.于是,一切就从 ...

  5. 【转】larbin中的url去重算法

    1.bloom filter算法 传说中,larbin使用bloom filter算法来进行url去重.那我们就先来了解下bloom filter算法好了. [以下转自:http://hi.baidu ...

  6. 用C#调用Matlab图像处理自制QQ游戏2D桌球瞄准器

    平时不怎么玩游戏,有时消遣就玩玩QQ里的2D桌球,但是玩的次数少,不能像骨灰级玩家一样百发百中,肿么办呢?于是某天突发奇想,决定自己也来做个“外挂”.说是外挂,其实只是一个瞄准器,毕竟外挂是修改别人的 ...

  7. Python学习 之 对内存的使用(浅拷贝和深拷贝)

    1.浅拷贝:对引用的拷贝,只拷贝父对象 cope() 深拷贝:对对象资源的拷贝 deepcope()

  8. 利用 Composer 一步一步构建自己的 PHP 框架(一)——基础准备

    『Composer 一统天下的时代已经到来!』——白岩松 “一个时代结束了,另一个时代开始了.” Framework Interoperability Group(框架可互用性小组),简称 FIG,成 ...

  9. 小白日记13:kali渗透测试之服务扫描(三)-SMTB扫描、防火墙识别、负载均衡识别、WAF识别

    SMTP扫描 SMTP(Simple Mail Transfer Protocol)即简单邮件传输协议,它是一组用于由源地址到目的地址传送邮件的规则,由它来控制信件的中转方式.SMTP协议属于TCP/ ...

  10. css笔记04:属性选择器

    1.属性选择器: 带有 title 属性的所有元素设置样式: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN ...