java - Citrus Framework - 可以从响应中分配变量吗?

标签 java citrus-framework

我正在尝试测试以下两个 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: [...]");
}

最佳答案

您可以使用 JsonPath 表达式:

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

或者您可以在有效负载中使用创建变量匹配器:

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

这两个选项都会创建一个新的测试变量 apiVersion,您可以在进一步的测试操作中使用 ${apiVersion} 引用该变量。

关于java - Citrus Framework - 可以从响应中分配变量吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43143927/

相关文章:

java - 使用 JFreeChart 在同一框架上显示多个图表

java - 未从 JUnit 测试加载 'application.yml' 中的 Spring Boot 属性

java - Apache tomcat + eclipselink下的Servlet

java - 如何确定 JSON 键是否已设置为 null 或未提供

java - 有没有办法在柑橘框架中关闭 ssl 验证?

java - 如何将 ORC BytesColumnVector 值设置为 NULL?

java - 使用 Cucumber(和/或 Citrus)执行异步步骤

java - 使用 Citrus 框架和 Spring Integration 从 tcp 服务器接收消息超时

java - 使用 <payload> 元素的 Citrus <TestMessage> 子标签时,出现以下错误