我正在尝试测试以下两个 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/