Free MuleSoft CONNECT Keynote & Expo Pass Available!

Register now+
Nav

To Configure MUnit Maven Plugin From the POM File

Additional argument lines

Since the tests are being run in a new JVM, additional argument lines can be specified to the JVM. Each argument should be specified in separate argLine.

Argument lines

         
      
1
2
3
4
5
6
7
8
9
10
11
12
13
14
<plugins>
  <plugin>
    <groupId>com.mulesoft.munit.tools</groupId>
    <artifactId>munit-maven-plugin</artifactId>
    <configuration>
      ...
      <argLines>
          <argLine>-XX:MaxPermSize=512m</argLine>
          <argLine>-Xmx1024m</argLine>
      </argLines>
      ...
    </configuration>
  </plugin>
</plugins>

To Enable Coverage

To enable MUnit Coverage, add the following configuration to the MUnit Plugin:

MUnit Coverage - Minimal Configuration

         
      
1
2
3
4
5
6
7
8
9
10
11
12
13
<plugins>
  <plugin>
    <groupId>com.mulesoft.munit.tools</groupId>
    <artifactId>munit-maven-plugin</artifactId>
    <configuration>
      ...
      <coverage>
        <runCoverage>true</runCoverage>   (1)
      </coverage>
      ...
    </configuration>
  </plugin>
</plugins>
1 This enables the coverage feature

To Set a Minimum Coverage Level

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

To make the build fail, add the following lines to the configuration:

MUnit Coverage - Fail Build

         
      
1
2
3
4
<coverage>
  <runCoverage>true</runCoverage>
  <failBuild>true</failBuild>       (1)
</coverage>
1 Enable Fail Build Feature

Now, the next logical step is to define the coverage levels.

To Ignore Flows for Coverage Report

MUnit Coverage - Ignoring Flows

         
      
1
2
3
4
5
<coverage>
  <ignoreFlows>
          <ignoreFlow>the-name-of-your-flow</ignoreFlow>       (1)
  </ignoreFlows>
</coverage>
1 The name of the flow you want to ignore.

This is a list, so you can ignore as many flows as you need.

Dynamic Ports

When testing a Mule application in a continuous integration (CI) environment, you might encounter the following scenario:

Your application tries to open a specific port. The port is already in use. The application fails with a port binding exception.

The easy solution to this problem is to have your application use a free port. The MUnit Maven Plugin comes with a built-in feature to do just that.

MUnit Dynamic Ports instructs the MUnit Maven Plugin to look for unbound ports and reserve them before running the tests over the Mule application. Each port selected is placed in a system property under the name indicated in the configuration. The application can acquire the port number through the use of placeholders afterward.

The Ports to be selected by the plugin are taken from the following range: [40000,50000)

Dynamic Ports feature is only available as part of the MUnit Maven Plugin.
You can not expect this feature to work when running tests from inside Anypoint Studio.

Enabling Dynamic Ports

To enable the feature, you need to add the following code to the configuration section of the MUnit Maven Plugin:

Dynamic Ports Configuration

          
       
1
2
3
4
5
6
7
8
9
10
11
12
13
<plugins>
  <plugin>
    <groupId>com.mulesoft.munit.tools</groupId>
    <artifactId>munit-maven-plugin</artifactId>
    <configuration>
    ...
    <dynamicPorts>
      <dynamicPort>a.dynamic.port</dynamicPort>
    </dynamicPorts>
    ...
    </configuration>
  </plugin>
</plugins>

If you have the ${http.port} placeholder in your application, the configuration looks something like:

Example

          
       
1
2
3
<dynamicPorts>
  <dynamicPort>http.port</dynamicPort>
</dynamicPorts>

Preparing Your Application

A placeholder must parametrize the part of the application trying to make use of a port.
For instance, you may want to have your Mule application listening for HTTP traffic. To do that you should provide the following configuration:

HTTP Simple Application

           
        
1
2
3
4
5
6
7
<http:listener-config name="HTTP_Listener_config">
  <http:listener-connection host="0.0.0.0" port="8081" />
</http:listener-config>

<flow name="httpFlow">
  <http:listener path="/" config-ref="HTTP_Listener_config"/>
</flow>

Now this application always listens in port 8081. To make it dynamic, change it to:

HTTP Simple Application with dynamic port

           
        
1
2
3
4
5
6
7
<http:listener-config name="HTTP_Listener_config">
  <http:listener-connection host="0.0.0.0" port="${http.port}"/> (1)
</http:listener-config>

<flow name="httpFlow">
  <http:listener path="/" config-ref="HTTP_Listener_config"/>
</flow>
1 Notice the placeholder ${http.port}.

With the application coded in this way and the configuration of Dynamic Ports in place, your application starts each run listening on a different port.

Enable surefire reports

MUnit has built-in support for Surefire. No additional configuration is needed for this but it can be disabled if not needed.

Disabling surefire reports

         
      
1
<enableSurefireReports>false</enableSurefireReports>

The reports can be found under ${project.build.directory}/surefire-reports.

By default, it is set to true.

To Run Specific Tests


         
      
1
2
3
4
5
6
7
8
9
10
11
<plugins>
  <plugin>
      <groupId>com.mulesoft.munit.tools</groupId>
      <artifactId>munit-maven-plugin</artifactId>
      <configuration>
      ...
      <munitTest>example-MunitTest-suite.xml</munitTest>
      ...
    </configuration>
  </plugin>
</plugins>

To Run Tests With Specific Tags


         
      
1
2
3
4
5
6
7
8
9
10
11
<plugins>
  <plugin>
      <groupId>com.mulesoft.munit.tools</groupId>
      <artifactId>munit-maven-plugin</artifactId>
      <configuration>
      ...
      <munitTags>exampleMunitTag</munitTags>
      ...
    </configuration>
  </plugin>
</plugins>

You can specify more than one tag by separating them using a comma.

To Skip MUnit Tests


         
      
1
2
3
4
5
6
7
8
9
10
11
<plugins>
  <plugin>
      <groupId>com.mulesoft.munit.tools</groupId>
      <artifactId>munit-maven-plugin</artifactId>
      <configuration>
      ...
      <skipMunitTests>True</skipMunitTests>
      ...
    </configuration>
  </plugin>
</plugins>

To Skip Tests After One Suite Fails

MUnit allows you to skip the rest of the tests if one test suite fails.
If not specified, this value is false by default.


         
      
1
2
3
4
5
6
7
8
9
10
11
<plugins>
  <plugin>
      <groupId>com.mulesoft.munit.tools</groupId>
      <artifactId>munit-maven-plugin</artifactId>
      <configuration>
      ...
      <skipAfterFailure>true</skipAfterFailure>
      ...
    </configuration>
  </plugin>
</plugins>

To Specify the Runtime Product

MUnit allows you to specify the runtime version in which your applications being tested will run.


         
      
1
2
3
4
5
6
7
8
9
10
11
<plugins>
  <plugin>
    <groupId>com.mulesoft.munit.tools</groupId>
    <artifactId>munit-maven-plugin</artifactId>
    <configuration>
      ...
      <runtimeVersion>1.2.3</runtimeVersion>
      ...
    </configuration>
  </plugin>
</plugins>

To Specify The Runtime

MUnit allows you to specify the type of runtime in which your applications being tested will run.
The two possible values are MULE for community edition, and MULE_EE for Enterprise Edition.


         
      
1
2
3
4
5
6
7
8
9
10
11
<plugins>
  <plugin>
    <groupId>com.mulesoft.munit.tools</groupId>
    <artifactId>munit-maven-plugin</artifactId>
    <configuration>
      ...
      <runtimeProduct>MULE</runtimeProduct>
      ...
    </configuration>
  </plugin>
</plugins>

Environment Variables

To set additional environment variables during the test run, you can specify them with the respective key and value.

Additional Environment Variables

         
      
1
2
3
4
5
6
7
8
9
10
11
12
13
14
<plugins>
  <plugin>
    <groupId>com.mulesoft.munit.tools</groupId>
    <artifactId>munit-maven-plugin</artifactId>
    <configuration>
      ...
      <environmentVariables>
        <MY_ENV>exampleValue</MY_ENV>
        <MY_OTHER_ENV>val2</MY_OTHER_ENV>
      </environmentVariables>
      ...
    </configuration>
  </plugin>
</plugins>

Environment variables can be used to replace placeholders such as ${MY_ENV} (using the example above).

Redirect Test Output to File

When running several tests, the build output can get very complex to read. You may want to redirect the output of each Test suite to a file. This way what remains in the build output will be the test results and to check the standard output of each test suite you can find it in its respective file.

These files will be located in the testOutputDirectory folder following this naming convention: munit.${suiteName}-output.txt, where the suiteName represents the name of the XML file relative to the MUnit test folder.

The test run output that doesn’t belong to a particular suite won’t be printed to keep the build output clean, but it can be enabled by running maven in debug mode.

Redirect test output to file

         
      
1
2
3
4
5
6
7
8
9
10
11
<plugins>
  <plugin>
    <groupId>com.mulesoft.munit.tools</groupId>
    <artifactId>munit-maven-plugin</artifactId>
    <configuration>
      ...
      <redirectTestOutputToFile>true</redirectTestOutputToFile>
      ...
    </configuration>
  </plugin>
</plugins>

By default, it is set to false

System Properties Variables

You may wish to define specific system variables needed for your MUnit test to run successfully. The example below shows how you can send them.

Setting system property variables

         
      
1
2
3
4
5
6
7
8
9
10
11
12
13
<plugins>
  <plugin>
    <groupId>com.mulesoft.munit.tools</groupId>
    <artifactId>munit-maven-plugin</artifactId>
    <configuration>
      ...
      <systemPropertyVariables>
        <my.property.key>my.property.value</my.property.key>
      </systemPropertyVariables>
      ...
    </configuration>
  </plugin>
</plugins>

Depending on the execution context, the system properties values may vary. When referencing these properties, it is a good practice to override their value to enforce test reproducibility.

You can do so using the ­-D argument when running MUnit with Maven.
Variables passed with the -D argument take full priority over any other property.

For example:

-Dmy.property.key=my.property.another.value

Test Output Directory

You may want to choose the location where the test output files will be created. The path specified can be absolute or written as a maven placeholder.

Test output directory with absolute path

         
      
1
2
3
4
5
6
7
8
9
10
11
<plugins>
  <plugin>
    <groupId>com.mulesoft.munit.tools</groupId>
    <artifactId>munit-maven-plugin</artifactId>
    <configuration>
      ...
      <testOutputDirectory>/my/absolute/path</testOutputDirectory>
      ...
    </configuration>
  </plugin>
</plugins>
Test output directory using maven placeholders

         
      
1
<testOutputDirectory>${project.build.directory}/my/output/folder</testOutputDirectory>

By default, the files will be created in ${project.build.directory}/munit-reports/output/.