About MUnit Test

The MUnit Test is the basic processor of an MUnit Test Suite. It represents each test scenario you want to try.

The MUnit Test is divided in three scopes:

Scope Description


The behavior scope is meant to set all the preconditions before executing the test logic.
Mocks and Spies should go in this section.


The execution scope is meant to have the testing logic which will be wait for all processes to finish before executing the next scope.


The validation scope is meant to have all the validations regarding the result of the execution scope.
Assertions and Verifications should go here.

All these scopes are optional.

<munit:test name="exampleTest" description="Test to verify scenario 1">>


         <flow-ref name="exampleFlow"/>

         <munit-tools:assert-that ...>
Figure 1. A Studio representation of the MUnit Test

About MUnit Test Description Attribute

In MUnit, it’s mandatory that you write a description in your test.
Ideally, you should write a useful, representative description of the scenario you are testing. This description displays in the test console before running the test, and also in the reports.
The more representative the description, the easier to read and troubleshoot any failures.

About MUnit Test Ignore Attribute

There may be scenarios where you need to shut down a test. Whether this be because the test is failing or because it has nasty side effects.
The point is you shouldn’t have to comment out the code.

In this case, MUnit allows you to ignore a specific test.

You can ignore any of your tests by adding the ignore boolean to the test definition.

<munit:test name="my-flow-Test"
      description="Test to verify scenario 1">
Valid values for ignore are true and false. If the attribute is not present, the default is false.
Figure 2. In Anypoint Studio, you can check the Ignore Test option.

About MUnit Test Tag Attribute

MUnit also allows you to tag your tests, so you can choose to run tests under a specific tag. For example:

  description="A test that works as an example"
  tags="integration,http" >
Figure 3. In Anypoint Studio, you can define tags in the proper field.

About MUnit Test Expected Error and Exception Attributes

Sometimes, the only thing you want to validate is that the flow or sub-flow you are testing fails and throws a specific error, which depends on the business logic being tested. In these cases, MUnit provides a simple way to validate the scenario.
You can add the attribute expectedErrorType and expectException, as shown below:

<munit:test name="MUnit-test-suite"
  description="A test that works as an example"
munit test concept e7280
Figure 4. A visual representation on Anypoint Studio
  • The attribute expectedErrorType expects an error ID that needs to be defined inside the application being tested.

    This attribute allows you to validate that a defined error type in your application is thrown. If you define an errorType that does not exists in your application, the test does not run.

    <munit:test name="MUnit-test-suite"
      description="Test Error Type"

    This Error Type test expects that an FTP operation will throw an FTP:ILLEGAL_PATH error.
    If none of your flows are using using an FTP connector, then the error type is not defined within your application and the test will not run.

    ErrorTypes that belong to the Mule Runtime does not need to be typified. For example, MULE:RETRY_EXHAUSTED can be defined as simply RETRY_EXHAUSTED.
  • The attribute expectException expects a literal exception class name (canonical form).

    When you provide a literal value, it should take the form of the canonical class name of the exception that is expected. In these cases, Mule always throws a MuleMessagingException. MUnit validates the provided classname if the underlying cause of the MuleMessagingException thrown is of the exact same type.

    <munit:test name="testExceptions"
      description="Test Exceptions"

    If you define that your test expects an exception and none is thrown, the test fails immediately.

Was this article helpful?

💙 Thanks for your feedback!

Edit on GitHub
Give us your feedback!
We want to build the best documentation experience for you!
Help us improve with your feedback.
Take the survey!