【问题标题】:How to write a test case for negative scenario(exception scenario) in munit..?如何在 munit.. 中为负面场景(异常场景)编写测试用例?
【发布时间】:2017-05-26 09:18:10
【问题描述】:

有流将调用 http-outbound 端点。如果 http 不可用,我想为该场景编写测试用例(捕获流程中的异常并按 POSTMAN 的预期工作)。我尝试使用 throw an exception 来模拟消息处理为 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>

【问题讨论】:

    标签: mule anypoint-studio munit


    【解决方案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>
    

    【讨论】:

      猜你喜欢
      • 2012-12-17
      • 1970-01-01
      • 2016-08-26
      • 2021-09-05
      • 1970-01-01
      • 1970-01-01
      • 2021-10-23
      • 1970-01-01
      • 2021-08-10
      相关资源
      最近更新 更多