在没有HTTP服务器的情况下进行泽西单元测试 [英] Jersey Unit Tests without HTTP Server
问题描述
我想设置一个非常轻巧的球衣单元测试.我的Web服务会生成JSON结果,我想直接验证它而不使用HTTP服务器.无需使用像Grizzly HTTP这样的HTTP服务器就可以创建轻量级的jsersey测试吗?
I would like to setup a very lightweight jersey unit-test. My webservice produces JSON result which I want to validate directly without using a HTTP-Server. Is it possible to create lighweight jsersey tests without using a HTTP-Server like Grizzly HTTP?
推荐答案
不确定所使用的Jersey版本,但请查看Jersey测试框架.有一个不涉及任何网络连接的内存容器.这是文档
Not sure what version of Jersey you are using, but look into Jersey Test Framework. There is an in-memory container, that doesn't involve any network connection. Here's a documentation
- Jersey 2.x Test Framework
- Jersey 1.x Test Framework
您可以在项目源代码测试中看到一些不同的示例
You can see some different examples in the project source code tests
- Jersey 2
- Jersey 1
依赖项
-
Jersey 2.x
Jersey 2.x
<dependency>
<groupId>org.glassfish.jersey.test-framework.providers</groupId>
<artifactId>jersey-test-framework-provider-inmemory</artifactId>
<version>${jersey2.version}</version>
</dependency>
Jersey 1.x
Jersey 1.x
<dependency>
<groupId>com.sun.jersey.jersey-test-framework</groupId>
<artifactId>jersey-test-framework-inmemory</artifactId>
<version>${jersey1.version}</version>
</dependency>
唯一的缺点是,如果您对某些特定的Servlet功能有任何依赖,则可能无法在此处找到它.
The only down side, is that if you have any dependency on some specific Servlet functionality, you may not get it here.
这篇关于在没有HTTP服务器的情况下进行泽西单元测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!