0

Using Mule 4 and have a GET endpoint : getEmployee The API I am trying to MUNIT is an experience api and its expected response is :

{
   
    "correlationId": correlationId  ( Mule correlation id ),
    "empDetails" : {
       "ename": "John",
       "eid": "100" 
    }
}

In the above response, data received from process api is set against empDetails.The experience api is adding correlationId in response.

The problem I am encountering is when trying to Munit this endpoint . I am mocking the process api response
So the mocked response is a static json file :

{
       "ename": "John",
       "eid": "100"
    }

Once this mocked response ( from process api ) is received back in experience api , we append correlation id before sending response back.

So in My Munit I am trying to compare results from runtime with a static response from a json file. My static response in json file is as below :

    {

    "correlationId": "abcd",
    "empDetails" : {
       "ename": "John",
       "eid": "100" 
    }
}

Notice that above in static json file the correlation id is expected to be abcd

So in my munit I am trying to set correlation id with abcd :

   <munit:set-event doc:name="Set Event">
    <munit:attributes value="#[{ method : 'GET',  requestPath: '/employee', headers : { 'X- 
    CORRELATION-ID' : 'abcd'}}]" />
    ...
    ...

However inspite of setting the correlationid header value to a fixed static value abcd , mule runtime is dynamically generating the value and as a result rather than having abcd in response we are getting a dynamic value causing munit to fail ...

Note - when I run the api and invoke this endpoint via postman , then whatever value i send in header X-Correlation-Id is being set against correlationid ( expected behaviour )

However in case of munit this is not happening .

I think the difference between the two is - in actual run - the HTTP listener is invoked Vs in case of Munit the specific flow is being tested ( after http listener and api kit router ) , so I am suspecting this is why mule is already setting correlation id in case of munit.

So really I need some way to control the value of correlationid in Munit , how do we achieve the same ?

  • Please share versions of Mule and MUnit, and the actual test case including the comparison. – aled Nov 27 '22 at 14:35

1 Answers1

0

Here are two ways to control the correlationId in your situation:

  1. Use the Tracing module in the MUnit execution scope
    <munit:execution>
        <tracing:with-correlation-id correlationId='#["abcd"]'>
            <!-- invoke flow here -->
        </tracing-with-correlation-id>
    </munit:execution>
  1. Initiate the flow from your MUnit execution scope using the HTTP Requester and set the correlationId attribute to your intended value. Here you have to include both your APIKit main flow and your specific route flow in your enableFlowSources list for the MUnit test.
Ryan Hoegg
  • 2,415
  • 2
  • 14
  • 15
  • 1
    The Tracing Module depends on a feature released with Mule 4.4 and will not work with previous versions. – aled Nov 28 '22 at 00:51