0

我正在使用 Munit 来测试我的 Mule Flow,并且我正在使用模拟来设置变量,以便流程不会引发异常。当我自己运行测试时,它通过了,但是当我运行我的完整套件时,测试失败并且运行调试显示模拟在作为套件运行时没有设置变量。

<?xml version="1.0" encoding="UTF-8"?>

<mule xmlns:mock="http://www.mulesoft.org/schema/mule/mock" xmlns="http://www.mulesoft.org/schema/mule/core"
xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
xmlns:munit="http://www.mulesoft.org/schema/mule/munit" xmlns:spring="http://www.springframework.org/schema/beans"
xmlns:core="http://www.mulesoft.org/schema/mule/core" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.mulesoft.org/schema/mule/munit http://www.mulesoft.org/schema/mule/munit/current/mule-munit.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd
http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/mock     http://www.mulesoft.org/schema/mule/mock/current/mule-mock.xsd">
<munit:config name="munit" doc:name="MUnit configuration" />
<spring:beans>
    <spring:import resource="classpath:endpointproject.xml" />
</spring:beans>

<munit:test
    name="test_FilePickUpFlow_CallsFileToStringTransformer_ExactlyOnce"
    description="Test">
    <flow-ref name="filePickUpFlow" doc:name="filePickUpFlow" />
    <mock:verify-call messageProcessor="file:file-to-string-transformer"
        times="1" doc:name="Verify Call" />
</munit:test>

<munit:test
    name="test_FilePickUpFlow_CallsJsonToObjectTransformer_ExactlyOnce"
    description="MUnit Test">
    <flow-ref name="filePickUpFlow" doc:name="filePickUpFlow" />
    <mock:verify-call messageProcessor="json:json-to-object-transformer"
        times="1" doc:name="Verify Call" />
</munit:test>

<munit:test name="test_FilePickUpFlow_CallsJsonParseSubFlow_ExactlyOnce"
    description="Validate calls to sub flows are being done properly ">
    <set-payload
        value="[{&quot;protocol&quot;:&quot;http&quot;,&quot;host&quot;:&quot;0.0.0.0&quot;,&quot;port&quot;:&quot;8085&quot;,&quot;path&quot;:&quot;&quot;,&quot;operation&quot;:&quot;GET&quot;}]"
        doc:name="Set Payload" />
    <flow-ref name="filePickUpFlow" doc:name="filePickUpFlow" />
    <mock:verify-call messageProcessor="mule:sub-flow"
        doc:name="Verify Call" times="1">
        <mock:with-attributes>
            <mock:with-attribute name="name"
                whereValue="#[matchContains('jsonParseSubFlow')]" />
        </mock:with-attributes>
    </mock:verify-call>
</munit:test>

<munit:test name="test_httpInboundFlow_CallsSetPayload_ExactlyOnce"
    description="MUnit Test">
    <flow-ref name="httpInboundFlow" doc:name="httpInboundFlow" />
    <mock:verify-call messageProcessor="set-payload"
        doc:name="Verify Call" />
</munit:test>

<munit:test
    name="test_HttpInboundFlow_CallsJsonToObjectTransformer_ExactlyOnce"
    description="MUnit Test">
    <munit:set payload="#[]" doc:name="Set Message">
        <munit:inbound-properties>
            <munit:inbound-property key="http.query.params"
                value="ParameterMap{[json=[[{&quot;protocol&quot;:&quot;http&quot;,&quot;host&quot;:&quot;0.0.0.0&quot;,&quot;port&quot;:&quot;8085&quot;,&quot;path&quot;:&quot;&quot;,&quot;operation&quot;:&quot;GET&quot;}]]]}" />
        </munit:inbound-properties>
    </munit:set>
    <flow-ref name="httpInboundFlow" doc:name="httpInboundFlow" />
    <mock:verify-call messageProcessor="json:json-to-object-transformer"
        doc:name="Verify Call" times="1" />
</munit:test>
<munit:test name="test_HttpInboundFlow_CallsJsonParseSubFlow_ExactlyOnce"
    description="Validate calls to sub flows are being done properly ">
    <mock:when messageProcessor="mule:set-payload" doc:name="Mock">
        <mock:with-attributes>
            <mock:with-attribute name="doc:name"
                whereValue="#['Set Payload To Query Params']" />
        </mock:with-attributes>
        <mock:then-return
            payload="#['[{&quot;protocol&quot;:&quot;http&quot;,&quot;host&quot;:&quot;0.0.0.0&quot;,&quot;port&quot;:&quot;8085&quot;,&quot;path&quot;:&quot;&quot;,&quot;operation&quot;:&quot;GET&quot;}]']" />
    </mock:when>
    <mock:when messageProcessor="json:json-to-object-transformer"
        doc:name="Mock">
        <mock:with-attributes>
            <mock:with-attribute name="doc:name" whereValue="#['JSON to Object']" />
        </mock:with-attributes>
        <mock:then-return
            payload="#['[{protocol=http, host=0.0.0.0, port=8085, path=, operation=GET}]']" />
    </mock:when>
    <flow-ref name="httpInboundFlow" doc:name="httpInboundFlow" />
    <mock:verify-call messageProcessor="mule:sub-flow"
        times="1" doc:name="Verify Call">
        <mock:with-attributes>
            <mock:with-attribute name="name"
                whereValue="#[matchContains('jsonParseSubFlow')]" />
        </mock:with-attributes>
    </mock:verify-call>
</munit:test>

<munit:test
    name="test_HttpInboundFlow_CallsSetEndpointVariablesSubFlow"
    description="MUnit Test">
    <mock:when messageProcessor="mule:set-payload" doc:name="Mock">
        <mock:with-attributes>
            <mock:with-attribute name="doc:name"
                whereValue="#['Set Payload To Query Params']" />
        </mock:with-attributes>
        <mock:then-return
            payload="#['[{&quot;protocol&quot;:&quot;http&quot;,&quot;host&quot;:&quot;0.0.0.0&quot;,&quot;port&quot;:&quot;8085&quot;,&quot;path&quot;:&quot;&quot;,&quot;operation&quot;:&quot;GET&quot;}]']" />
    </mock:when>
    <mock:when messageProcessor="mule:set-payload" doc:name="Mock">
        <mock:with-attributes>
            <mock:with-attribute name="doc:name" whereValue="set port variable" />
        </mock:with-attributes>
        <mock:then-return payload="#[]">
            <mock:inbound-properties>
                <mock:inbound-property key="port" value="8085" />
            </mock:inbound-properties>
        </mock:then-return>
    </mock:when>
    <mock:when messageProcessor="mule:set-payload" doc:name="Mock">
        <mock:with-attributes>
            <mock:with-attribute name="doc:name" whereValue="set path variable" />
        </mock:with-attributes>
        <mock:then-return payload="#[]">
            <mock:inbound-properties>
                <mock:inbound-property key="path" value="#[]" />
            </mock:inbound-properties>
        </mock:then-return>
    </mock:when>
    <mock:when messageProcessor="mule:set-payload" doc:name="Mock">
        <mock:with-attributes>
            <mock:with-attribute name="doc:name" whereValue="set host variable" />
        </mock:with-attributes>
        <mock:then-return payload="#[]">
            <mock:inbound-properties>
                <mock:inbound-property key="host" value="0.0.0.0" />
            </mock:inbound-properties>
        </mock:then-return>
    </mock:when>
    <mock:when messageProcessor="mule:set-payload" doc:name="Mock">
        <mock:with-attributes>
            <mock:with-attribute name="doc:name"
                whereValue="set protocol variable" />
        </mock:with-attributes>
        <mock:then-return payload="#[]">
            <mock:inbound-properties>
                <mock:inbound-property key="protocol" value="http" />
            </mock:inbound-properties>
        </mock:then-return>
    </mock:when>
    <mock:when messageProcessor="mule:set-payload" doc:name="Mock">
        <mock:with-attributes>
            <mock:with-attribute name="doc:name"
                whereValue="set operation variable" />
        </mock:with-attributes>
        <mock:then-return payload="#[]">
            <mock:inbound-properties>
                <mock:inbound-property key="operation" value="GET" />
            </mock:inbound-properties>
        </mock:then-return>
    </mock:when>
    <munit:set payload="#[]" doc:name="Set Message">
        <munit:inbound-properties>
            <munit:inbound-property key="http.query.params"
                value="ParameterMap{[json=[[{&quot;protocol&quot;:&quot;http&quot;,&quot;host&quot;:&quot;0.0.0.0&quot;,&quot;port&quot;:&quot;8085&quot;,&quot;path&quot;:&quot;&quot;,&quot;operation&quot;:&quot;GET&quot;}]]]}" />
        </munit:inbound-properties>
    </munit:set>
    <flow-ref name="httpInboundFlow" doc:name="httpInboundFlow" />
    <mock:verify-call messageProcessor="mule:sub-flow"
        doc:name="Verify Call">
        <mock:with-attributes>
            <mock:with-attribute name="name" whereValue="#[matchContains('setEndpointVariablesSubFlow')]"/>
        </mock:with-attributes>
    </mock:verify-call>
</munit:test>

<munit:test name="test_HttpInboundFlow_CallsHttpEndpointSubFlow"
    description="MUnit Test">
    <mock:when messageProcessor="mule:set-payload" doc:name="Mock">
        <mock:with-attributes>
            <mock:with-attribute name="doc:name" whereValue="#['Set Payload To Query Params']"/>
        </mock:with-attributes>
        <mock:then-return payload="#['[{&quot;protocol&quot;:&quot;http&quot;,&quot;host&quot;:&quot;0.0.0.0&quot;,&quot;port&quot;:&quot;8085&quot;,&quot;path&quot;:&quot;&quot;,&quot;operation&quot;:&quot;GET&quot;}]']"/>
    </mock:when>
    <mock:when messageProcessor="mule:set-payload" doc:name="Mock">
        <mock:with-attributes>
            <mock:with-attribute name="doc:name" whereValue="set port variable"/>
        </mock:with-attributes>
        <mock:then-return payload="#[]">
            <mock:inbound-properties>
                <mock:inbound-property key="port" value="8085"/>
            </mock:inbound-properties>
        </mock:then-return>
    </mock:when>
    <mock:when messageProcessor="mule:set-payload" doc:name="Mock">
        <mock:with-attributes>
            <mock:with-attribute name="doc:name" whereValue="set path variable"/>
        </mock:with-attributes>
        <mock:then-return payload="#[]">
            <mock:inbound-properties>
                <mock:inbound-property key="path" value="#[]"/>
            </mock:inbound-properties>
        </mock:then-return>
    </mock:when>
    <mock:when messageProcessor="mule:set-payload" doc:name="Mock">
        <mock:with-attributes>
            <mock:with-attribute name="doc:name" whereValue="set host variable"/>
        </mock:with-attributes>
        <mock:then-return payload="#[]">
            <mock:inbound-properties>
                <mock:inbound-property key="host" value="0.0.0.0"/>
            </mock:inbound-properties>
        </mock:then-return>
    </mock:when>
    <mock:when messageProcessor="mule:set-payload" doc:name="Mock">
        <mock:with-attributes>
            <mock:with-attribute name="doc:name" whereValue="set protocol variable"/>
        </mock:with-attributes>
        <mock:then-return payload="#[]">
            <mock:inbound-properties>
                <mock:inbound-property key="protocol" value="http"/>
            </mock:inbound-properties>
        </mock:then-return>
    </mock:when>
    <mock:when messageProcessor="mule:set-payload" doc:name="Mock">
        <mock:with-attributes>
            <mock:with-attribute name="doc:name" whereValue="set operation variable"/>
        </mock:with-attributes>
        <mock:then-return payload="#[]">
            <mock:inbound-properties>
                <mock:inbound-property key="operation" value="GET"/>
            </mock:inbound-properties>
        </mock:then-return>
    </mock:when>
    <munit:set payload="#[]" doc:name="Set Message">
        <munit:inbound-properties>
            <munit:inbound-property key="http.query.params"
                value="ParameterMap{[json=[[{&quot;protocol&quot;:&quot;http&quot;,&quot;host&quot;:&quot;0.0.0.0&quot;,&quot;port&quot;:&quot;8085&quot;,&quot;path&quot;:&quot;&quot;,&quot;operation&quot;:&quot;GET&quot;}]]]}" />
        </munit:inbound-properties>
    </munit:set>
    <flow-ref name="httpInboundFlow" doc:name="httpInboundFlow" />
    <mock:verify-call messageProcessor="mule:sub-flow"
        doc:name="Verify Call">
        <mock:with-attributes>
            <mock:with-attribute name="name" whereValue="#[matchContains('httpEndpointSubFlow')]"/>
        </mock:with-attributes>
    </mock:verify-call>
</munit:test>

我不明白为什么当一个套件时测试会有所不同,因为我每个之间没有通信,你知道我怎样才能避免这种情况发生吗?

失败的测试是 test_HttpInboundFlow_CallsHttpEndpointSubFlow。

4

1 回答 1

0

我设法通过将“test_HttpInboundFlow_CallsJson{arseSubFlow_ExactlyOnce”测试更改为

<munit:test name="test_HttpInboundFlow_CallsJsonParseSubFlow_ExactlyOnce"
    description="Validate calls to sub flows are being done properly ">
    <mock:when messageProcessor="mule:set-payload" doc:name="Mock">
        <mock:with-attributes>
            <mock:with-attribute name="doc:name" whereValue="#['Set Payload To Query Params']"/>
        </mock:with-attributes>
        <mock:then-return payload="#['[{&quot;protocol&quot;:&quot;http&quot;,&quot;host&quot;:&quot;0.0.0.0&quot;,&quot;port&quot;:&quot;8085&quot;,&quot;path&quot;:&quot;&quot;,&quot;operation&quot;:&quot;GET&quot;}]']"/>
    </mock:when>
    <munit:set payload="#[]" doc:name="Set Message">
        <munit:inbound-properties>
            <munit:inbound-property key="http.query.params" value="ParameterMap{[json=[[{&quot;protocol&quot;:&quot;http&quot;,&quot;host&quot;:&quot;0.0.0.0&quot;,&quot;port&quot;:&quot;8085&quot;,&quot;path&quot;:&quot;&quot;,&quot;operation&quot;:&quot;GET&quot;}]]]}"/>
        </munit:inbound-properties>
    </munit:set>
    <flow-ref name="httpInboundFlow" doc:name="httpInboundFlow" />
    <mock:verify-call messageProcessor="mule:sub-flow"
        times="1" doc:name="Verify Call">
        <mock:with-attributes>
            <mock:with-attribute name="name"
                whereValue="#[matchContains('jsonParseSubFlow')]" />
        </mock:with-attributes>
    </mock:verify-call>
</munit:test>

我不明白为什么这个测试会影响另一个测试,有人有什么建议吗?

于 2016-06-17T11:01:45.453 回答