Thomas Sundberg

February 18, 2012

Test coverage in a multi module Maven project

Filed under: Automation, Java, Maven, Test Automation — Tags: , , , , , — Thomas Sundberg @ 09:35

This post is available at http://www.thinkcode.se/blog/2012/02/18/test-coverage-in-a-multi-module-maven-project

41 Comments »

  1. Great post. I’ve used it and it works excellently. I adapted it using the maven antrun plugin so you can have everything in the parent pom file, optionally with external ant script files. Using the plugin you don’t need the cobertura installation and only need to run mvn twice and you have your report. If you’re interested in more details, let me know.

    Comment by Wouter — August 8, 2012 @ 14:19

    • hi Wouter,
      can you please provide your pom.xml for multi module project.

      Comment by gourab — November 5, 2012 @ 22:14

      • Hi Wouter,
        Yes, that would be great if you could post your pom file here. Thanks a lot.

        Comment by moudug — November 15, 2012 @ 13:29

    • Hi Wouter,
      I’m having the problem of multi-module and test coverage. I read this post, but it works with cobertura plugin and in my project we don’t use this plugin. Could you please send me your pom without cobertura? is that has to do with the command run? Is it possible to run ant command with a project maven?

      Regards,

      Sarang (igiagante@gmail.com)

      Comment by Ignacio — November 12, 2012 @ 16:49

      • We use cobertura :P.

        Comment by Ignacio — November 13, 2012 @ 13:37

    • can uu post u r pom please ..i am facing issues/////

      Comment by anandkatti108 — February 7, 2013 @ 13:35

  2. Hi
    we are getting stuck in maven pom.xml to get coverage report if the test case are executing fine but data tables are not available now becasue they are old for some modules and we wont use further but it is affecting the overall coverage report

    can you please help and my email id is shankerdeee1@yahoo.co.in

    Comment by shanker — August 20, 2012 @ 08:51

  3. Hi Thomas,

    I received the error: [taskdef] Could not load definitions from resource tasks.properties. It could not be found. could you help me please? Thanks

    Regards,

    Sarang

    Comment by Ignacio — November 14, 2012 @ 14:51

    • Hi!

      Does the cobertura.classpath contain the proper jars? If it doesn’t then I would expect this error to occur.
      Double check your paths, there might be an error there.

      HTH
      Thomas

      Comment by Thomas Sundberg — November 14, 2012 @ 20:29

      • I could resolve all problems. The path of my cobertura.dir was wrong. Now, I have another problem. When I run report and want to see the result of the coverage, it appear all in red, 0% coverage over all class tested. What could be?

        Comment by Ignacio — November 14, 2012 @ 23:36

      • One reason may be that you have somehow failed to annotate the classes before executing the tests. Another may be that you haven’t combined the .ser files properly. I would double check the order the steps are executed in.

        HTH
        Thomas

        Comment by Thomas Sundberg — November 15, 2012 @ 06:14

      • I tried to post the ant.xml with the structure of my project and I did not have success. Please, send me your e-mail, and I will send you if you do not matter.

        Comment by Ignacio — November 15, 2012 @ 14:38

      • The files cobertura.ser and sum.ser are located in a folder call ant, which contains the ant.xml file. Is correct
        to have these files in the root project?

        Structure project:

        -Project root
        — ant
        — ant.xml
        — cobertura.ser
        — sum.ser
        — target
        — reports

        * The commands are ran in the next steps:

        — mvn clean compile
        — ant instrument
        — mvn test
        — ant report


        Comment by Ignacio — November 15, 2012 @ 14:43

      • Hi Thomas,
        I am facing the same problem, like [taskdef] Could not load definitions from resource tasks.properties. It could not be found, and my build.xml is similar to the above and i just updated the path COBERTURA_HOME to be D:\cobertura-1.9.41
        Can you please help me on this.

        Thanks.
        Majeed H.

        Comment by MohamedKMajeed Haja moideen — December 18, 2015 @ 11:50

  4. Thomas!! I got it. One path was wrong. Now, I have to configure jenkins to run these commands in order to get the correct coverage. Any suggestion for that?

    Thanks!

    Comment by Ignacio — November 16, 2012 @ 15:01

    • Hi!

      Glad you found the problem.

      My suggestion if you want to run this using Jenkins is to create freestyle project. Check out the source and add four consecutive steps. First the Maven compile step, then the Ant instrument step, then the Maven test step and finally the Ant report step. The last thing you would like to do is to have Jenkins to display the result. You can add support for Cobertura reports at the end of the job configuration. I don’t remember the details and I don’t have a Jenkins that I could try with to get the commands correct. But Jenkins has a Cobertura plugin that you can use. I think it s a plugin at least, it could be built in as well.

      HTH
      Thomas

      Comment by Thomas Sundberg — November 17, 2012 @ 08:55

      • [cobertura-merge] Cobertura 1.9.4.1 – GNU GPL License (NO WARRANTY) – See COPYRIGHT file
        [cobertura-merge] Exception in thread “main” java.lang.NoClassDefFoundError: org.apache.log4j.Logger

        Comment by anandkatti108 — February 7, 2013 @ 13:34

  5. Hi Thomas,

    Thanks for this interesting post. I’m wondering how surefire plugin knows about the generated .ser file in your example.
    I tried to pass it via system.properties.variable or -D option but it seems it doesn’t work.
    Tests are executed against instrumented libraries but .ser file is not updated.
    If I run unit tests with ant and by providing the .ser it works fine.

    Regards

    Comment by jnc — April 30, 2013 @ 15:22

    • Sorry for the wrong information, but writing it down finally helped me to find the trick.

      Surefire was running tests against non instrumented libraries (the one declared in project dependency section), as I instrument previously generated artifacts.

      By excluding these project dependencies in surefire configuration and by adding additional classpath element pointing to the instrumented dependencies it works like a charm.

      Thanks again for your post.

      Comment by jnc — April 30, 2013 @ 15:41

      • Hi!

        I am glad that you found my post valuable and that you found the problem. I obviously acted as a rubber duck without even knowing it 🙂

        /Thomas

        Comment by Thomas Sundberg — May 1, 2013 @ 10:01

  6. Thomas,

    THANK YOU for posting this solution. I spent a day looking for a solution, many of which are either out of date or simple don’t work. My frustration has ended with your post. It was easy to read and easy to understand and I don’t even know how to use Ant, but picked up on it quickly.

    Thanks again!! =)

    Comment by Billy — April 30, 2013 @ 23:00

  7. Thanks A lot Thomas, it helped me a lot .. I implement your solution with maven-antrun-plugin http://mebada.blogspot.com/2013/05/how-to-use-maven-with-cobertura-code.html it may help

    Comment by worldpython — May 27, 2013 @ 16:49

  8. I do have an antrun example, but not one that describes how to solve the problem with multi module test coverage.

    Maven and Antrun

    /Thomas

    Comment by Thomas Sundberg — October 8, 2013 @ 22:07

  9. Thanks for the nice post. I found this article very helpful as I am using a separate module for testing. The report is working fine except for a small issue – unable to locate the source code. I am using the ANT script as-is and run from the root. Would you be able to help with this?
    Thanks

    Comment by patb23 — March 31, 2015 @ 20:30

    • Hi!

      It is very hard to know what is causing your problem. I would assume that you need to change something in the Ant script but i am not certain what you should change. I would double check the paths in the Ant script.

      So, sorry. No I can’t say what is wrong without running on your system. And I can’t do that.

      /Thomas

      Comment by Thomas Sundberg — March 31, 2015 @ 20:43

    • Please ignore the request for help. Added a task that copies all src to the destination. it worked. Thanks a lot

      Comment by patb23 — March 31, 2015 @ 21:12

  10. Hi Thomas, this article is very useful to me. but i found a problem:

    when i run ‘mvn test’, i get error ‘java.lang.NoClassDefFoundError: net/sourceforge/cobertura/coveragedata/TouchCollector’; however, when i run ‘mvn cobertura:cobertura’, it’s all right.

    thanks again!

    Comment by onlyjdoc — May 18, 2015 @ 09:14

  11. To run this in Jenkins, do you need to download the Cobertura jar file and put it somewhere in your project?

    Comment by David — October 26, 2015 @ 20:12

  12. Excellent information and I’ve implemented it myself. Since the original post in 2012 I wonder if changes to Maven have made a pure Maven solution possible.

    Comment by Mike West — April 9, 2016 @ 00:16

    • Hi!

      I haven’t checked if any change in Maven has made it possible to do this with a pure Maven solution.

      But I kind of doubt it. The problem is that Maven uses depth first when it builds a multi module project. Each module is built completely before it starts with next module. Instrumenting the classes is done before the tests are executed. But the classes in an earlier module will not be touched again once the module is built and especially will they not be instrumented. Therefore, a pure Maven solution is not possible.

      The better solution? Keep your unit tests within the same module and make sure the test coverage is relevant using unit tests.

      Cheers,
      Thomas

      Comment by Thomas Sundberg — April 9, 2016 @ 16:58

  13. Hi Thomas,

    Excellent post. Congrats!!

    I’m trying to do the same with Gradle, but i’m not having success. 😦

    The reports are generated with 0% of coverage.

    Can you give some tips?

    Thanks.

    Comment by Inácio Nery — June 29, 2016 @ 15:24

    • Hi!

      I haven’t tried to use any code coverage tools using Gradle. Cobertura has to annotate the classes that should be executed before the tests are run. The coverage will then be recorded and you can see stats.

      That said, the symptom you report sounds to me as if the production classes haven’t been properly annotated before the tests was executed.

      /Thomas

      Comment by Thomas Sundberg — July 8, 2016 @ 13:34

  14. Hi, thank you…
    Now I can finally generate the reports.
    My problem now is to integrate it with sonar.
    Do you know if is possible integrate multi module project in SOnar?

    Comment by Renan — September 24, 2016 @ 17:56

    • Hi!

      I know that it is possible to integrate a Maven multi module project with Sonar. I have seen it been done. I have never done it myself so I can’t say how complicated it might be.

      Cheers,
      Thomas

      Comment by Thomas Sundberg — September 25, 2016 @ 08:05


RSS feed for comments on this post. TrackBack URI

Leave a comment

Create a free website or blog at WordPress.com.