A little description of the flow: I have a flow that is making an HTTP call to a REST Webservice. There are a couple of errors that we get as statusCode=500
but with different "errorCodes"
inside the response body. This request connector is wrapped inside a try block with multiple on-error-continue
based on the content of the response body in its "when" attribute. eg: error.errorMessage.payload.errorCode==CODE_A
. Adding an image and source code of the flow below
<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns="http://www.mulesoft.org/schema/mule/core"
xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd">
<http:listener-config name="HTTP_Listener_config" doc:name="HTTP Listener config" doc:id="dee432d7-a176-448c-a1ac-a374a86fa85b" >
<http:listener-connection host="0.0.0.0" port="8081" />
</http:listener-config>
<flow name="test2Flow" doc:id="be602af1-cfcf-46f1-a312-ea6a5544503b" >
<http:listener doc:name="Listener" doc:id="f160fa61-15be-4744-b823-973e20740e73" config-ref="HTTP_Listener_config" path="/test"/>
<logger level="INFO" doc:name="Some logic before request" doc:id="6354a117-e918-4937-a1d9-df4ae0ef6386" message="Some logic before request"/>
<try doc:name="Try" doc:id="15bd106a-c116-4d87-958e-8ad698026739" >
<http:request method="GET" doc:name="Request" doc:id="a046919e-f149-45d6-9a70-db37cac0540f" url="http://example.com/some-resource" />
<error-handler >
<on-error-continue enableNotifications="true" logException="true" doc:name="when error.errorMessage.payload.errorCode==CODE_A" doc:id="20fc0575-da65-41b4-81f7-d364227b1639" when='error.errorMessage.payload.errorCode=="CODE_A"'>
<logger level="INFO" doc:name="Some logic for CODE_A" doc:id="a699ccd0-952c-415f-b288-4ea027f2cff2" message="Some logic for CODE_A" />
</on-error-continue>
<on-error-continue enableNotifications="false" logException="false" doc:name="when error.errorMessage.payload.errorCode==CODE_B" doc:id="63dc8aa5-5fae-4235-a4ae-a80903a76362" when='error.errorMessage.payload.errorCode=="CODE_B"' >
<logger level="INFO" doc:name="Some logic for CODE_B" doc:id="9364ff1e-4bcd-48d5-a37a-a207828cda9d" message="Some logic for CODE_B" />
</on-error-continue>
</error-handler>
</try>
<logger level="INFO" doc:name="Some logic After request" doc:id="fd754198-d6b5-40dd-b23f-c8619faee7e6" message="Some logic After request" />
</flow>
</mule>
What I am trying to do: I am trying to write MUnit to verify if the correct error-handler block is executed based on response. However, I do not see any option to mock an error with its errorMessage
part filled.
What I am looking for: I am trying to find out one of these
- Anyway to mock error with
errorMessage
using the simplemock-when
'sthen-return
element. I do not think it is possible, but if there is a way please let me know. - Maybe there is a way to use a Java class for raising complex errors. I am thinking if I can use
mock-when
'sthen-call
to call a flow and then raise an error using it. - Any other way to do it.
Note:
- Mule version: 4.3.0 MUnit version: 2.3.0
- I can not make change in the REST webservice, I do not own that.
- I am trying to avoid any code change, For example, I can ignore status code in the message validator and do the error handling using response directly, with something like choice. But I will do it as the last resort
You can try using something like this when mocking error for HTTP requests:
Create the following DW file:
httpError.dwl
In your MUnit test suite file, create the following flow:
And, finally, in your actual MUnit test flow, configure the
then-call
option to call the above flow. Eg:Although I'm not sure if this is something recommended to do, it should work for what you want to achieve.
Let me know if it works.