WSO2 Integration Studio单元测试报服务无效操作类型错误
报错根因
这个错误和接口本身逻辑无关,curl能正常返回预期结果已经证明服务运行正常。报错的核心原因是WSO2 Micro Integrator单元测试框架无法解析你配置的断言表达式,将裸写的$body误识别为后端服务的操作名称,因此抛出「is not a valid operation type for services」的错误,和是否收到响应没有关系。
测试配置存在的问题
<request-path>配置错误:该字段仅需要填写API的相对访问路径,你当前填入了完整URL,多写了http://localhost:9008前缀。- 断言取值语法错误:MI单元测试框架不支持直接使用
$body作为取值表达式,必须显式指定表达式类型,获取JSON格式的完整响应体需要使用标准JSON Path表达式jsonPath($)。 - 预期值格式不匹配:你在
<expected>中定义的JSON带多余空格(比如姓名写为thomas collins、时间写为9.00 a.m - 11.00 a.m),和接口实际返回的无空格格式不一致,即便修正表达式问题,断言依然会失败。
修正后的测试配置
将测试套件XML调整为如下内容即可正常运行断言:
<unit-test> <artifacts> <test-artifact> <artifact>/SampleServicesConfigs/src/main/synapse-config/api/HealthcareAPI.xml</artifact> </test-artifact> <supportive-artifacts> <artifact>/SampleServices/SampleServicesConfigs/src/main/synapse-config/endpoints/QueryDoctorEP.xml</artifact> </supportive-artifacts> <registry-resources/> <connector-resources/> </artifacts> <test-cases> <test-case name="querydoctorCategoryTestSuccess"> <input> <request-path>/healthcare/querydoctor/surgery</request-path> <request-method>GET</request-method> <request-protocol>http</request-protocol> </input> <assertions> <assertEquals> <actual>jsonPath($)</actual> <expected><![CDATA[[ {"name":"thomascollins","hospital":"grandoakcommunityhospital","category":"surgery","availability":"9.00a.m-11.00a.m","fee":7000.0}, {"name":"anneclement","hospital":"clemencymedicalcenter","category":"surgery","availability":"8.00a.m-10.00a.m","fee":12000.0}, {"name":"sethmears","hospital":"pinevalleycommunityhospital","category":"surgery","availability":"3.00p.m-5.00p.m","fee":8000.0} ]]]></expected> <message>Body does not match expected value!</message> </assertEquals> </assertions> </test-case> </test-cases> <mock-services/> </unit-test>
内容的提问来源于stack exchange,提问作者viggnah
相关产品推荐
相关产品推荐

