Mule - How to fail jenkins pipeline when code coverage is less than predefined threshold?

698 Views Asked by At

I have a built a Mule API with Munits. I would like to build and deploy the application using jenkins pipeline. However, before build & deploy, I would like to execute maven clean test and check if code coverage is less than some x%. if yes, then I would like to fail the build/deployment to Mule cloud hub.

Is there a Jenkins plugin or any option to fail the jenkins pipeline when the code coverage is less than pre-defined threshold (let's say 80%)?

Thanks, Bala

1

There are 1 best solutions below

0
On

Looks like the topic is covered in MUnit documentation for coverage in Maven:



<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>
        <coverage>
            <runCoverage>true</runCoverage>
            <failBuild>false</failBuild>

            <requiredApplicationCoverage>75</requiredApplicationCoverage>
            <requiredResourceCoverage>50</requiredResourceCoverage>
            <requiredFlowCoverage>50</requiredFlowCoverage>

            <formats>
                <format>console</format>
                <format>html</format>
            </formats>
        </coverage>
    </configuration>
</plugin>

If you set the appropriate threshold (for example requiredApplicationCoverage) to the desired value it will fail the Maven test phase if coverage is not high enough. Since Jenkins just executes Maven it will fail too.