java 为 JERSEY Web 服务编写测试用例的最佳方法是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/911813/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 14:19:48  来源:igfitidea点击:

What is the best way to write a test case for JERSEY web services?

javajunitjerseyjax-rstestng

提问by IgorM

I have a JAX-RS web service implemented with Jersey library and now I want to test it. In order to do that I'd like to host this service in my test by preinitializing it with mocked services.

我有一个用 Jersey 库实现的 JAX-RS Web 服务,现在我想测试它。为了做到这一点,我想通过使用模拟服务对其进行预初始化来在我的测试中托管该服务。

What is the best way to host such a service and execute the test calls?

托管此类服务并执行测试调用的最佳方法是什么?

@Path("/srv")
public class MyService
{
   @GET
   public void action(@Context UriInfo uri)
   { ... }
}

@Test
public void myTest()
{
   MyService service = new MyService();
   service.setSomething(...);

   // How do I host it?

   // How do I call it?
}

回答by IgorM

The new (revised) Jersey Test Framework which is part of the Jersey 1.1.2-ea release now supports the In-Process or In-Memory testing. In order to run your tests in-memory all you have to do is set the property test.containerFactoryto com.sun.jersey.test.framework.spi.container.inmemory.InMemoryTestContainerFactory, i.e., run your tests as follows:

作为 Jersey 1.1.2-ea 版本的一部分的新的(修订的)Jersey 测试框架现在支持 In-Process 或 In-Memory 测试。为了在内存中运行您的测试,您所要做的就是将属性test.containerFactory设置为com.sun.jersey.test.framework.spi.container.inmemory.InMemoryTestContainerFactory,即,按如下方式运行您的测试:

mvn clean test -Dtest.containerFactory=com.sun.jersey.test.framework.spi.container.inmemory.InMemoryTestContainerFactory -DenableLogging

mvn clean test -Dtest.containerFactory=com.sun.jersey.test.framework.spi.container.inmemory.InMemoryTestContainerFactory -DenableLogging

For more details please go through the blog entry titled Jersey Test Framework re-visited!at http://blogs.oracle.com/naresh.

有关更多详细信息,请阅读名为Jersey 测试框架重新访问的博客条目http://blogs.oracle.com/naresh

回答by IgorM

I believe the Jersey Test Frameworkprovides a solution for your requirement. It allows you to deploy a single service, and run all its tests. You could use the framework to run your tests against Grizzly Web Container, Embedded GlassFish and/or HTTPServer.

我相信Jersey 测试框架为您的需求提供了解决方案。它允许您部署单个服务,并运行其所有测试。您可以使用该框架针对 Grizzly Web Container、嵌入式 GlassFish 和/或 HTTPServer 运行您的测试。

Please note that you could use the framework to run your tests against the regular web containers like GlassFish and Tomcat too. In case you have any more queries please feel free to send me or the Jersey users mailing list - [email protected] an e-mail.

请注意,您也可以使用该框架针对 GlassFish 和 Tomcat 等常规 Web 容器运行测试。如果您有任何疑问,请随时向我或 Jersey 用户邮件列表 - [email protected] 发送电子邮件。

回答by Hyman Cox

I haven't tried it, but a JUnit extension like HtmlUnit or HttpUnit may be a good way to test a JAX-RS/Jersey service. The test case can use XPaths to find expected return values and validate the returned value against the expected. See: http://htmlunit.sourceforge.net/gettingStarted.htmlfor more info.

我还没有尝试过,但是像 HtmlUnit 或 HttpUnit 这样的 JUnit 扩展可能是测试 JAX-RS/Jersey 服务的好方法。测试用例可以使用 XPaths 来查找预期返回值并根据预期验证返回值。请参阅:http: //htmlunit.sourceforge.net/gettingStarted.html了解更多信息。

回答by Jonathan

You can use Grizzly to host the services and then use the Jersey Client to access them. Take a look at the sample applications. For example, in the Bookstore sample you may find the TestSupport class and JerseyTest class (found in the jersey-test-framework) of particular interest.

您可以使用 Grizzly 托管服务,然后使用 Jersey 客户端访问它们。查看示例应用程序。例如,在 Bookstore 示例中,您可能会发现特别感兴趣的 TestSupport 类和 JerseyTest 类(在 jersey-test-framework 中找到)。

I hope this helps.

我希望这有帮助。

(Unfortunately Stack Overflow wouldn't let me post until I removed all the hyperlinks so happy Googling!).

(不幸的是 Stack Overflow 不允许我发帖,直到我删除了所有超链接,所以很高兴谷歌搜索!)。

回答by deverton

Have you looked in to using the Jersey Test Framework? Unfortunately it's still more integration test than unit test, but it might get you on your way.

您是否考虑过使用Jersey 测试框架?不幸的是,它仍然是比单元测试更多的集成测试,但它可能会让你走上正轨。

回答by deverton

Okay I get it now. Right now the framework doesn't support IN PROCESS, bt we are working on it. We will see that this support would be added in a coming version of the Jersey Test Framework

好的,我现在明白了。目前该框架不支持 IN PROCESS,但我们正在努力。我们将看到这种支持将添加到即将发布的 Jersey 测试框架版本中