3

I have a maven based multi-module Jersey project which exposes few RestFul API. Project Structure is like -

Project

  • Module1

    ----------src

    --------- unit test

  • Module2

    ----------src

    --------- unit test

  • Module3

    ----------src

    --------- unit test

  • ModuleN - this module contains Integration Tests which will hit endpoints exposed by project and test the whole service like a black box

    ----------src

    --------- unit test

I want to build this project and execute unit tests in during build phase then create a jar, deploy this jar somewhere, execute integration tests (which is present in one module of project and this will hit the REST end points) then I wanted to measure combine coverage (unit+integration tests).

I have gone though a lot of blogs and articles but everywhere we have half information. can someone point me or guide me how can I do it.

Thanks -Shahid

3
  • Have you tried the cobertura plugin? with the aggregate flag to true it should accomplish what you ask Commented Aug 24, 2016 at 14:38
  • NO ... I tried with Jacoco with Jetty plug-in because my integration tests executes after deployment of jar on jetty server. Can you please provide me an example? Commented Aug 26, 2016 at 9:43
  • @Sammyrulez can you please provide me some example how can I achieve this. thanks! Commented Sep 8, 2016 at 4:13

1 Answer 1

2

As I've answered here: You can put all the reports together in one folder (don't forget to call the different!) and use the merge mojo for that, or use a central unique file to all your reports by adding the flag "append":

-javaagent:append=true,destFile=/home/YourProject/report.exec

[Here][2] you'll find more information about how to configure the agent.

Hope it helps!

(this is a copy of my other answer to the same problem)

EDIT:

As long as you were asking for different kind of execution (unit and integration) I assume you are using different plugins for the execution of your tests.

What you have to do is to prepare two JaCoCo agents and give the argLine of them to the plugin that will run your test:

        <plugin>
            <groupId>org.jacoco</groupId>
            <artifactId>jacoco-maven-plugin</artifactId>
            <version>0.7.7.201606060606</version>
            <executions>
                <!--
                    Prepares the property pointing to the JaCoCo runtime agent which
                    is passed as VM argument when Maven the Surefire plugin is executed.
                -->
                <execution>
                    <id>pre-unit-test</id>
                    <goals>
                        <goal>prepare-agent</goal>
                    </goals>
                    <configuration>
                        <!-- Sets the path to the file which contains the execution data. -->
                        <destFile>${project.build.directory}/jacoco.exec</destFile>
                        <!--
                            Sets the name of the property containing the settings
                            for JaCoCo runtime agent.
                        -->
                        <propertyName>surefireArgLine</propertyName>
                    </configuration>
                </execution>
                <!--
                    Ensures that the code coverage report for unit tests is created after
                    unit tests have been run.
                -->
                <execution>
                    <id>post-unit-test</id>
                    <phase>test</phase>
                    <goals>
                        <goal>report</goal>
                    </goals>
                    <configuration>
                        <!-- Sets the path to the file which contains the execution data. -->
                        <dataFile>${project.build.directory}/jacoco.exec</dataFile>
                        <!-- Sets the output directory for the code coverage report. -->
                        <outputDirectory>${project.reporting.outputDirectory}</outputDirectory>
                    </configuration>
                </execution>
                        <!-- The Executions required by unit tests are omitted. -->
                <!--
                    Prepares the property pointing to the JaCoCo runtime agent which
                    is passed as VM argument when Maven the Failsafe plugin is executed.
                -->
                <execution>
                    <id>pre-integration-test</id>
                    <phase>pre-integration-test</phase>
                    <goals>
                        <goal>prepare-agent</goal>
                    </goals>
                    <configuration>
                        <!-- Sets the path to the file which contains the execution data. -->
                        <destFile>${project.build.directory}/jacoco-it.exec</destFile>
                        <!--
                            Sets the name of the property containing the settings
                            for JaCoCo runtime agent.
                        -->
                        <propertyName>failsafeArgLine</propertyName>
                    </configuration>
                </execution>
                 <!--
                    Ensures that the code coverage report for integration tests after
                    integration tests have been run.
                -->
                <execution>
                    <id>post-integration-test</id>
                    <phase>post-integration-test</phase>
                    <goals>
                        <goal>report</goal>
                    </goals>
                    <configuration>
                        <!-- Sets the path to the file which contains the execution data. -->
                        <dataFile>${project.build.directory}/jacoco-it.exec</dataFile>
                        <!-- Sets the output directory for the code coverage report. -->
                        <outputDirectory>${project.reporting.outputDirectory}</outputDirectory>
                    </configuration>
                </execution>
            </executions>
        </plugin>

Then you have to configure your plugins (normally used failsafe and surefire) for accepting the argLine created for the JaCoCo agents:

            <plugin>
                <artifactId>maven-surefire-plugin</artifactId>
                <configuration>
                    <argLine>${surefireArgLine}</argLine>
                </configuration>
            </plugin>

The same for the failsafe plugin.

Sign up to request clarification or add additional context in comments.

1 Comment

My question is not only about merging code coverage report. Its about how can I run unit tests then start jetty server, run integration tests with coverage. So, question here is how can I measure coverage for unit+integration tests when unit tests runs as part of CI or build process whereas Integration tests run post deployment of jar.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.