Search code examples
mulemunit

Build message with inbound properties in MUnit


I am trying to unit test a groovy component.

    <sub-flow name="public_sf_util_munit"
        doc:description="Input:Mule Message
Processing:Adds correlationId to MuleMessage
Output:MuleMessage with populated correlationId property">
        <scripting:component doc:name="Add correlationId">
            <scripting:script engine="Groovy"><![CDATA[String correlationId = message.correlationId;
if(message.correlationId==null){
    correlationId = message.getInboundProperty('x-messageid');
    if(correlationId == null){
        correlationId = message.rootId;     
    }
}
message.correlationId = correlationId;
return message]]></scripting:script>
        </scripting:component>
    </sub-flow>

The below line expects a inbound message property, how can I add this into my test Mule Message ? I am using MUnit 3.6.1.

correlationId = message.getInboundProperty('x-messageid');

The test I have so far is as below, and I need to add in the inbound property

@Test
    public void givenAMuleMessageWithNullCorrelationIdAndXMessageIdHeader_whenCorrelationIdIsSet_itShouldSetCorrelationWithMessageId() throws Exception {
        MuleEvent testEvent = testEvent("something");
        MuleEvent resultMuleEvent = runFlow("transformToOutbound", testEvent);
        assertThat(resultMuleEvent).isNotNull();
        assertThat(resultMuleEvent.getMessage().getCorrelationId()).isEqualTo("321");
    }

Solution

  • You can set it on the message retrieved from the test event:

    import org.mule.api.transport.PropertyScope;
    
    ...
    
    MuleEvent testEvent = testEvent("something");
    testEvent.getMessage().setProperty("x-messageid", "somevalue",  PropertyScope.INBOUND);