1
votes

I have a flow I wish to test. The flow has an HTTP inbound connector, and the flow itself acts as a RESTful service.

The flow expects a few URL parameters to be set, and accesses these throughout using the standard notation: #[message.inboundProperties.'http.query.params'.test].

I am wanting to test this flow using MUnit. When I create a default MUnit test against the flow, MUnit creates a flow-ref to call my original flow. Running this test immediately fails however, because the flow expects to be called by HTTP, and expects the URL parameters set.

This is where I'm at now. I wish to set these variables before calling the flow-ref, so that my flow can process normally.

I have tried a few things:

  • using the munit:set component, which allows one to set various properties
  • manually instantiating a MessagePropertyMapContext and assigning it to the http.query.params variable
  • replacing the flow-ref component with an HTTP component and calling the flow with an actual HTTP message

These all do not seem to work properly.

The variable setting seems to always fail, and results in NullPointerException when trying to access the variables.

Calling the flow using an HTTP component doesn't work because Mule randomly assigns a port for testing, and I can't seem to get access to this port at runtime. Thus I have no way to call the endpoint.

I have looked elsewhere for similar questions here and here, but they do not seem to help me.

So my basic question is: how can I set URL parameters that can be accessed by a flow, when being tested from an MUnit test. Is there instead a better way to achieve what I'm trying to achieve here?

Thank you!

3

3 Answers

3
votes

@TroyS you can try the following code. It worked for me.

<munit:set payload="#['']" doc:name="Set Message url_key:payload_1">
        <munit:inbound-properties>
            <munit:inbound-property key="http.query.params" value="#[['url_key':'payload_1']]"/>
        </munit:inbound-properties>
    </munit:set>
0
votes

Well it turns out the Mule documentation has the answer if you look hard enough ;) The answer is right here.

You just need to add the munit:set component at the start of your MUnit test flow (search for "Set Message" in your Mule Palette for this component):

<munit:set payload="#['']" doc:name="Set HTTP query params">
    <munit:inbound-properties>
        <munit:inbound-property key="http.query.params" value="#[['transactionId': 'x873h3dj']]"/>
    </munit:inbound-properties>
</munit:set>

This creates a parameter pair transactionId: x873h3dj, which can then be accessed in your code as needed. You can add more parameters by extending the following structure according to normal MEL syntax:

#[['key1':'val1', 'key2': 'val2', 'key3': 'val3']]
0
votes

To add to the answer above, this corresponds to "Set Message" component in Mule 3 pallette.

enter image description here