Search code examples
muleanypoint-studiomunit

Mock an Exception in MUnit with Mulesoft


I have a flow containing an http request to an external service. I want to add an MUnit test to make sure the logic functions when the external service is down and returns a 500. I mock the http request and return a 500, but the flow does not raise an exception since I am testing outside of APIKit.

Is there any way to force a mocked component to raise a specific exception?


Solution

  • You can mock a message processor and instruct it to throw an exception by using the throw-an message processor:

    https://docs.mulesoft.com/munit/v/1.2.0/mock-message-processor#defining-mock-response-exceptions

    It short it follows the same definition patter as the mock message processor:

    <mock:config name="mock_config" doc:name="Mock configuration"/>
       <mock:throw-an whenCalling="mule:set-payload" exception-ref="#[new java.lang.Exception()]">
    </mock:throw-an>
    

    (in this example only defining the message processor name but you can also use the attributes of the message processor).

    HTH