Studio Visual Editor
XML or Standalone Editor
<munit:assert-payload-equals message="oops!" expectedValue="'#[['value 1','value 2']]"/>
The Assert feature, provided by MUnit, allows the developer to run assertions in order to validate the state of a Mule message’s content.
All the message processors listed bellow can be used either to validate the Mule message after the production code runs, or as part of the MUnit Spy message processor.
Every MUnit Assertion message processor contains an attribute message. It is recommended to fill this optional attribute in order to easily identify a failing assertion. |
The assert-payload-equals
message processor validates that the payload of a Mule message is equal to an expected value.
<munit:assert-payload-equals message="oops!" expectedValue="'#[['value 1','value 2']]"/>
Attribute Name | Description |
---|---|
|
The error message to print if the assertions fails. It accepts MEL expressions. If left as a literal, it assumes a string value. |
|
The value to compare the payload against. It accepts MEL expressions. If left as a literal, it assumes a string value. |
If the assertion fails, the message processor throws a java.lang.AssertionError
.
If you wish to compare the payload against a Spring bean, you can use the MUnit MEL function getBeanFromMuleContext('bean_name') . This function inspects the Mule registry and returns the bean with the matching name if it is present.
|
The assert-null
message processor validates that the Mule message’s payload is NullPayload
.
<munit:assert-null message="oops!"/>
Attribute Name | Description |
---|---|
|
Defines the error message to print if the assertions fails. It accepts MEL expressions. If left as a literal, it assumes a string value. |
If the assertion fails, the message processor throws a java.lang.AssertionError
.
The assert-not-null
message processor validates that the Mule message’s payload is not NullPayload
.
<munit:assert-not-null message="oops!"/>
Attribute Name | Description |
---|---|
|
Defines the error message to print if the assertions fails. It accepts MEL expressions. If left as literal, it assumes a string value. |
If the assertion fails, the message processor throws a java.lang.AssertionError
.
The assert-on-equals
message processor validates equality between two objects.
<munit:assert-on-equals message="oops!" expectedValue="#['expected']" actualValue="#['actual_value']"/>
Attribute Name | Description |
---|---|
|
Defines the error message to print if the assertions fails. It accepts MEL expressions. If left as a literal, it assumes a string value. |
|
Defines the value of the expected object. It accepts MEL expressions. If left as a literal, it assumes a string value. |
|
Defines the actual value we are validating. It accepts MEL expressions. If left as a literal, it assumes a string value. |
If the assertion fails, the message processor throws a java.lang.AssertionError
.
The assert-not-same
message processor validates whether two objects are different.
<munit:assert-not-same message="oops!" expectedValue="#['']" actualValue="#[payload]"/>
Attribute Name | Description |
---|---|
|
Defines the error message to print if the assertions fails. It accepts MEL expressions. If left as a literal, it assumes a string value. |
|
Defines the value of the expected object. It accepts MEL expressions. If left as a literal, it assumes a string value. |
|
Defines the actual value we are validating. It accepts MEL expressions. If left as a literal, it assumes a string value. |
If the assertion fails, the message processor throws a java.lang.AssertionError
.
The assert-false
message processor validates that the result of a boolean expression is false.
<munit:assert-false message="oops!" condition="#[ 1 != 1 || payload.length != 0]"/>
Attribute Name | Description |
---|---|
|
Defines the error message to print if the assertions fails. It accepts MEL expressions. If left as a literal, it assumes a string value. |
|
Defines the expression you want to validate. |
If the assertion fails, the message processor throws a java.lang.AssertionError
.
The assert-true
message processor validates that the result of a boolean expression is true.
<munit:assert-true message="oops!" condition="#[#[ 1 == 1 && payload.length == 0]]" doc:name="Assert True"/>
Attribute Name | Description |
---|---|
|
Defines the error message to print if the assertions fails. It accepts MEL expressions. If left as a literal, it assumes a string value. |
|
Defines the expression you want to validate. |
If the assertion fails, the message processor throws a java.lang.AssertionError
.
You can greatly enhance the capabilities of the Assert True message processor by combining it with the MUnit utilitarian MEL Functions, a set of MEL expressions that help validate the status of a Mule message.
These functions validate the existence of a certain message property by its name. They are specially useful in cases where the value of a message property is irrelevant, but you need to validate that the property was created by the flow you’re testing.
Function | Description |
---|---|
|
Returns true if a property in any scope with the name provided is found. |
|
Returns true if an inbound property with the provided name is found. |
|
Returns true if an outbound property with the provided name is found. |
|
Returns true if a session property with the provided name is found. |
|
Returns true if an invocation property with the provided name is found. |
|
Returns true if an inbound attachment property with the provided name is found. |
|
Returns true if an outbound attachment property with the provided name is found. |
<munit:assert-true condition="#[messageHasPropertyInAnyScopeCalled('my_property')]"/>
<munit:assert-true condition="#[messageHasInvocationPropertyCalled('another_property')]"/>
Use the fail
message processor if you want to fail your test on purpose, for example in order to validate that a specific event should not happen.
<munit:fail message="This should not happen"/>
Attribute Name | Description |
---|---|
|
Defines the error message to print if the assertion fails. It accepts MEL expressions. If left as a literal, it assumes a string value. |
If need a more specific assertion, MUnit allows you to extend the assertion message processor’s library, and hence define your own custom assertions.
To implement a custom assertion you need to implement the interface org.mule.munit.MunitAssertion
.
package your.package;
public class CustomAssertion implements MunitAssertion{
@Override
public MuleEvent execute(MuleEvent muleEvent) throws AssertionError { (1)
if ( !muleEvent.getMessage().getPayload().equals("Hello World") ){ (2)
throw new AssertionError("Error the payload is incorrect");
}
return muleEvent; (3)
}
}
1 | Implement the only method in the interface public MuleEvent execute(MuleEvent muleEvent) throws AssertionError . |
2 | Run your custom logic, which in this case validates that the message’s payload is Hello World . |
3 | If the validation is passed, return the same event. |
Implement your custom assertions with care, since modifying the message payload or variables could affect subsequent assertions in your test. Normal MUnit assertions guarantee that this does not happens unless specified. |
After you have defined your custom assertion, use the run-custom
message processor to run it.
<munit:run-custom assertion-ref="#[new your.package.CustomAssertion()]"/>
Attribute Name | Description |
---|---|
|
Defines the custom assertion instance to run. |
You can also define your custom assertion as a bean.
Navigate to the Global Elements tab from your test Suite, click Create, select Bean and configure your custom bean assertion
<spring:beans> (1)
<spring:bean class="your.package.CustomAssertion" name="customAssertion"/>
</spring:beans>
...
<munit:test name="testCustomAssertion" description="run custom assertion test">
<munit:run-custom assertion-ref="customAssertion"/> (2)
</munit:test>
1 | Define custom assertion bean. |
2 | Run custom assertion using bean name. |
The run-custom message processor does not allow to define an error message in case of failure. This is handled by the custom assertion implementation.
|