2014-08-28 36 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); 
    } 
}