为 JERSEY Web 服务编写测试用例的最佳方法是什么?
我有一个使用 Jersey 库实现的 JAX-RS Web 服务,现在我想测试它。 为了做到这一点,我想通过使用模拟服务预初始化该服务来在我的测试中托管该服务。
托管此类服务并执行测试调用的最佳方法是什么?
@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?
}
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.
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?
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
新的(修订版)Jersey 测试框架是 Jersey 1.1.2-ea 版本的一部分,现在支持进程内或内存中测试。 为了在内存中运行测试,您只需将属性 test.containerFactory 设置为 com.sun.jersey.test.framework.spi.container.inmemory.InMemoryTestContainerFactory< /em>,即按如下方式运行测试:
mvn clean test -Dtest.containerFactory=com.sun.jersey.test.framework.spi.container.inmemory.InMemoryTestContainerFactory -DenableLogging
有关更多详细信息,请浏览标题为 Jersey Test Framework re-visited! 的博客文章,网址为 http://blogs.oracle.com/naresh。
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.containerFactory to com.sun.jersey.test.framework.spi.container.inmemory.InMemoryTestContainerFactory, i.e., run your tests as follows:
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 测试框架 提供了一个解决方案您的要求。 它允许您部署单个服务并运行其所有测试。 您可以使用该框架针对 Grizzly Web Container、嵌入式 GlassFish 和/或 HTTPServer 运行测试。
请注意,您也可以使用该框架针对常规 Web 容器(例如 GlassFish 和 Tomcat)运行测试。 如果您有任何疑问,请随时向我或泽西岛用户邮件列表发送 - [ email protected] 一封电子邮件。
I believe the Jersey Test Framework provides 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.
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.
我还没有尝试过,但像 HtmlUnit 或 HttpUnit 这样的 JUnit 扩展可能是测试 JAX-RS/Jersey 服务的好方法。 测试用例可以使用 XPath 查找预期返回值并根据预期验证返回值。 请参阅:http://htmlunit.sourceforge.net/gettingStarted.html 了解更多信息。
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.html for more info.
您可以使用 Grizzly 托管服务,然后使用 Jersey Client 访问它们。 查看示例应用程序。 例如,在 Bookstore 示例中,您可能会发现特别感兴趣的 TestSupport 类和 JerseyTest 类(在 jersey-test-framework 中找到)。
我希望这有帮助。
(不幸的是,除非我删除所有超链接,否则 Stack Overflow 不会让我发帖,所以快乐谷歌搜索!)。
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.
I hope this helps.
(Unfortunately Stack Overflow wouldn't let me post until I removed all the hyperlinks so happy Googling!).
好吧,我现在明白了。 目前该框架不支持 IN PROCESS,但我们正在努力解决它。
我们将看到此支持将添加到即将发布的 Jersey 测试框架版本中
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
您是否考虑过使用 Jersey 测试框架? 不幸的是,它仍然是集成测试而不是单元测试,但它可能会让您上路。
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.