我有一个返回以下json负载的服务:
{
"location-details": {
"fromPostalCode": "1508XB",
"fromGeoCoordinates": "116532,496398",
"toPostalCode": "1511MA",
"toGeoCoordinates": "120427,493380"
},
"commute-distance": "5817.799",
"commute-time": "370.0152"
}
我使用相同的工具创建了一个MUNIT测试套件,其中我使用assert该实用工具来声明有效负载。代码段如下:
<munit:validation >
<munit-tools:assert-that doc:name="Assert That Status Code is 200" doc:id="1c2e536b-513e-4b76-958b-2ea864a64805" expression="#[attributes.statusCode]" is="#[MunitTools::equalTo(200)]" message="The HTTP Status code is not correct!" />
<munit-tools:assert-that doc:name="Assert That - Payload is Expected" doc:id="f4b811fa-ff11-4746-93cc-f87576504808" expression="#[payload]" is="#[MunitTools::getResourceAsString('SuccessResponse.json')]" message="The response payload is not correct!"/>
</munit:validation>
但这会引发以下错误,并且无法正常工作:
...38 more
Caused by: org.mule.runtime.api.el.ExpressionExecutionException: Unable to convert '{
"location-details": {
"fromPostalCode": "1508XB",
"fromGeoCoordinates": "116532,496398",
"toPostalCode": "1511MA",
"toGeoCoordinates": "120427,493380"
},
"commute-distance": "5817.799",
"commute-time": "370.0152"
}' with class 'java.lang.String' to class 'Matcher', while writing Java at org.mule.munit.tools.util.GetResourceFunctions.getResourceAsString.
at org.mule.weave.v2.el.WeaveExpressionLanguage.doEvaluate(WeaveExpressionLanguage.scala:139)
at org.mule.weave.v2.el.WeaveExpressionLanguage.evaluate(WeaveExpressionLanguage.scala:236)
答案 0 :(得分:3)
您在表达式中缺少匹配器组件,请按如下所示进行更改,然后重试:
<munit-tools:assert-that doc:name="Assert Payload is Correct" doc:id="5b45470f-05e8-406e-9d44-877ff1506220" expression='#[output application/json --- write(payload, "application/json")]' is="#[output application/json --- MunitTools::equalTo(MunitTools::getResourceAsString('json\SuccessResponse.json'))]" message="The message is not correct"/>
这可以肯定地工作!!
答案 1 :(得分:0)
我必须使用readUrl
而不是MunitTools::getResourceAsString
来比较 MUNIT 2.1
<munit-tools:assert-that doc:name="Assert That - JSON compare" doc:id="2fab5bf0-a710-4619-a7a9-262a867e0ad9" expression="#[payload]" is="#[MunitTools::equalTo(readUrl('classpath://sample_data/test_payload.json', 'application/json'))]" message="payload is not correct" />