3

我正试图在泽西岛进行单元测试。

不直接依赖 Jersey 的类可以使用标准 JUnit 进行单元测试。这个还行。

但是也有 Jersey(或者,我应该说 JAX-RS?)依赖类。为了测试这些(包括注释的正确性、序列化、状态码等),Jersey 提供了包含基JerseyTest类的“测试框架”。

这也很好,可以理解。

然后,然而,官方文档指定了几种类型的支持的容器,其中的子类JerseyTest可以执行。

这些容器似乎可以分为两种类型:

  1. 真正的容器
  2. 内存容器

从我目前(新手)的角度来看,所有类型 #1 的容器都提供相同的功能。因此,让我们以灰熊作为第一类代表。

现在,很明显内存容器(根本不是真正的容器)比 Grizzly 快。因此,我想使用这个容器来测试 Jersey 依赖类。但是官方文档中的这个声明让我感到困惑:

此容器不支持 servlet 和其他容器相关功能,但它是简单单元测试的完美选择。

我试图用谷歌搜索内存容器与 Grizzly 的比较,但没有找到任何明确的比较。我也阅读了这个高度技术性的线程,但我仍然不清楚内存容器的缺点。

在这方面,我有两个问题:

  1. 什么是“servlet 和其他依赖容器的特性”(不需要一个全面的列表,只是一般性的描述)?
  2. 如果我选择内存容器,我是否会遇到代码通过测试但在生产中失败的情况(如果这很重要,我将在生产中使用 Tomcat)?

谢谢

4

1 回答 1

2

What "servlet and other container dependent features" are (no need for a comprehensive list, but just general description)?

Say you need to use HttpServletRequest, ServletContext, etc. in your resources. Or if you are using serlvet filters or listeners that affect the Jersey application. These are servlet features.

Other "container features" just means any other features that are specific to that container you are using in production. For example, with Grizzly, you can inject a Grizzly specific Request object.

If I choose in-memory container, can I then encounter a situation of code that passes test, but fails in production (I will use Tomcat in production, if that matters)?

Mainly if you use items mentioned above.

See an example of where a servlet environment is required. Here, there was a Servlet Filter being used for security. So the filter needed to be configured in the test. And another example where HttpServletRequest is needed

于 2017-05-04T19:12:48.293 回答