在 Citrus Framework 中以不同的方法重用 HTTP 消息
Reuse HTTP message in different methods in Citrus Framework
问题:如何在 Citrus 框架中以两种不同的方法(在同一步骤中)重用相同的 HTTP 消息
版本:Citrus 2.8.0-SNAPSHOT; 黄瓜 3.0.2; Java 8
有这个小黄瓜:
Scenario: Client has permission to access the action
Given that client has access to action
When the client calls the endpoint /some-endpoint/client/1/action/some-action
Then the client receives status code of 200
And the client receives a response with {"action" : "some-action", "permission": "AUTHORIZED"}
和下面的一段Java代码:
@Then("the client receives status code of {int}")
public void the_client_receives_status_code_of(Integer statusCode) {
designer.http().client(httpClient).receive().response(HttpStatus.valueOf(statusCode))
.contentType("application/json;charset=UTF-8"):
}
@Then("the client receives a response with {string}")
public void the_client_receives_a_response_with(String payload) {
designer.http().client(httpClient).receive().response().payload(payload);
}
如果此代码是 运行,它将在方法 the_client_receives_a_response_with
中给出超时,因为 Citrus 期望收到第二条消息(send
方法只调用了一次)。
这里的objective是为了把HTTP代码的验证和payload分开,所以分开是通过创建两个方法来实现的。如何重用方法 the_client_receives_status_code_of
?
中收到的消息
已尝试以下方法但未成功:
为收到的消息命名:
designer.http().client(httpClient).receive().response(HttpStatus.valueOf(statusCode)).name("currentMessage")
.contentType("application/json;charset=UTF-8");
但是尝试像这样访问消息:
@CitrusResource
private TestContext testContext;
...
testContext.getMessageStore().getMessage("currentMessage");
Returns null
.
但是使用这个:
designer.echo("citrus:message(currentMessage)");
打印正确的消息。
那么,我如何在 Java 代码中访问消息,即访问消息以执行如下操作:
Assert.assertTrue(testContext.getMessageStore().getMessage("currentMessage").getPayload().equals(payload));
两种不同的方法。
你可以这样做:
@Then("the client receives a response with {string}")
public void the_client_receives_a_response_with(String payload) {
designer.action(new AbstractTestAction() {
public void doExecute(TestContext context) {
Assert.assertTrue(context.getMessageStore()
.getMessage("currentMessage")
.getPayload(String.class)
.equals(payload));
}
});
}
抽象操作始终随 运行 测试的当前 TestContext 实例一起提供。所以 @CitrusResource
注入在这里不起作用,因为你得到一个不同的实例,其中命名消息是未知的。
此外,您可以按照此处描述的默认命名消息步骤 BDD API 进行操作:https://citrusframework.org/citrus/reference/html/index.html#named-messages
也许消息创建者 BDD API 也会提供帮助:https://citrusframework.org/citrus/reference/html/index.html#message-creator-steps
问题:如何在 Citrus 框架中以两种不同的方法(在同一步骤中)重用相同的 HTTP 消息
版本:Citrus 2.8.0-SNAPSHOT; 黄瓜 3.0.2; Java 8
有这个小黄瓜:
Scenario: Client has permission to access the action
Given that client has access to action
When the client calls the endpoint /some-endpoint/client/1/action/some-action
Then the client receives status code of 200
And the client receives a response with {"action" : "some-action", "permission": "AUTHORIZED"}
和下面的一段Java代码:
@Then("the client receives status code of {int}")
public void the_client_receives_status_code_of(Integer statusCode) {
designer.http().client(httpClient).receive().response(HttpStatus.valueOf(statusCode))
.contentType("application/json;charset=UTF-8"):
}
@Then("the client receives a response with {string}")
public void the_client_receives_a_response_with(String payload) {
designer.http().client(httpClient).receive().response().payload(payload);
}
如果此代码是 运行,它将在方法 the_client_receives_a_response_with
中给出超时,因为 Citrus 期望收到第二条消息(send
方法只调用了一次)。
这里的objective是为了把HTTP代码的验证和payload分开,所以分开是通过创建两个方法来实现的。如何重用方法 the_client_receives_status_code_of
?
已尝试以下方法但未成功:
为收到的消息命名:
designer.http().client(httpClient).receive().response(HttpStatus.valueOf(statusCode)).name("currentMessage")
.contentType("application/json;charset=UTF-8");
但是尝试像这样访问消息:
@CitrusResource
private TestContext testContext;
...
testContext.getMessageStore().getMessage("currentMessage");
Returns null
.
但是使用这个:
designer.echo("citrus:message(currentMessage)");
打印正确的消息。
那么,我如何在 Java 代码中访问消息,即访问消息以执行如下操作:
Assert.assertTrue(testContext.getMessageStore().getMessage("currentMessage").getPayload().equals(payload));
两种不同的方法。
你可以这样做:
@Then("the client receives a response with {string}")
public void the_client_receives_a_response_with(String payload) {
designer.action(new AbstractTestAction() {
public void doExecute(TestContext context) {
Assert.assertTrue(context.getMessageStore()
.getMessage("currentMessage")
.getPayload(String.class)
.equals(payload));
}
});
}
抽象操作始终随 运行 测试的当前 TestContext 实例一起提供。所以 @CitrusResource
注入在这里不起作用,因为你得到一个不同的实例,其中命名消息是未知的。
此外,您可以按照此处描述的默认命名消息步骤 BDD API 进行操作:https://citrusframework.org/citrus/reference/html/index.html#named-messages
也许消息创建者 BDD API 也会提供帮助:https://citrusframework.org/citrus/reference/html/index.html#message-creator-steps