Search code examples
mulesoftmule-esbmule4munit

How to write a Munit test case for an integration that is using a common HTTP request connector for calling 5 different resources of the system layer?


I am calling a system layer from process layer as

first get: /abc get: /xyz post: /efg for all these 3 calls using a common http requestor by setting path, method and host before calling this http requestor.

how to mock this http requestor for each call that give me different responses?


Solution

  • You can just add 3 mock when's to your Munit. Each of them will have the processor 'http:request', but with attributes specific to the call. So in each mock you will provide the AttributeName="method" and AttributeName="path" where the values correspond to the specific call you want to mock. See an example of two mocks below.

    <munit-tools:mock-when doc:name="Mock when POST" doc:id="8e3b66fe-b5bb-4f96-97b5-8970c1635b12" processor="http:request">
        <munit-tools:with-attributes >
            <munit-tools:with-attribute whereValue="POST" attributeName="method" />
            <munit-tools:with-attribute whereValue="/post" attributeName="path" />
        </munit-tools:with-attributes>
        <munit-tools:then-return >
            <munit-tools:payload value='#[{"data": "post"}]' mediaType="application/json" />
        </munit-tools:then-return>
    </munit-tools:mock-when>
    <munit-tools:mock-when doc:name="Mock when GET" doc:id="4797dd50-8bc2-428a-9aba-7d03692fc1a2" processor="http:request" >
        <munit-tools:with-attributes >
            <munit-tools:with-attribute whereValue="GET" attributeName="method" />
            <munit-tools:with-attribute whereValue="/get" attributeName="path" />
        </munit-tools:with-attributes>
        <munit-tools:then-return >
            <munit-tools:payload value='#[{"data": "get"}]' mediaType="application/json" />
        </munit-tools:then-return>
    </munit-tools:mock-when>