Contact Us 1-800-596-4880

Run Custom Event Processor

logo acb active Anypoint Code Builder

logo studio active Anypoint Studio

The Run Custom event processor enables you to assert the Mule event content against a custom assertion.

To add the Run Custom processor to your project, add the following dependency to your pom.xml file:

<dependency>
    <groupId>com.mulesoft.munit</groupId>
    <artifactId>munit-assert</artifactId>
    <version>${munit.version}</version>
    <scope>test</scope>
</dependency>

You also have to add your custom assertion class to the mule-artifact.json file as an exported resource.

For example, you can define the following assertion in Java:

package org.mule.custom;
import org.mule.munit.assertion.api.MunitAssertion;
import org.mule.munit.assertion.api.TypedValue;

public class CustomAssertion implements MunitAssertion {

    @Override
    public void execute(TypedValue expression, Object params) throws AssertionError { (1)
        if (!"Hello World".equals(expression.getValue())) { (2)
            throw new AssertionError("Error the payload is incorrect");
        }
     }

}
1 Implement the only method in the interface public void execute(TypedValue expression, Object params) throws AssertionError.
2 Run your custom logic, which in this case validates that the message payload is Hello World.

The CustomAssertion class must be located in the scr/test/java directory.

And use the Run Custom Event Processor to run it:

<munit-tools:run-custom
  assertion="org.mule.custom.CustomAssertion" (1)
  expression="#[payload]"/>
1 The assertion field needs to have the canonical class name of your custom assertion and the class needs to be exported.

To export it when packaging your application, add the org.mule.custom package to your mule-artifact.json

{
    "name": "mule-application",
    "minMuleVersion": "4.2.0",
    "classLoaderModelLoaderDescriptor": {
        "id": "mule",
        "attributes": {
            "exportedPackages": [
                "org.mule.custom"
            ]
        }
    }
}
You only need to configure the package name in the exportedPackages collection.