2010-06-04 119 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的代码覆盖率。