= MUnit Maven Plugin ifndef::env-site,env-github[] include::_attributes.adoc[] endif::[] :version-info: 2.0 and newer :keywords: munit, testing, unit testing :page-aliases: munit-maven-support.adoc, to-set-up-munit-maven-plugin.adoc, faq-working-with-parent-pom.adoc, to-inherit-parent-plugin.adoc, to-ignore-parent-plugin.adoc, to-override-parent-plugin.adoc, munit-maven-plugin-configuration.adoc,to-configure-munit-maven-plugin-maven.adoc MUnit provides a Maven plugin that enables you to run your MUnit tests as part of your continuous integration environment. The following configurations to the `pom.xml` or `settings.xml` files of your Maven installation apply only when running Munit through Maven. == Set Up MUnit Maven Plugin This documentation assumes you have the `` property in your pom.xml. * Enable the MUnit Maven plugin in your `pom.xml` file: + .MUnit Maven Plugin [source,xml,linenums] ---- ... com.mulesoft.munit.tools munit-maven-plugin ${munit.version} test test test ... ---- * Add MUnit dependencies to your `pom.xml` file: + .Dependencies [source,xml,linenums] ---- ... com.mulesoft.munit munit-runner ${munit.version} mule-plugin test com.mulesoft.munit munit-tools ${munit.version} mule-plugin test ... ---- * Configure the repositories for the MUnit dependencies and plugin: + .Repositories [source,xml,linenums] ---- mulesoft-releases MuleSoft Releases Repository https://repository.mulesoft.org/releases/ default ---- + .Plugin Repositories [source,xml,linenums] ---- mulesoft-release mulesoft release repository default https://repository.mulesoft.org/releases/ false ---- MUnit has Surefire support built in. The reports output to `target/surefire-reports`. === Handle Parent POM Files You can declare the MUnit plugin in a parent `pom.xml` file and every child project under this file can reference this definition. To configure the MUnit Maven plugin in a parent-child POM relationship, include the MUnit plugin declaration in the `` section of your parent `pom.xml` file. In the following example, the `pirate-pom` project declares the MUnit Maven plugin in its plugin management section, which defines a global configuration that each child under this parent can reference, override or even ignore. [source,xml,linenums] ---- 4.1.0 org.pirate pirate-pom 1.0-SNAPSHOT pom 2.3.0 com.mulesoft.munit.tools munit-maven-plugin ${munit.version} test test test true false 0 0 0 console html ---- === Inherit a Parent Plugin To inherit the MUnit plugin in your child `pom.xml` files, reference it in the `` section of each child POM file individually. The following sample references `pirate-pom` file as its parent and then declares the MUnit plugin in a `` section without specifying its `` because the MUnit plugin configuration is inherited from the `` section in the parent POM file: .POM child file sample [source,xml,linenums] ---- org.pirate pirate-pom 1.0-SNAPSHOT 4.0.0 com.mycompany ninja 1.0.0-SNAPSHOT mule Mule ninja Application UTF-8 UTF-8 4.1.4 2.3.0 com.mulesoft.munit.tools munit-maven-plugin ---- === Ignore a Parent Plugin Each child project under a parent `pom.xml` file can ignore the plugin referenced in the parent plugin management section and not implement the plugin configuration declared there. By not declaring the Munit Plugin in your `` section, you avoid inheriting the plugin declared in *pirate-pom*: .Child POM file not inheriting the MUnit Maven Plugin [source,xml,linenums] ---- org.pirate pirate-pom 1.0-SNAPSHOT 4.0.0 com.mycompany ninja 1.0.0-SNAPSHOT mule Mule ninja Application UTF-8 UTF-8 4.1.0 2.3.0 ---- === Override a Parent Plugin When inheriting the plugin from a parent POM file, you can also override the parent configuration. Overriding the plugin configuration from your parent POM file suppresses the original configuration requiring you to declare all necessary configurations again. The following example overrides the console coverage report from its parent POM file, replacing it by the HTML report. Because the values from the other elements in the parent (``, ``, ``, ``) are not being referenced, this child file won't inherit them and the default values will apply: .Coverage configuration in parent POM file [source,xml,linenums] ---- true false 0 0 0 console html ---- .POM child file overriding coverage report's format [source,xml,linenums] ---- com.mulesoft.munit.tools munit-maven-plugin true html ---- == Set Up Coverage The following configurations only apply when you execute your MUnit tests using the Maven plugin. They do not apply when running tests from Studio. See xref:coverage-maven-concept.adoc[Using Coverage in Maven] for more information. include::page$coverage-maven-concept.adoc[tags=coverage-report;!report-types,leveloffset=+1] == Dynamic Ports MUnit 2.2 and later introduce the `dynamic-port` global element, that allows you to define dynamic ports at the MUnit suite level. Using this element instead of the plugin configuration described as follows allows you to set the dynamic port both from Maven and Studio. + See xref:dynamic-ports.adoc[Dynamic Ports] to learn how to configure this element. 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 MUnit Maven Plugin comes with a built-in feature to make your application use a free port. 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 range: `[40000,50000]` [CAUTION] -- Dynamic Ports feature is only available as part of the MUnit Maven Plugin. This feature does not work when running tests from inside Anypoint Studio. -- === Enable 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 [source,xml,linenums] ---- com.mulesoft.munit.tools munit-maven-plugin ... a.dynamic.port ... ---- If you have the `${http.port}` placeholder in your application, the configuration looks something like: .Example [source,xml,linenums] ---- http.port ---- === Prepare Your Application A placeholder must parameterize the part of the application trying to make use of a port. For example, to have your Mule application listening for HTTP traffic, provide the following configuration: .HTTP Application [source,xml,linenums] ---- ---- The previous application always listens in port `8081`. To make it dynamic, change it to: .HTTP Application with dynamic port [source,xml,linenums] ---- //<1> ---- <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, but you can disable it if you don't need it: .Disable surefire reports [source] ---- false ---- The reports can be found under `${project.build.directory}/surefire-reports`. By default, it is set to `true`. == Enable Sonar Reports MUnit has built-in support for SonarQube, with a default setting of `true` for `enableSonarReports`. No additional configuration is needed. You can find the reports under `${project.build.directory}/sonar-reports`. You can disable SONAR reports if you don't need them: .Disable sonar reports [source] ---- false ---- The reports can be found under `${project.build.directory}/sonar-reports`. By default, it is set to `true`. == Run Tests Using the Plugin === Run MUnit Tests for a Mule Application .Run MUnit tests in a Single Resource Mule project. [source] ---- mvn clean test ---- === Run a Specific MUnit Test Suite By using the property `munit.test`, you can instruct the MUnit Maven plugin to run only tests that belong to a specific test suite: [source,maven,linenums] ---- mvn clean test -Dmunit.test= ---- This path is relative to `src/test/munit`. The property `munit.test` accepts regular expressions. The expression is applied to the name of the MUnit Test Suite file. The regular expression language is the Java implementation. + For example: [source,console] ---- mvn clean test -Dmunit.test=.*my-test.* ---- You can leverage this feature by adding naming conventions to your MUnit Test suites. === Run Specific MUnit Tests In the same way that you instruct MUnit to run one test suite, you can also configure it to run a specific test inside that test suite. You must use the property `munit.test` with the addition of the the special character `#` to append the test name: [source,console] ---- mvn clean test -Dmunit.test=# ---- It also accepts regular expressions. The expression is applied to the attribute `name` of the MUnit Test. For example: [source,console] ---- mvn clean test -Dmunit.test=.*my-test.*#.*test-scenario-1.* ---- The tests inside the MUnit Test Suite that don't match the regular expression are flagged as *ignored*. You can also configure this from the `pom.xml` configuration: [source,xml,linenums] ---- com.mulesoft.munit.tools munit-maven-plugin ... example-MunitTest-suite.xml ... ---- === Run Tests Using a Specific Tag You can run only the tests that you grouped under one specific tag: [source,console] ---- mvn clean test -Dmunit.tags= ---- You can also configure this from the `pom.xml` configuration: [source,xml,linenums] ---- com.mulesoft.munit.tools munit-maven-plugin ... exampleMunitTag ... ---- You can specify more than one tag by separating them using a comma. === Skip All Tests MUnit leverages the same mechanism as Maven. To skip tests, you must use the `skipTests` parameter: .Skip Tests example [source,console] ---- mvn clean package -DskipTests ---- You can also configure this from the `pom.xml` configuration: [source,xml,linenums] ---- com.mulesoft.munit.tools munit-maven-plugin ... true ... ---- === Skip Tests After One Suite Fails MUnit allows you to skip the rest of the tests if one test suite fails: [source,xml,linenums] ---- com.mulesoft.munit.tools munit-maven-plugin ... true ... ---- If not specified, this value is `false` by default. [[specify-runtime-product]] == Specify The Runtime Product You can specify the type of runtime in which your applications being tested will run. Accepted values are: * `MULE` for community edition. * `MULE_EE` for enterprise edition. [source,xml,linenums] ---- com.mulesoft.munit.tools munit-maven-plugin ... MULE ... ---- === Specify the Runtime Product Version MUnit allows you to specify the runtime version in which your applications being tested will run. [source,xml,linenums] ---- com.mulesoft.munit.tools munit-maven-plugin ... 4.2.2 ... ---- == Specify The JVM You can specify the JVM (or the java executable) in which your applications being tested run. You must populate the `munit.jvm` parameter with the path to the executable: .Specify the JVM [source,console] ---- mvn clean package -Dmunit.jvm=/path/to/jdk/bin/java ---- You can also configure this from the `pom.xml` configuration: [source,xml,linenums] ---- com.mulesoft.munit.tools munit-maven-plugin ... /path/to/jdk/bin/java ... ---- === Additional Argument Lines You can pass additional argument lines to the JVM. Specify each argument in a separate `argLine`: .Argument lines [source,xml,linenums] ---- com.mulesoft.munit.tools munit-maven-plugin ... -XX:MaxPermSize=512m -Xmx1024m ... ---- === Environment Variables To set additional environment variables during the test run, you can specify them with the respective key and value: .Additional Environment Variables [source,xml,linenums] ---- com.mulesoft.munit.tools munit-maven-plugin ... exampleValue val2 ... ---- As shown in the previous example, you can use environment variables to replace placeholders such as `$\{MY_ENV}`. === System Properties Variables You may need to define specific system variables so that your MUnit test runs successfully: .Setting system property variables [source,xml,linenums] ---- com.mulesoft.munit.tools munit-maven-plugin ... my.property.value ... ---- 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: [source] ---- -Dmy.property.key=my.property.another.value ---- == Redirect Test Output to File When running several tests, the build output can get very complex to read. MUnit allows you to redirect the output of each test suite to a file. This way, only the test results will remain in the build output and to you can check the respective file to check the standard output of each test suite. These files are located in the `testOutputDirectory` folder following the naming convention: `munit.$\{suiteName}-output.txt`, where the `suiteName` represents the name of the XML file relative to the MUnit test folder. The test's 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. To redirect the output of each test suite to a file: .Redirect test output to file [source,xml,linenums] ---- com.mulesoft.munit.tools munit-maven-plugin ... true ... ---- By default, it is set to `false` === 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 [source,xml,linenums] ---- com.mulesoft.munit.tools munit-maven-plugin ... /my/absolute/path ... ---- .Test output directory using maven placeholders [source,xml,linenums] ---- ${project.build.directory}/my/output/folder ---- By default, the files are created in `${project.build.directory}/munit-reports/output/`. == MUnit Maven Plugin Configuration Reference The MUnit Maven Plugin offers a set of optional parameters that can be configured. //_NOTE: This params are taken from MUnitMojo.java in munit-runtime code. .Maven Plugin Parameters [%header,cols="3a,^2a,4a"] |=== |Name |Type | Description |`argLines` |List |Additional JVM argument lines to set on the test run. | `coverage` | Configuration | Coverage configuration to be set on the test run. |`dynamicPorts` |List |Dynamic ports to be set on the test run. |`enableSurefireReports` |boolean |Set value to `true` to generate MUnit test results in the surefire format. + Default value: `true` | `munitTest` | String | Name of the MUnit tests to run. | `munitTags` | String | Name of the MUnit tags. Only tests tagged with these names will run. | `skipMunitTests` | boolean | Set to `true` when you want to skip all MUnit tests. + Default value: `false` | `skipAfterFailure` | boolean | Skip all tests if one fails. + Default value: `false` | `munitTestsDirectory` | File | Directory where the MUnit tests reside. + Default value: `${project.build.directory}/test-mule/munit` | `runtimeVersion` | String | Version of the Mule runtime + When you run your tests from Maven, this value is taken from the `minMuleVersion` parameter in the `mule-artifact.json` file. + To modify the Mule runtime version used to run your MUnit tests in Studio, see xref:runtime-patching.adoc#studio[Runtime Patching in Studio]. | runtimeProduct | String | Type of runtime. Expected values are: * `MULE` for community edition * `MULE_EE` for enterprise edition. By default, this value is taken from the `requiredProduct` parameter in the `mule-artifact.json` file. | `jvm` | String | Option to specify the JVM (or path to the java executable) to use. By default, the JVM will the same VM as the one used to run Maven. |`environmentVariables` |Map |Additional environment variables to be set on the test run. |`redirectTestOutputToFile` |boolean |Set value to `true` to redirect the MUnit tests standard output to a file. + Default value: `false` |`systemPropertyVariables` |Map |System properties to be set on the test run. |`testOutputDirectory` |File |Directory where the test outputs will be written to. + Default value: `${project.build.directory}/munit-reports/output/` |===