2

I'm trying to create an MUnit test that mocks an HTTP request by setting the payload to a JSON object that I have saved in a file. In Mule 3 I would have just done getResource('fileName.json').asString() and that worked just fine. In Mule 4 though, I can't statically call getResource.

I found a forum post on the Mulesoft forums that suggested I use MunitTools::getResourceAsString. When I run my test, I do see the JSON object but with all the \n and \r characters as well as a \ escaping all of the quotation marks. Obviously this means my JSON is no longer well formed.

Ideally I would like to find a reference for MunitTools so that I can see a list of functions that I can call and maybe find one that does not add the escape characters, but I haven't had any luck. If anybody knows of a some reference document that I can refer to, please let me know.

Not being able to find a way to return the data without the extra characters, I tried replacing them via dataweave. This is fine when replacing \n and \r, but as there are also more \s in front of each double quote and I can't seem to make these go away.

If I do this...

replace (/\/) with ("")

...I get an error. A co-worker suggested targeting the each \" and replacing them with ", but that's a problem because that gives me """. To get around this, I've tried

replace(/\"/) with "\""

...which does not cause any errors, but for some reason it reads the \ as a literal so it replaces the original string with itself. I've also tried... replace(/\"/) with '"' ...but that also results in an error

I'm open to any other solutions as well.

Thanks

--Drew

Drew Ingram
  • 75
  • 1
  • 9

2 Answers2

4

I had the same concern so I started using the readUrl() method. This is a DataWeave method so you should be able to use it in any MUnit processor. Here is an example of how I used it in the set event processor. It reads the JSON file and then converts it into Java for my own needs but you can just replace java with JSON for your needs.

<munit:set-event doc:name="Set Event" doc:id="e7b1da19-f746-4964-a7ae-c23aedce5e6f" >
        <munit:payload mediaType="application/java" value="#[output application/java --- readUrl('classpath://singleItemRequest.json','application/json')]"/>
</munit:set-event>

Here is the documentation for readUrl https://docs.mulesoft.com/mule-runtime/4.2/dw-core-functions-readurl

Hope that helps!

Yuri
  • 4,254
  • 1
  • 29
  • 46
maloney77
  • 41
  • 2
1

Follow this snippet (more specifically the munit-tools:then-return tag):

<munit-tools:mock-when doc:name="Mock GET /users" doc:id="89c8b7fb-1e94-446f-b9a0-ef7840333328" processor="http:request" >
    <munit-tools:with-attributes >
        <munit-tools:with-attribute attributeName="doc:name" whereValue="GET /users" />
    </munit-tools:with-attributes>
    <munit-tools:then-return>
        <munit-tools:payload value="#[read(MunitTools::getResourceAsString('examples/responses/anypoint-get-users-response.json'), &quot;application/json&quot;)]" />
    </munit-tools:then-return>
</munit-tools:mock-when>

It mocks an HTTP request and returns a JSON object using the read() function.