Using MUnit in Anypoint Studio
To use MUnit in Studio, you have to install it from the update site.
To install MUnit, follow these steps:
Verify that your version of Studio is 5.2.0 (July 2015 release) or newer.
Add the MUnit update site:
Go to Help > Install New Software.
Studio displays the Available Software window. In the Work with: field, paste the MUnit update site:
Check Munit and Munit Tools for Mule, as shown below.
Click Next, then complete the installation wizard.
MUnit Studio integration is mainly built around XML-based tests. Most of the MUnit-Studio integration tools guide you in the creation of these tests.
There are two basic ways to create a new MUnit test in Studio:
Right-click a specific flow by right clicking the flow and selecting MUnit.
Use the wizard, which allows you to create a test for any flow or application in the workspace.
The most basic ones are by right-clicking the flow you want to test, or by using the wizard.
To create a test for a specific flow, right-click the flow, then select MUnit > Create a new <flow_name> suite.
Creates a new test suite named after the XML file where the flow resides, in this case
munit2.xml. Studio displays the test suite on a new tab, next to the tab for the original application:
Imports the XML file to the test suite
Creates a new MUnit test like the one shown below:
To add an MUnit message processor to the test shown above, drag it from the Palette to the Test area after the flow-ref.
Below is the XML file automatically generated for the new test suite.
1 2 3 4 5 6 7 8 9 10 11 12 13 <?xml version="1.0" encoding="UTF-8"?> <mule xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:munit="http://www.mulesoft.org/schema/mule/munit" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation" xmlns:spring="http://www.springframework.org/schema/beans" xmlns:core="http://www.mulesoft.org/schema/mule/core" version="EE-3.7.3" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.mulesoft.org/schema/mule/munit http://www.mulesoft.org/schema/mule/munit/current/mule-munit.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd"> <munit:config name="munit" doc:name="MUnit configuration"/> <spring:beans> <spring:import resource="classpath:munit2.xml"/> </spring:beans> <munit:test name="munit2-test-suite-munit2FlowTest" description="Test"> <flow-ref name="munit2Flow" doc:name="Flow-ref to munit2Flow"/> </munit:test> </mule>
The wizard allows you to create a test for any flow or application in your Studio workspace.
To create a test via the wizard, go to File > New > MUnit Test. Studio displays the MUnit test creation wizard, shown below.
As you can see, the wizard allows you to select any of the flows and applications in the workspace.
If you want to create a test that does not reference a specific flow, click the Create empty test checkbox.
|You can also create a new test via the wizard by right-clicking the desired application in the Package Explorer pane, then selecting MUnit > Create new suite.|
Studio displays the newly-created test suite in its own canvas. Here you can tailor your test suite using the Studio interface, just like a regular application. The Studio Palette displays two new sections: MUnit and MUnit Integration Test Tools. To quickly see all MUnit message processors, type
munit in the Palette search filter.
You can edit run configurations for your suites/tests in Studio, just like any other run configuration. A run configuration must contain at least the project and the suite; additionally, you can specify a test if you want to run only that test.
To edit the configuration for your suit or test in Studio, follow these steps:
In the Package Explorer, right click the .xml file for your suite or test, then select Properties.
In the Properties window, click Run/Debug Settings, then click New.
In the Select Configuration Type window, click MUnit, then click OK.
The configuration window displays the three tabs for editing configuration properties: Test, Arguments and Environment, shown below:
If the test name is blank, all tests in the given suite are run.
To add JVM arguments, click the Arguments tab, then enter your arguments in the VM arguments input field. In the image below,
java.library.path is being added as an argument.
| If your path contains a space followed by a dash, anything after the dash is parsed as a new JVM argument. For example, if the path is
To run a test suite, right-click the empty canvas where the suite resides, then select Run MUnit suite.
Studio displays the output from the running suite in the console.
To run a test, right-click the name of the test, then select Run MUnit Test.
To check that the test is actually running, view the output in the console. In order not to overwhelm the user, the default output provides little information, but enough to verify that the test has run.
An alternative way to run a test is to use the MUnit tab (see below).
Select the desired test in the MUnit tab.
Right-click the test, then select Run.
|In the event you have failed tests, you can select the button run failed. This only runs tests that fail.|
Studio displays MUnit test results in the MUnit tab of the left-hand explorer pane, outlined below:
The MUnit tab displays successful tests in green, failed tests in red.
The Coverage button in the image above allows you to see what flow the test covered, and the percentage of message processors in the flow that was covered by the test:
If you run more than one test, the MUnit tab displays a history of run tests. For failed tests, the Errors button displays the stack trace, which you can copy to your clipboard. To copy the stack trace, right-click the name of the failed test, then select Copy Stack Trace.
As you can see in the image above, you can also use the MUnit tab to Running Your Test or debug your test, by selecting the appropriate menu option.
Clicking the debug button or the play button on the top right causes the last run or debug to be rerun. This rerun includes all tests that were run on the previous run. You can also select a single test from the previous run to rerun on its own.
You can debug MUnit tests just like Studio applications, using Studio’s debugging perspective (for details on the debugging UI, see Studio Visual Debugger).
To access the debugging perspective, click Mule Debug on the top right of the Studio toolbar. This takes you away from the default Mule Design perspective to the debugging perspective, which displays debugging controls.
As with Mule applications, you can mark an MUnit message processor as a breakpoint, where a debug run should stop to enable you to see the information that reaches the message processor.
To debug a test, you can:
Right-click the test in the canvas, then select Debug MUnit test:
If you are working in the MUnit tab, you can select a test that previously ran, click the debug icon on the top right:
|In the event you have failed test you can select the button debug failed. This only debugs tests that fail.|
Or right-click the desired test, then select Debug: