有一个流将调用http-outbound endpoint。如果http不可用,我想为场景编写测试用例(捕获流中的异常并按预期从POSTMAN工作)。我尝试使用抛出异常来模拟在消息进程为http:request时抛出的异常但它没有用。有人可以帮助如何在munit中模拟异常吗?
以下是我尝试过的代码:
<munit:test name="test-project-test-suite-munit-testFlowTest3" description="Test" >
<mock:when messageProcessor="mule:set-payload" doc:name="Mock">
<mock:with-attributes>
<mock:with-attribute name="doc:name" whereValue="#['Set Payload']"/>
</mock:with-attributes>
<mock:then-return payload="#['payload3']"/>
</mock:when>
<mock:when messageProcessor="mule:flow" doc:name="Mock">
<mock:with-attributes>
<mock:with-attribute name="name" whereValue="#[matchContains('munit-testFlow2')]"/>
</mock:with-attributes>
<mock:then-return payload="#[]">
<mock:invocation-properties>
<mock:invocation-property key="variable2" value="#['response2']"/>
</mock:invocation-properties>
</mock:then-return>
</mock:when>
<mock:throw-an exception-ref="#[new org.mule.api.MessagingException()]" whenCalling="http:request" doc:name="Throw an Exception">
<mock:with-attributes>
<mock:with-attribute name="doc:name" whereValue="#['HTTP-RES']"/>
</mock:with-attributes>
</mock:throw-an>
<flow-ref name="munit-testFlow" doc:name="munit-testFlow"/>
<munit:assert-payload-equals message="oops failed" expectedValue="#['error-response']" doc:name="Assert Payload"/>
</munit:test>
答案 0 :(得分:1)
而不是像new org.mule.api.MessagingException()
那样使用
new IllegalArgumentException('messaging exception')
或
new java.lang.Exception("messaging exception")
org.mule.api.MessagingException()
是一个受保护的函数,可能无法以可能的方式使用。实际上它位于org.mule.api.MuleException()
之下,又是protected
。
https://www.mulesoft.org/docs/site/3.3.0/apidocs/org/mule/api/MessagingException.html
请参阅以下网址了解详情。
https://forums.mulesoft.com/questions/44929/munit-throw-exception-mock-not-working.html
工作代码
<munit:test name="sample-test-suite-sampleFlowTest" description="Test">
<mock:throw-an exception-ref="#[new java.lang.Exception('messaging exception')]" whenCalling="http:request" doc:name="Throw an Exception">
</mock:throw-an>
<flow-ref name="sampleFlow" doc:name="sampleFlow"/>
<logger level="INFO" doc:name="Logger"/>
</munit:test>