代码之家  ›  专栏  ›  技术社区  ›  pringi Syeda Uzma

Citrus框架下不同方法重用HTTP消息

  •  1
  • pringi Syeda Uzma  · 技术社区  · 7 年前

    :如何在Citrus框架中以两种不同的方法(在同一步骤中)重用相同的HTTP消息

    : ; 黄瓜3.0.2 ; 爪哇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 自从 希望收到第二条消息( send 方法只调用了一次)。

    这里的目标是将 超文本传输协议 代码,因此通过创建两个方法进行分离。如何重用方法中接收的消息 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");
    

    退换商品 null

    designer.echo("citrus:message(currentMessage)");
    

    打印正确的消息。

    那么,我如何访问Java代码中的消息,即访问消息以执行以下操作:

    Assert.assertTrue(testContext.getMessageStore().getMessage("currentMessage").getPayload().equals(payload));
    

    1 回复  |  直到 7 年前
        1
  •  2
  •   Christoph Deppisch    7 年前

    你可以这样做:

    @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

    https://citrusframework.org/citrus/reference/html/index.html#message-creator-steps