2010-06-04 51 views
0

我正在使用EMMA eclipse插件來生成代碼覆蓋率報告。 我的應用程序是一個RESTFul webservice。 Junit的編寫方式是爲web服務創建一個客戶端,並通過各種輸入進行調用。EclEmma JAVA代碼覆蓋率 - 無法覆蓋RESTful Web服務的服務層

但是,EMMA顯示源文件夾的覆蓋率爲0%。僅包含測試文件夾。

應用程序服務器(jetty服務器)使用主要方法啓動。

報告:

Element   Coverage Covered Instructions Total Instructions 
MyRestFulService 13.6%   900      11846 
src    0.5%   49      10412 
test    98%   1021      1434 

JUnit測試方法:

@Test 
    public final void testAddFlow() throws Exception { 
     Client c = Client.create(); 
     WebResource webResource = c.resource(BASE_URI); 

     // Sample files for Add 

     String xhtmlDocument = null; 

     Iterator iter = mapOfAddFiles.entrySet().iterator(); 

     while (iter.hasNext()) { 
       Map.Entry pairs = (Map.Entry) iter.next(); 

       try { 
        document = helper.readFile(requestPath 
           + pairs.getKey()); 
       } catch (Exception e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } 
       /* POST */ 
       MultiPart multiPart = new MultiPart(); 
       multiPart.bodyPart(.... 
       ........... 
       ClientResponse response = webResource.path("/add").type(
          MEDIATYPE_MULTIPART_MIXED).post(ClientResponse.class, 
          multiPart); 

        assertEquals("TESTING ADD FOR >>>>>>> " + pairs.getKey(), 
           Status.OK, response.getClientResponseStatus()); 



       } 
     } 
    } 

調用的服務方法:

@POST 
    @Path("add") 
    @Consumes("multipart/mixed") 
    public Response add(MultiPart multiPart) 
       throws Exception { 
     Status status = null; 
     List<BodyPart> bodyParts = null; 
     bodyParts = multiPart.getBodyParts(); 

     status = //call to business layer 

     return Response.ok(status).build(); 
    } 

回答

1

Emma提供了另一種執行離線檢測的解決方案。 這幫助我解決了這個問題。

0

如果該服務通過服務調用的代碼不會被覆蓋HTTP呼叫。 但是,通過傳遞輸入來直接調用webservice/business層方法是唯一的解決方案。 這也適用於任何Web應用程序。我們將直接嘲笑業務層。

我利用這個解決方案來獲得我的Junits的代碼覆蓋率。