2016-11-04 106 views
0

我有異步測試場景。我打電話給我的SUT(被測系統),它返回確認響應。接下來,正確的響應是異步返回的。我配置被指定爲接收以下回調響應我的模擬服務器:Citrus Framework異步HTTP測試場景

<citrus-http:server id="receiveCallbackMockService" 
     port="${server.port}" 
     auto-start="true" 
     timeout="10000" 
     endpoint-adapter="dispatchingEndpointAdapter" /> 

<citrus:dispatching-endpoint-adapter id="dispatchingEndpointAdapter" 
     mapping-key-extractor="mappingKeyExtractor" 
     mapping-strategy="mappingStrategy"/> 


<bean id="mappingKeyExtractor" class="com.consol.citrus.endpoint.adapter.mapping.HeaderMappingKeyExtractor"> 
    <property name="headerName" value="#{T(com.consol.citrus.http.message.HttpMessageHeaders).HTTP_REQUEST_URI}"/> 
</bean> 

<bean id="mappingStrategy" 
    class="com.consol.citrus.endpoint.adapter.mapping.SimpleMappingStrategy"> 
    <property name="adapterMappings"> 
     <map> 
      <entry key="/callback" value-ref="responseAdapter"/> 
     </map> 
    </property> 
</bean> 

<citrus:static-response-adapter id="responseAdapter"> 
</citrus:static-response-adapter> 

然後我用應該接受回調

receive(receiveCallbackMockService) 
    .payload(new ClassPathResource("/async/callbackExpectedRequest01.xml")); 

預期的有效載荷的Java DSL代碼,但我得到這樣一個例外,執行時測試:

14:25:43,516 ERROR  citrus.Citrus| TEST FAILED HELLO_ASYNC_01: output 039 <com.mycompany.myproject> Nested exception is: 
com.consol.citrus.exceptions.CitrusRuntimeException: Unable to create endpoint for static endpoint adapter type 'class com.consol.citrus.endpoint.adapter.RequestDispatchingEndpoint 
Adapter' 
     at com.consol.citrus.endpoint.adapter.StaticEndpointAdapter.getEndpoint(StaticEndpointAdapter.java:35) 
     at com.consol.citrus.server.AbstractServer.createConsumer(AbstractServer.java:200) 
     at com.consol.citrus.actions.ReceiveMessageAction.receive(ReceiveMessageAction.java:146) 
     at com.consol.citrus.actions.ReceiveMessageAction.doExecute(ReceiveMessageAction.java:125) 
     at com.consol.citrus.actions.AbstractTestAction.execute(AbstractTestAction.java:42) 
     at com.consol.citrus.dsl.actions.DelegatingTestAction.doExecute(DelegatingTestAction.java:54) 
     at com.consol.citrus.actions.AbstractTestAction.execute(AbstractTestAction.java:42) 
     at com.consol.citrus.TestCase.executeAction(TestCase.java:214) 
     at com.consol.citrus.TestCase.doExecute(TestCase.java:142) 
     at com.consol.citrus.actions.AbstractTestAction.execute(AbstractTestAction.java:42) 
     at com.consol.citrus.Citrus.run(Citrus.java:254) 
     at com.consol.citrus.dsl.testng.TestNGCitrusTest.invokeTestMethod(TestNGCitrusTest.java:124) 
     at com.consol.citrus.dsl.testng.TestNGCitrusTestDesigner.invokeTestMethod(TestNGCitrusTestDesigner.java:73) 
     at com.consol.citrus.dsl.testng.TestNGCitrusTest.run(TestNGCitrusTest.java:100) 
     at com.consol.citrus.dsl.testng.TestNGCitrusTest.run(TestNGCitrusTest.java:58) 
     at org.testng.internal.MethodInvocationHelper.invokeHookable(MethodInvocationHelper.java:209) 
     at org.testng.internal.Invoker.invokeMethod(Invoker.java:639) 
     at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:820) 
     at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1128) 
     at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:129) 
     at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:112) 
     at org.testng.TestRunner.privateRun(TestRunner.java:782) 
     at org.testng.TestRunner.run(TestRunner.java:632) 
     at org.testng.SuiteRunner.runTest(SuiteRunner.java:366) 
     at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:361) 
     at org.testng.SuiteRunner.privateRun(SuiteRunner.java:319) 
     at org.testng.SuiteRunner.run(SuiteRunner.java:268) 
     at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52) 
     at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86) 
     at org.testng.TestNG.runSuitesSequentially(TestNG.java:1244) 
     at org.testng.TestNG.runSuitesLocally(TestNG.java:1169) 
     at org.testng.TestNG.run(TestNG.java:1064) 
     at org.apache.maven.surefire.testng.TestNGExecutor.run(TestNGExecutor.java:132) 
     at org.apache.maven.surefire.testng.TestNGDirectoryTestSuite.executeMulti(TestNGDirectoryTestSuite.java:224) 
     at org.apache.maven.surefire.testng.TestNGDirectoryTestSuite.execute(TestNGDirectoryTestSuite.java:113) 
     at org.apache.maven.surefire.testng.TestNGProvider.invoke(TestNGProvider.java:146) 
     at org.apache.maven.surefire.booter.ForkedBooter.invokeProviderInSameClassLoader(ForkedBooter.java:286) 
     at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:240) 
     at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:121) 

我應該如何配置HTTP柑橘模擬服務器接收回調請求(預計請求負載進行驗證)在異步測試scenari Ø?

回答

0

剛剛找到一段時間來分享我的解決方案。首先 - 有一篇文章(http://citrus.895196.n3.nabble.com/Citrus-to-act-as-a-mock-server-td4023066.html)告訴我們,「你需要的是一個獨立的模擬器,它等待傳入的請求併發送預定義的響應消息,這不是Citrus作爲測試框架的主要焦點。」 。但是,我發現了一個解決辦法:

1)使用全局變量作爲標誌告訴我們,沒有異步請求從SUT到達:

Spring上下文:

<bean id="globalVariables" class="com.consol.citrus.variables.GlobalVariables"></bean> 

Java代碼:

@Autowired 
private GlobalVariables globalVariables 

並在開始等待SUT異步回調請求之前設置變量(標誌)

globalVariables.getVariables().put("SUTcallbackStatus", "waiting"); 

2)延伸IteratingConditionExpression - 重寫爲下面的evaluate方法:

@Override 
public boolean evaluate(int index, TestContext context) { 

    if(index > threshold){ 
     testToFail.fail("The async message didn't arrived to the mock server."); 
     return false; 
    } 

    if(globalVariables.getVariables().get("SUTcallbackStatus").equals("received")){ 
     return false; 
    } 

    return true; 
} 

testToFailthreshold從上述代碼的變量應該從Test類進行設置:

myIteratingConditionExpression.setThreshold(50); 
myIteratingConditionExpression.setTestToFail(this); 

它允許你控制測試應等待響應的時間(threshold * 0.5秒),並允許您指向測試,如果未收到回叫響應在假定的時期內。

3)延長StaticResponseEndpointAdapter - 覆蓋以下方法

@Override 
public Message handleMessageInternal(Message message) { 

    globalVariables.getVariables().put("SUTcallbackStatus", "received"); 
    return super.handleMessageInternal(message); 
} 

,並使用該適配器爲您的模擬服務適配器:然後

<citrus-http:server id="asyncResponseMockService" 
    port="${server.callback.port}" 
    auto-start="true" 
    timeout="10000" 
    endpoint-adapter="myResponseAdapter" /> 

4),在您的測試類 - 開始等待異步回調請求 - 只需使用iterate動作自定義iteratingConditionExpression:

iterate(sleep(0.5)).condition(myIteratingConditionExpression); 

這爲我做的工作:)

0

您在Citrus服務器組件上同時使用靜態響應適配器和receive()操作。這不起作用。靜態響應適配器將始終跳入,並且測試中的接收操作永遠不會收到任何請求。

如果您想在測試用例中接收來自服務器組件的請求,請結合靜態響應適配器刪除調度響應適配器。

+0

感謝您的提示。所以我從'citrus-context.xml'中的citrus-http:server id =「receiveCallbackMockService」中刪除了'endpoint-adapter =「dispatchingEndpointAdapter」',現在我得到了這樣一個異常:'com.consol.citrus.exceptions。 ActionTimeoutException:從通道'receiveCallbackMockService.inbound''接收消息時的操作超時。看起來更好,但不夠好:)我很確定,該服務器響應。 – supertramp