2017-02-22 55 views
0

Can I use Citrus variable in Citrus static response adapter payload?如何訪問變量中(柑橘)的靜態響應適配器

跟進我使用柑橘2.7和我的測試擴展TestNGCitrusTestRunner:

@Test 
@CitrusTest 
public void testRequestOk() { 
    variable("myTest", "baz"); 

    http(builder -> builder 
     .client("fooClient") 
     .send() 
     .post("/foo/bar") 
     .payload(new ClassPathResource("/foo/bar-baz.xml")) 
     .messageType(MessageType.XML) 
     .contentType("application/xml") 
     .accept("application/xml")); 

    http(builder -> builder 
     .client("fooClient") 
     .receive() 
     .response(HttpStatus.OK) 
     .validate("foo.bar", "baz")); 
} 

請求被髮送到SUT ,這反過來又觸發了兩個對Citrus的http調用(對mockOne和mockTwo)。

與以下配置:

<citrus-http:server id="mockOne" 
        port="9090" 
        auto-start="true" 
        endpoint-adapter="staticResponseAdapter" 
        security-handler="securityHandlerOne"/> 

<citrus-http:server id="mockTwo" 
        port="9080" 
        auto-start="true" 
        endpoint-adapter="dispatchingEndpointAdapter" 
        security-handler="securityHandlerTwo"/> 

... 

<citrus:static-response-adapter id="staticResponseAdapter"> 
    <citrus:payload> 
     <![CDATA[ 
     <?xml version="1.0" encoding="UTF-8"?> 
     <foo> 
      <bar>${myTest}</bar> 
     </foo> 
     ]]> 
    </citrus:payload> 
</citrus:static-response-adapter> 

我收到:com.consol.citrus.exceptions.CitrusRuntimeException: Unknown variable 'myTest'

在日誌中,我看到intially的變量設置:

15:02:48,696 DEBUG  citrus.Citrus| TEST STEP 1: create-variables 
15:02:48,697 INFO reateVariablesAction| Setting variable: myTest to value: foo 
15:02:48,697 DEBUG context.TestContext| Setting variable: myTest with value: 'foo' 

但右邊的變量替換之前應該發生,柑橘這樣做:

15:02:49,281 DEBUG ngHandlerInterceptor| Received Http request: 
... 
15:02:49,297 DEBUG t.TestContextFactory| Created new test context - using global variables: '{}' 
15:02:49,299 DEBUG rusDispatcherServlet| Could not complete request 
com.consol.citrus.exceptions.CitrusRuntimeException: Unknown variable 'myTest' 

我做錯了什麼或者這是預期的行爲?

回答

0

這是靜態響應適配器創建自己的測試環境時的預期行爲。你可以看到在日誌中

15:02:49,297 DEBUG t.TestContextFactory| Created new test context - using global variables: '{}' 

因此,通常情況下,靜態響應適配器無法訪問測試實例的測試變量。這僅僅是因爲傳入請求與運行測試之間沒有可能的關聯。這是描述「靜態」響應適配器中的「靜態」響應的內容。

如果您想要獲得更多動態響應消息,您應該在測試中包含服務器通信,其中包括http(builder -> builder.server("mockOne").receive())http(builder -> builder.server("mockOne").send().response())

然後您可以使用測試行爲(http://www.citrusframework.org/reference/html/behaviors.html)代替靜態響應適配器。通過這種方式,您可以定義該服務器通信一次並在多次測試中使用它

public class MockOneServerBehavior extends AbstractTestBehavior { 
    public void apply() { 
     http(builder -> builder.server("mockOne") 
           .receive() 
           .post()); 

     http(builder -> builder.server("mockOne") 
           .send() 
           .response() 
           .payload("<foo><bar>${myTest}</bar></foo>")); 
    } 
} 

行爲能作爲的行爲得到在您的測試顯式應用來訪問你的測試的測試變量。

@Test 
@CitrusTest 
public void testRequestOk() { 
    variable("myTest", "baz"); 

    http(builder -> builder 
     .client("fooClient") 
     .send() 
     .post("/foo/bar") 
     .payload(new ClassPathResource("/foo/bar-baz.xml")) 
     .messageType(MessageType.XML) 
     .contentType("application/xml") 
     .accept("application/xml")) 
     .fork(true); 

    MockOneServerBehavior mockOneBehavior = new MockOneServerBehavior(); 
    applyBehavior(mockOneBehavior); 

    http(builder -> builder 
     .client("fooClient") 
     .receive() 
     .response(HttpStatus.OK) 
     .validate("foo.bar", "baz")); 
} 

請注意,我添加了一個選項HTTP客戶端發送操作。這是因爲Http協議本質上是同步的,Citrus同時作爲客戶端和服務器。

+0

感謝您的回覆和建議的解決方案。它在給我的http服務器組件添加超時後完美運行。 –