Igo*_*orM 12 java junit testng jax-rs jersey
我有一个用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?
}
Run Code Online (Sandbox Code Playgroud)
小智 9
新的(修订的)Jersey测试框架是Jersey 1.1.2-ea版本的一部分,现在支持进程内或内存测试.为了在内存中运行测试,您只需将属性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
有关详细信息,请浏览名为Jersey Test Framework的博客文章重新访问!在http://blogs.oracle.com/naresh.
小智 5
我相信Jersey 测试框架为您的需求提供了解决方案。它允许您部署单个服务并运行其所有测试。您可以使用该框架针对 Grizzly Web Container、嵌入式 GlassFish 和/或 HTTPServer 运行您的测试。
请注意,您也可以使用该框架针对 GlassFish 和 Tomcat 等常规 Web 容器运行测试。如果您有任何疑问,请随时向我或 Jersey 用户邮件列表 - users@jersey.dev.java.net 发送电子邮件。
我还没有尝试过,但像 HtmlUnit 或 HttpUnit 这样的 JUnit 扩展可能是测试 JAX-RS/Jersey 服务的好方法。测试用例可以使用 XPath 查找预期返回值并根据预期验证返回值。请参阅: http: //htmlunit.sourceforge.net/gettingStarted.html了解更多信息。
| 归档时间: |
|
| 查看次数: |
11065 次 |
| 最近记录: |