views:

143

answers:

1

I am using EMMA eclipse plugin to generate code coverage reports. My application is a RESTFul webservice. Junits are written such that a client is created for the webservice and invoked with various inputs.

However EMMA shows 0% coverage for the source folder. The test folder alone is covered.

The application server(jetty server) is started using a main method.

Report:

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

Junit Test method:

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



              }
        }
  }

Invoked service method:

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

The service code will not be covered if the service is invoked via a http call. However a direct invokation of the webservice/business layer methods by passing the inputs is the only solution. This is the way with any web application as well. We will directly mock the business layer.

I utilized this solution to get code coverage for my Junits.

Radhika