0

我在 mule 中有 db 连接器,它具有用于计算记录数的选择查询,来自数据库的响应如下:

骡子代码片段:

     <db:select config-ref="Oracle_Configuration" doc:name="FetchDetails">
            <db:parameterized-query><![CDATA[SELECT COUNT(1) AS COUNT from MAPPER_T where ST_NO=#[flowVars.fetchNO] and DATE=SYSDATE]]></db:parameterized-query>
        </db:select>

 <choice doc:name="EvaluateCount">
            <when expression="#[payload[0]['COUNT'] == 0]">
           <logger message="#[payload]" level="INFO" category="DO SOMETHING X" doc:name="Logger"/>
            </when>
 <otherwise>
                    <logger message="#[payload]" level="INFO" category="DO SOMETHING Y" doc:name="Logger"/>
            </otherwise>
        </choice>

使用带有 mel #[message.payload] 的记录器的数据库调用响应

[{COUNT=1}]

在 munit 实现中,我试图模拟这个响应,我使用了 MULE 的 MOCK 组件,下面是我的代码:

 <munit:test name="api-Happy-Scenario-test-suiteTest" description="MUnit Test">
  	 <mock:when messageProcessor=".*:.*" doc:name="Mock - Evaluate Results">
            <mock:with-attributes>
                <mock:with-attribute name="doc:name" whereValue="#['FetchDetails']"/>
            </mock:with-attributes>
            <mock:then-return payload="#[{COUNT: 0}]"/>
        </mock:when>
               <munit:set payload="#[getResource('json/ResultsRequest.json').asString()]" mimeType="application/json" doc:name="Set Message">
	</munit:set>
        <flow-ref name="post:/results:api-config" doc:name="InvokeResultsService"/>
        <munit:assert-true message="The HTTP Status code is not correct!" condition="#[messageInboundProperty('http.status').is(eq(201))]" doc:name="assert that - http.status eq 201"/>
        <munit:assert-null doc:name="Assert Null Payload"/>
    </munit:test>

但是在执行测试用例时,我得到了以下异常:

Caused by: org.mule.api.expression.ExpressionRuntimeException: Execution of the expression "payload[0]['COUNT']" failed.
Caused by: [Error: java.lang.Character cannot be cast to java.lang.Class]
[Near : {... payload[0]['COUNT'] ....}]
             ^
[Line: 1, Column: 1]

你能帮我如何模拟回应吗?谢谢 !!

4

2 回答 2

1

我知道你问这个问题已经有一段时间了,但无论如何我都会为未来的观众回答这个问题。这对我有用。

通常,来自 db 连接器的响应是一个哈希映射。您将不得不参考 groovy 文件来模拟数据库响应。

模拟示例:

<mock:when messageProcessor="db:select" doc:name="Mock - Evaluate Results">
    <mock:with-attributes>
        <mock:with-attribute name="doc:name" whereValue="FetchDetails"/>
    </mock:with-attributes>
    <mock:then-return payload="#[resultOfScript('FetchDetailsGroovyScript')]"/>
</mock:when>

还要在 munit config.xml 中包含以下行以引用 groovy 脚本。

<scripting:script name="FetchDetails" engine="groovy" file="mock-data/Fetch-Details-Groovy-Script.groovy" doc:name="Script"/>

Groovy 脚本示例:Fetch-Details-Groovy-Script.groovy

    def map1 = new org.mule.util.CaseInsensitiveHashMap()
map1.put('COUNT',0)


def listOfMap = new LinkedList<org.mule.util.CaseInsensitiveHashMap>()
listOfMap.add(map1)

return listOfMap
于 2020-02-12T20:55:39.197 回答
0

您在 中的语法mock:then-return无效。

你错过了几件事:

  1. 缺少开始 MEL 表达式的井号。你应该有这样的东西:#[...code goes here...]
  2. 未正确创建地图。你想要这样的东西:{COUNT: 1}

所以你mock:then-return应该看起来像这样:

<mock:then-return payload="#[[{COUNT: 1}]]"/>

于 2018-09-04T15:09:18.397 回答