Free MuleSoft CONNECT Keynote & Expo Pass Available!

Register now+
Nav

About Coverage in Maven

Since MUnit 2.x, the coverage report goal is properly integrated with the maven reporting section.


         
      
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
<plugin>
    <groupId>com.mulesoft.munit.tools</groupId>
    <artifactId>munit-maven-plugin</artifactId>
    <version>${project.version}</version>
    <executions>
        <execution>
            <id>test</id>
            <phase>test</phase>
            <goals>
                <goal>test</goal>
                <goal>coverage-report</goal>
            </goals>
        </execution>
    </executions>
    <configuration>
        <coverage>
            <runCoverage>true</runCoverage>
            <failBuild>false</failBuild>
            <requiredApplicationCoverage>0</requiredApplicationCoverage>
            <requiredResourceCoverage>0</requiredResourceCoverage>
            <requiredFlowCoverage>0</requiredFlowCoverage>
            <formats>
                <format>console</format>
                <format>html</format>
            </formats>
        </coverage>
    </configuration>
</plugin>

Coverage Reports are generated during Maven’s site lifecycle.
Since MUnit 2.x, coverage report generation is run separately during the "coverage-report" goal.

When enabling MUnit Coverage, you only see a summary report in the console.
By default no other action is taken so it’s merely informative.

This is how a summary report looks like:


         
      
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
[INFO] ===============================================================================
[INFO]                                 MUnit Coverage Report
[INFO] ===============================================================================
[INFO]
[INFO] -------------------------------------------------------------------------------
[INFO] Resource File: mule-app.xml - Flows: 1 - Weight: 100.00 -  Coverage: 100.00%
[INFO] -------------------------------------------------------------------------------
[INFO]   * Flow: muleFlow - Covered (MP): 2 - Total (MP): 2 - Coverage: 100.00%
[INFO]
[INFO] ===============================================================================
[INFO]                                         Summary
[INFO] ===============================================================================
[INFO]   * CMPs: 2
[INFO]   * MPs: 2
[INFO]   * Flows: 1
[INFO]   * Resources: 1
[INFO] ===============================================================================
[INFO]                                 ** Application Coverage: 100.00% **
[INFO] ===============================================================================
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 27.462 s
[INFO] Finished at: yyyy-mm-ddThh:mm:ss
[INFO] Final Memory: 20M/379M
[INFO] ------------------------------------------------------------------------

But a console report is not your only option. MUnit Coverage offers three types of reports:

  • Console

  • HTML

  • JSON

The Console report is printed in the console. It works with the summary report and shows details of each resource, flow, sub-flow, and batch, and its coverage level.

The HTML report shows the same information, which you can view in any web browser. To access the HTML report, browse your application folder structure:

  • ${application.path}/target/site/munit/coverage/

Locate the file summary.html, which is the starting point of the report and lets you navigate through all the data.

The JSON report shows the same information as the HTML report, in a JSON format.
To access the JSON report file, browse your application folder structure:

${application.path}/target/site/munit/coverage/munit-coverage.json

To enable the report generation, add the following configuration:

MUnit Coverage - Report Configuration

         
      
1
2
3
4
5
6
7
8
9
<coverage>
  <runCoverage>true</runCoverage>

  <formats>
    <format>console</format>  (1)
    <format>html</format>     (2)
    <format>json</format>     (3)
  </formats>
</coverage>
1 Console report
2 HTML report
3 JSON report
You can have none, one, or all the report types added to your configuration.

To configure, for example, the JSON report generation, you need to add the following configuration in your pom.xml file:


         
      
1
2
3
4
5
6
<coverage>
<runCoverage>true</runCoverage>
  <formats>
    <format>json</format>
  </formats>
</coverage>

One of the features of MUnit Coverage is to fail the build if a certain coverage level is not reached.

MUnit Coverage handles three different levels:

  • Application

  • Resource

  • Flow

Here is how to define the required coverage level:

MUnit Coverage - Require Coverage

         
      
1
2
3
4
5
6
7
8
<coverage>
  <runCoverage>true</runCoverage>
  <failBuild>true</failBuild>

  <requiredApplicationCoverage>20</requiredApplicationCoverage>
  <requiredResourceCoverage>10</requiredResourceCoverage>
  <requiredFlowCoverage>5</requiredFlowCoverage>
</coverage>
Each value represents a percentage.

If you define coverage levels, but set the property failBuild to false, and if the levels are not reached, a warning shows up in the MUnit Coverage summary.

Something like this:


         
      
1
2
3
4
5
6
7
8
9
[INFO] ===============================================================================
[INFO] MUnit Coverage Summary
[INFO] ===============================================================================
[INFO]  * Resources: 1 - Flows: 1 - Processors: 2
[INFO]  * Application Coverage: 00.00%
[INFO]
[WARNING] ----------------------------- WARNING --------------------------------------
[WARNING]  * Application coverage is below defined limit. Required: 50.0% - Current: 00.00% (1)
[INFO] ====================================================================================
1 Warning detailing which coverage level wasn’t met
If no level is defined, -1 is assumed, which indicates that the build won’t fail due to low coverage.

Another feature is the ability to ignore either a flow or a file. This way, the ignored resource:

  • Doesn’t count as coverage data.

  • Doesn’t affect the overall number of message processors.

  • Doesn’t cause a build to fail if the flow is not tested or if the flow doesn’t reach coverage metrics.

In this topic: