Nav
You are viewing an older version of this section. Click here to navigate to the latest version.

Assertion Message Processors

Overview

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.

Assert Payload

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

message

The error message to print if the assertions fails. It accepts MEL expressions. If left as a literal, it assumes a string value.

expectedValue

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 junit.framework.AssertionFailedError.

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.

Assert Null

The assert-null message processor validates that the Mule message’s payload is NullPayload.

assert-null example
<munit:assert-null message="oops!"/>
Attribute Name Description

message

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 junit.framework.AssertionFailedError.

Assert Not Null

The assert-not-null message processor validates that the Mule message’s payload is not NullPayload.

assert-not-null example
<munit:assert-not-null message="oops!"/>
Attribute Name Description

message

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 junit.framework.AssertionFailedError.

Assert Equals

The assert-on-equals message processor validates equality between two objects.

assert-on-equals example
<munit:assert-on-equals message="oops!" expectedValue="#['expected']" actualValue="#['actual_value']"/>
Attribute Name Description

message

Defines the error message to print if the assertions fails. It accepts MEL expressions. If left as a literal, it assumes a string value.

expectedValue

Defines the value of the expected object. It accepts MEL expressions. If left as a literal, it assumes a string value.

actualValue

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 junit.framework.AssertionFailedError.

Assert Not Equals

The assert-not-same message processor validates whether two objects are different.

assert-not-same example
<munit:assert-not-same message="oops!" expectedValue="#['']" actualValue="#[payload]"/>
Attribute Name Description

message

Defines the error message to print if the assertions fails. It accepts MEL expressions. If left as a literal, it assumes a string value.

expectedValue

Defines the value of the expected object. It accepts MEL expressions. If left as a literal, it assumes a string value.

actualValue

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 junit.framework.AssertionFailedError.

Assert False

The assert-false message processor validates that the result of a boolean expression is false.

assert-false example
<munit:assert-false  message="oops!" condition="#[ 1 != 1 || payload.length != 0]"/>
Attribute Name Description

message

Defines the error message to print if the assertions fails. It accepts MEL expressions. If left as a literal, it assumes a string value.

condition

Defines the expression you want to validate.

If the assertion fails, the message processor throws a junit.framework.AssertionFailedError.

Assert True

The assert-true message processor validates that the result of a boolean expression is true.

assert-true example
<munit:assert-true  message="oops!" condition="#[ 1 == 1 && payload.length == 0]"/>
Attribute Name Description

message

Defines the error message to print if the assertions fails. It accepts MEL expressions. If left as a literal, it assumes a string value.

condition

Defines the expression you want to validate.

If the assertion fails, the message processor throws a junit.framework.AssertionFailedError.

MUnit Utilitarian MEL Functions

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.

Message Properties Finder

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

messageHasproperty is foundInAnyScopeCalled(propertyName)

Returns true if a property in any scope with the name provided is found.

messageHasInboundproperty is foundCalled(propertyName)

Returns true if an inbound property with the provided name is found.

messageHasOutboundproperty is foundCalled(propertyName)

Returns true if an outbound property with the provided name is found.

messageHasSessionproperty is foundCalled(propertyName)

Returns true if a session property with the provided name is found.

messageHasInvocationproperty is foundCalled(propertyName)

Returns true if an invocation property with the provided name is found.

messageHasInboundAttachmentCalled(property is foundName)

Returns true if an inbound attachment property with the provided name is found.

messageHasOutboundAttachmentCalled(property is foundName)

Returns true if an outbound attachment property with the provided name is found.

MUnit Matchers - Property Finder Example

           
        
1
2
<munit:assert-true condition="#[messageHasPropertyInAnyScopeCalled('my_property')]"/>
<munit:assert-true condition="#[messageHasInvocationPropertyCalled('another_property')]"/>

Other MEL functions

Function Description

getBeanFromMuleContext('bean_name')

Inspects the Mule registry and returns the bean with the matching name if present.

Fail

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.

Fail Example
<munit:fail message="This should not happen"/>
Attribute Name Description

message

Defines the error message to print if the assertion fails. It accepts MEL expressions. If left as a literal, it assumes a string value.

Defining Custom Assertions

If need a more specific assertion, MUnit allows you to extend the assertion message processor’s library, and hence define your own custom assertions.

Defining Custom Assertion Implementations

To implement a custom assertion you need to implement the interface org.mule.munit.MunitAssertion.

Custom assertion example

          
       
1
2
3
4
5
6
7
8
9
10
11
12
13
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.

Defining a Custom Assertion Message Processor

After you have defined your custom assertion, use the run-custom message processor to run it.
You need to 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

custom-bean-assertion

assert-custom-assertion

Attribute Name Description

assertion-ref

Defines the custom assertion instance to run.


    
             
          
1
2
3
4
5
6
7
&lt;spring:beans&gt;    <i class="conum" data-value="1"></i><b>(1)</b>
  &lt;spring:bean class="your.package.CustomAssertion" name="customAssertion"/&gt;
&lt;/spring:beans&gt;
...
&lt;munit:test name="testCustomAssertion" description="run custom assertion test"&gt;
    &lt;munit:run-custom assertion-ref="customAssertion"/&gt; <i class="conum" data-value="2"></i><b>(2)</b>
&lt;/munit:test&gt;

<1> Define custom assertion bean. <2> Run custom assertion using bean name.

Attribute Name Description

assertion-ref

Defines the custom assertion instance to run.

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.

Defining Assertions With Java Code

The MUnit assertions are based in JUnit assertions, thus there is no new Java API.

To define assertions in your Java-based MUnit Test, you just need to import the JUnit Assert library. MUnit does not provide a Java assert library.