我有mule中的db连接器,它有select查询来计算记录数,数据库的响应如下:
MULE CODE SNIPPET:
<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]
你能帮我解释一下如何模仿回应。谢谢 !!
您在mock:then-return
中的语法无效。
你错过了几件事:
#[...code goes here...]
{COUNT: 1}
所以你的mock:then-return
应该是这样的:
<mock:then-return payload="#[[{COUNT: 1}]]"/>