2014-08-28 33 views
2

单元测试基于Jersey的Restful Web Services的最佳库/ API是什么?一些像JerseyTest这样的API看起来已经过时了(在我的pom中使用它们时会发生冲突),而且似乎取决于特定的容器,例如Glassfish或Grizzly ......我将基于Jersey的Restful Web Services作为war文件部署到Tomcat 7.有没有办法使用具有嵌入式Web服务器或内存解决方案的测试框架?再次感谢。单元测试Jersey Restful Web Services的最佳API/lib?

回答

1

我使用对我的许多项目使用,因为它提供了高度专业化的dsl来编写测试,一旦您将自定义添加到记录中,写入测试的速度非常快。

各种各样的例子可以在project website找到但对于一个快速预览 - 样品测试看起来是这样的片段:

expect() 
    .statusCode(200) 
    .body("user.id", equalTo(1)) 
.when() 
.given() 
    .contentType(ContentType.JSON) 
.get("http://test/rest"); 

由于我的博客是由巴拉吉报价,我想补充有this article of mine有更多用于其他框架的示例以及用于测试示例的可下载REST服务器。

球衣测试测试例子可能看起来像这个例子从project's documentation采取:

public class SimpleTest extends JerseyTest { 

    @Path("hello") 
    public static class HelloResource { 
     @GET 
     public String getHello() { 
      return "Hello World!"; 
     } 
    } 

    @Override 
    protected Application configure() { 
     return new ResourceConfig(HelloResource.class); 
    } 

    @Test 
    public void test() { 
     final String hello = target("hello").request().get(String.class); 
     assertEquals("Hello World!", hello); 
    } 
}