# Part 4: CI/CD pipeline with MuleSoft and GitHub Actions - MUnit minimum coverage percentage

> In this post, we'll see the steps to add the minimum coverage percentage for the MUnit tests in order to pass the pipeline.

- **Author:** Alex Martinez
- **Published:** May 2, 2023
- **Category:** Tutorials
- **Tags:** MuleSoft, GitHub Actions, MUnit, CI/CD
- **Source:** https://prostdev.com/post/part-4-ci-cd-pipeline-with-mulesoft-and-github-actions-munit-minimum-coverage-percentage

---
## Series: CI/CD Pipeline with MuleSoft & GitHub Actions (Part 4 of 6)

1. [Part 1: How to set up a CI/CD pipeline to deploy your MuleSoft apps to CloudHub using GitHub Actions](https://prostdev.com/post/how-to-set-up-a-ci-cd-pipeline-to-deploy-your-mulesoft-apps-to-cloudhub-using-github-actions)
2. [Part 2: CI/CD pipeline with MuleSoft and GitHub Actions - secured/encrypted properties](https://prostdev.com/post/part-2-ci-cd-pipeline-with-mulesoft-and-github-actions-secured-encrypted-properties)
3. [Part 3: CI/CD pipeline with MuleSoft and GitHub Actions - MUnit testing](https://prostdev.com/post/part-3-ci-cd-pipeline-with-mulesoft-and-github-actions-munit-testing)
4. Part 4: CI/CD pipeline with MuleSoft and GitHub Actions - MUnit minimum coverage percentage (this post)
5. [Part 5: CI/CD pipeline with MuleSoft and GitHub Actions - Enabling MFA through a Connected App](https://prostdev.com/post/part-5-ci-cd-pipeline-with-mulesoft-and-github-actions-enabling-mfa-through-a-connected-app)
6. [Part 6: CI/CD pipeline with MuleSoft and GitHub Actions - Deploying to CloudHub 2.0](https://prostdev.com/post/part-6-ci-cd-pipeline-with-mulesoft-and-github-actions-deploying-to-cloudhub-2-0)

---

In the [previous article](https://www.prostdev.com/post/part-3-ci-cd-pipeline-with-mulesoft-and-github-actions-munit-testing), we learned how to set up our Nexus credentials to be able to run the MUnits from the CI/CD pipeline. If you haven’t been following the series or you’re not familiar with GitHub Actions, we recommend you start from the [first article](https://www.prostdev.com/post/how-to-set-up-a-ci-cd-pipeline-to-deploy-your-mulesoft-apps-to-cloudhub-using-github-actions) to understand how we are setting up all the configurations we need.

In this post, we’ll see the steps to add the minimum coverage percentage for the MUnit tests in order to pass the pipeline.

## Prerequisites

You should already have all the setup in your Mule application for running the MUnits in a CI/CD pipeline. In summary, this is what you should already have:

- The required dependencies, plugins, or properties in your `pom.xml` to be able to run MUnits for your Mule project.
- The working MUnit tests under `src/test/munit`.
- A working CI/CD pipeline. See the [previous article](https://www.prostdev.com/post/part-3-ci-cd-pipeline-with-mulesoft-and-github-actions-munit-testing) if you still need to set this up.

## Modify your pom.xml

This time there’s nothing to add/modify in the other files (`build.yml` / `settings.xml`). We’ll simply add the following configuration to our `pom.xml` under the existing `munit-maven-plugin` configuration.

```xml
<plugin>
  <groupId>com.mulesoft.munit.tools</groupId>
  <artifactId>munit-maven-plugin</artifactId>
  <version>${munit.version}</version>
  <executions>
    <execution>
      <id>test</id>
      <phase>test</phase>
      <goals>
        <goal>test</goal>
        <goal>coverage-report</goal>
      </goals>
    </execution>
  </executions>
  <configuration>
    <!-- START: MUnit coverage -->
    <environmentVariables>
      <secure.key>${decryption.key}</secure.key>
    </environmentVariables>
    <!-- END: MUnit coverage -->
    <coverage>
      <runCoverage>true</runCoverage>
      <!-- START: MUnit coverage -->
      <failBuild>true</failBuild>
      <requiredApplicationCoverage>90</requiredApplicationCoverage>
      <!-- <requiredResourceCoverage>50</requiredResourceCoverage> -->
      <!-- <requiredFlowCoverage>50</requiredFlowCoverage> -->
      <!-- END: MUnit coverage -->
      <formats>
        <!-- START: MUnit coverage -->
        <format>console</format>
        <format>sonar</format>
        <format>json</format>
        <!-- END: MUnit coverage -->
        <format>html</format>
      </formats>
    </coverage>
  </configuration>
</plugin>
```

Let’s explore each of the changes one by one. Starting with the `environmentVariables`.

```xml
<environmentVariables>
   <secure.key>${decryption.key}</secure.key>
</environmentVariables>
```

We are adding this variable right here because it needs to be passed down to the Mule application to be able to decrypt the existing secured properties.

> [!NOTE]
> If you’re not sure how to do this setup or want to understand where we are using the `secure.key` or `decryption.key`, please refer to [Part 2](https://www.prostdev.com/post/part-2-ci-cd-pipeline-with-mulesoft-and-github-actions-secured-encrypted-properties) of this series.

Next, we added the configuration to set the minimum coverage percentage for the application to pass the build.

```xml
<failBuild>true</failBuild>
<requiredApplicationCoverage>90</requiredApplicationCoverage>
<!-- <requiredResourceCoverage>50</requiredResourceCoverage> -->
<!-- <requiredFlowCoverage>50</requiredFlowCoverage> -->
```

In this case, we only set up the `requiredApplicationCoverage` option with a minimum percentage of 90. If you instead (or additional to) want to set up the percentage for the resources or the flows, you can use the other two options. To see the difference in these settings, you can refer to [this live stream](https://youtu.be/Z0ORL0yoKCo) or the [official documentation](https://docs.mulesoft.com/munit/latest/coverage-maven-concept#set-up-a-minimum-coverage-level).

Finally, you can generate different coverage reports to be able to see your statistics in several formats.

```xml
<formats>
	<format>console</format>
	<format>sonar</format>
	<format>json</format>
	<format>html</format>
</formats>
```

You can choose to generate some of them or all of them. Simply add/remove the `format` field from this configuration.

> [!DOCS]
> For more information, see [Coverage Report Types](https://docs.mulesoft.com/munit/latest/coverage-maven-concept#coverage-report-types).

> [!NOTE]
> If you want to be able to see your generated reports from the CI/CD pipeline, you can add the following configuration under the `test` job in your `build.yml` file. Add this as the last step. You will be able to download the reports after the whole pipeline has finished running (see the next screenshot).

```yaml
- name: Upload MUnit reports
  uses: actions/upload-artifact@v3
  with:
    name: munit-test-reports
    path: target/site/munit/coverage/
```

## Run the pipeline

That’s it! Once you’re done with the changes, simply push a new change to the **main** branch and this will trigger the pipeline.

![Successful GitHub Actions run with test, build, and deploy jobs and two artifacts](../../assets/blog/part-4-ci-cd-pipeline-with-mulesoft-and-github-actions-munit-minimum-coverage-percentage-2.png)

## More resources

You can check out my [GitHub profile](https://github.com/alexandramartinez) for more CI/CD repos:

- [github-actions](https://github.com/alexandramartinez/github-actions) to deploy a Mule app to CloudHub
- [dataweave-utilities-library](https://github.com/alexandramartinez/dataweave-utilities-library) to publish a DataWeave library to Exchange
- [api-catalog-cli-example](https://github.com/alexandramartinez/api-catalog-cli-example) to update APIs in Exchange using the API Catalog CLI

I hope this was helpful!

Don't forget to subscribe so you don't miss any future content.

---

## FAQs

### How do I set a minimum MUnit coverage percentage that fails the pipeline?

Add a `coverage` block to the existing `munit-maven-plugin` configuration in your `pom.xml` with `failBuild` set to `true` and `requiredApplicationCoverage` set to your minimum percentage; in this post it is set to 90, so the build fails if application coverage drops below that.

### What's the difference between requiredApplicationCoverage, requiredResourceCoverage, and requiredFlowCoverage?

`requiredApplicationCoverage` sets the minimum percentage for the whole application, while `requiredResourceCoverage` and `requiredFlowCoverage` set the minimums for resources and flows respectively; this post only uses `requiredApplicationCoverage` at 90 and leaves the other two commented out, and points to a live stream at https://youtu.be/Z0ORL0yoKCo and the official docs at https://docs.mulesoft.com/munit/latest/coverage-maven-concept#set-up-a-minimum-coverage-level for the differences.

### Why do I need to add the secure.key environment variable to the plugin configuration?

The `environmentVariables` block adds `secure.key` mapped to `${decryption.key}` because it needs to be passed down to the Mule application so it can decrypt the existing secured properties when the MUnits run; Part 2 of the series at https://www.prostdev.com/post/part-2-ci-cd-pipeline-with-mulesoft-and-github-actions-secured-encrypted-properties covers where those keys come from.

### Which coverage report formats can I generate, and how do I choose them?

Under the `formats` section you can generate `console`, `sonar`, `json`, and `html` reports, and you choose which ones by simply adding or removing the `format` field from the configuration, generating some or all of them.

### Do I need to change build.yml or settings.xml to add the minimum coverage percentage?

No, this step requires nothing to be added or modified in `build.yml` or `settings.xml`; you only add the coverage configuration to the `pom.xml`, though you can optionally add an upload-artifact step to `build.yml` if you want to download the generated reports after the pipeline finishes.