【问题标题】:EclEmma JAVA Code coverage - Unable to coverage service layer of RESTful WebserviceEclEmma JAVA 代码覆盖 - 无法覆盖 RESTful Webservice 的服务层
【发布时间】:2011-02-27 15:38:23
【问题描述】:

我正在使用 EMMA eclipse 插件来生成代码覆盖率报告。 我的应用程序是一个 RESTFul 网络服务。 编写 Junit 以便为 Web 服务创建客户端并使用各种输入调用。

但是 EMMA 显示源文件夹的覆盖率为 0%。仅测试文件夹被覆盖。

应用服务器(jetty server)使用main方法启动。

报告:

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();
  }

【问题讨论】:

    标签: web-services client rest code-coverage


    【解决方案1】:

    Emma 提供了另一种执行离线检测的解决方案。 这帮助我解决了问题。

    【讨论】:

      【解决方案2】:

      如果服务是通过 http 调用调用的,服务代码将不会被覆盖。 然而,通过传递输入直接调用 web 服务/业务层方法是唯一的解决方案。 这也是任何 Web 应用程序的方式。我们将直接模拟业务层。

      我利用此解决方案为我的 Junit 获取代码覆盖率。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-02-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-09-29
        • 1970-01-01
        相关资源
        最近更新 更多