2017-05-26 55 views
0

有流量正在呼叫http出站端点。如果http不可用,我想为该方案编写测试用例(捕获流中的异常并按照POSTMAN的预期工作)。我尝试使用抛出一个异常嘲笑当消息进程是http:request ..但它没有奏效时抛出的异常。有人可以帮助如何在munit中模拟异常吗?如何在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

能否请您发表您的Munit您试图抛出异常? – star

+0

@star添加了代码 –

回答

0

而不是使用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> 
+0

感谢@star它为我工作。 –

相关问题