Java 柑橘框架-可以从响应中分配变量吗?
我正在尝试测试以下两个REST调用: 请求1Java 柑橘框架-可以从响应中分配变量吗?,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 ge
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: [...]");
}
一种有效的方法是从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中是否有命名消息的方法
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}
引用该变量。谢谢Christoph!