Unit testing 模拟外部REST API调用时MUnit测试失败
我有以下Mule流,用于从外部API检索一些详细信息,然后将其映射到一些有效负载以返回到调用客户端:Unit testing 模拟外部REST API调用时MUnit测试失败,unit-testing,mule,munit,Unit Testing,Mule,Munit,我有以下Mule流,用于从外部API检索一些详细信息,然后将其映射到一些有效负载以返回到调用客户端: <flow name="get:/clients/{clientId}:api-config"> <http:request config-ref="HTTPS_Request_Configuration" path="/api/clients/{clientId}" method="GET" host="host.com" port="443" doc:name="Find
<flow name="get:/clients/{clientId}:api-config">
<http:request config-ref="HTTPS_Request_Configuration" path="/api/clients/{clientId}" method="GET" host="host.com" port="443" doc:name="Find Client by ID">
<http:request-builder>
<http:uri-param paramName="clientId" value="#[flowVars['clientId']]"/>
</http:request-builder>
<http:success-status-code-validator values="0..599"/>
</http:request>
<logger message="#[payload]" level="INFO" doc:name="Logger"/>
...
当我尝试运行此测试时,我遇到了一个异常错误。似乎测试由于HTTP超时而失败。我不明白当HTTP请求处理器被模拟,因此不应该进行外部调用时,这是怎么可能的
我看到的错误消息如下:
ERROR 2016-02-19 12:25:59,359 [main] org.mule.exception.DefaultMessagingExceptionStrategy:
********************************************************************************
Message : Error sending HTTP request. Message payload is of type: String
Code : MULE_ERROR--2
--------------------------------------------------------------------------------
Exception stack is:
1. Timeout exceeded (java.util.concurrent.TimeoutException)
com.ning.http.client.providers.grizzly.GrizzlyAsyncHttpProvider:426 (null)
2. Error sending HTTP request. Message payload is of type: String (org.mule.api.MessagingException)
org.mule.module.http.internal.request.DefaultHttpRequester:287 (http://www.mulesoft.org/docs/site/current3/apidocs/org/mule/api/MessagingException.html)
--------------------------------------------------------------------------------
Root Exception stack trace:
java.util.concurrent.TimeoutException: Timeout exceeded
at com.ning.http.client.providers.grizzly.GrizzlyAsyncHttpProvider.timeout(GrizzlyAsyncHttpProvider.java:426)
at com.ning.http.client.providers.grizzly.GrizzlyAsyncHttpProvider$3.onTimeout(GrizzlyAsyncHttpProvider.java:274)
at org.glassfish.grizzly.utils.IdleTimeoutFilter$DefaultWorker.doWork(IdleTimeoutFilter.java:398)
+ 3 more (set debug level logging or '-Dmule.verbose.exceptions=true' for everything)
********************************************************************************
在您的测试中,为什么要在这里调用
http:endpoint
另外,我没有看到调用测试中的实际流的
调用
编辑:
基本测试结构可能遵循以下框架:
<munit:config name="munit" doc:name="MUnit configuration"/>
<spring:beans>
<spring:import resource="classpath:munit-test.xml"/>
</spring:beans>
<munit:test name="new-test-suite-get:/clients/{clientId}:api-configTest" description="Test">
<mock:when messageProcessor=".*:.*" doc:name="Mock">
<mock:with-attributes>
<mock:with-attribute name="doc:name"
whereValue="#['Find Client by ID']" />
</mock:with-attributes>
<mock:then-return payload="#[getResource('sample-client.json').asStream()]"
mimeType="application/json" />
</mock:when>
<set-variable variableName="clientId" value="#['null']"
doc:name="clientId" />
<!-- Below will make an actual call to the flow -->
<flow-ref name="get:/clients/{clientId}:api-config" doc:name="Flow-ref to get:/clients/{clientId}:api-config"/>
</munit:test>
谢谢您的评论。此测试由APIKit模块生成,并修改为包含模拟。我对使用MUnit非常陌生,如果有更好的方法来实现我的目标,我将非常感谢您的建议。我正在测试的流程在我的原始帖子中。我为您的流程添加了示例测试,以帮助您开始
<munit:config name="munit" doc:name="MUnit configuration"/>
<spring:beans>
<spring:import resource="classpath:munit-test.xml"/>
</spring:beans>
<munit:test name="new-test-suite-get:/clients/{clientId}:api-configTest" description="Test">
<mock:when messageProcessor=".*:.*" doc:name="Mock">
<mock:with-attributes>
<mock:with-attribute name="doc:name"
whereValue="#['Find Client by ID']" />
</mock:with-attributes>
<mock:then-return payload="#[getResource('sample-client.json').asStream()]"
mimeType="application/json" />
</mock:when>
<set-variable variableName="clientId" value="#['null']"
doc:name="clientId" />
<!-- Below will make an actual call to the flow -->
<flow-ref name="get:/clients/{clientId}:api-config" doc:name="Flow-ref to get:/clients/{clientId}:api-config"/>
</munit:test>