2017-03-31 63 views
0

我試圖測試以下兩個REST調用:柑橘框架 - 可以從響應分配變量嗎?

請求1

GET getLatestVersion 
Response: {"version": 10} 

請求2

POST getVersionData (body={"version": 10}) 
Response: {"version": 10, data: [...]} 

是否有可能從指定的 「版本」在同一個測試中請求1給請求2中使用的變量?

@CitrusTest(name = "SimpleIT.getVersionTest") 
public void getVersionTest() { 
    // Request 1 
    http() 
      .client("restClient") 
      .send() 
      .get("/getLatestVersion") 
      .accept("application/json"); 

    http() 
      .client("restClient") 
      .receive() 
      .response(HttpStatus.OK) 
      .messageType(MessageType.JSON) 
      // Can the version be assigned to a variable here? 
      .payload("{\"version\":10}"); 

    // Request 2 
    http() 
      .client("restClient") 
      .send() 
      .post("/getVersionData") 
      // Idealy this would be a Citrus variable from the previous response 
      .payload("{\"version\":10}") 
      .accept("application/json"); 

    http() 
      .client("restClient") 
      .receive() 
      .response(HttpStatus.OK) 
      .messageType(MessageType.JSON) 
      .payload("\"version\": 10, data: [...]"); 
} 

回答

1

您可以使用JsonPath表達式:

http() 
    .client("restClient") 
    .receive() 
    .response(HttpStatus.OK) 
    .messageType(MessageType.JSON) 
    .extractFromPayload("$.version", "apiVersion") 
    .payload("{\"version\":\"@[email protected]\"}"); 

或者您可以使用在有效載荷的創建變量匹配:

http() 
    .client("restClient") 
    .receive() 
    .response(HttpStatus.OK) 
    .messageType(MessageType.JSON) 
    .payload("{\"version\":\"@variable('apiVersion')@\"}"); 

這兩種方法都將創建一個新的測試變量apiVersion,你可以參考一下與${apiVersion}在進一步的測試行動。

+0

謝謝Christoph! –

0

一種方法,工程是從提取的TestContext的值並將其指定爲一個可變

@CitrusTest(name = "SimpleIT.getVersionTest") 
@Test 
public void getVersionTest(@CitrusResource TestContext context) { 

    http(httpActionBuilder -> httpActionBuilder 
     .client("restClient") 
     .send() 
     .get("/getLatestVersion") 
     .name("request1") 
     .accept("application/json") 
    ); 

    http(httpActionBuilder -> httpActionBuilder 
     .client("restClient") 
     .receive() 
     .response(HttpStatus.OK) 
     .messageType(MessageType.JSON) 
     .payload("{\"version\":\"@greaterThan(0)@\"}") 
    ); 

    // This extracts the version and assigns it as a variable 
    groovy(action -> action.script(new ClassPathResource("addVariable.groovy"))); 

    http(httpActionBuilder -> httpActionBuilder 
      .client("restClient") 
      .send() 
      .post("/getVersionData") 
      .payload("{\"version\":${versionId}}") 
      .accept("application/json") 
    ); 

    http(httpActionBuilder -> httpActionBuilder 
      .client("restClient") 
      .receive() 
      .response(HttpStatus.OK) 
      .messageType(MessageType.JSON) 
    ); 
} 

addVariable.groovy

這從響應中提取的變量,並增加了它到TestContext。

import com.consol.citrus.message.Message 
import groovy.json.JsonSlurper 

Message message = context.getMessageStore().getMessage("receive(restClient)"); 

def jsonSlurper = new JsonSlurper(); 
def payload = jsonSlurper.parseText((String) message.getPayload()) 

// Set the version as a variable in the TestContext 
context.getVariables().put("versionId", payload.version) 

問題

  • 有沒有更合適的方法來做到這一點?
  • 有沒有辦法爲MessageStore中的消息命名?