如何处理许多 "Then" 个步骤定义?

How to handle many "Then" step definitions?

考虑一个具有多个 "Then" 步骤定义的场景,这些步骤定义用作针对响应负载的断言:

  ...
  When a response is received
  Then the response should have an element "foo" with the content "bar"
  And the response should contain 1 "foobar" element
  And the response should have an element "rab" with the content "oof"
  ...

Citrus 处理未知数量验证的预期方式是什么?在调用 receive() 之前可以定义几个验证器吗?这可以用 validationCallback() 和最少的 Gherkin 重写来处理吗?

当前的实现使用 validationCallback() 将负载存储为实例变量,然后针对该变量进行验证。但是,利用柑橘的力量会更好。

您可以在 Given 部分甚至场景背景中使用命名消息:

Given message fooResponse
    And <fooResponse> payload is "Hi my name is Foo!"
    And <fooResponse> header operation is "sayHello"
    ...

您可以在这样的场景中使用命名消息:

Scenario: Send and receive
  When <client> sends message <fooRequest>
  Then <client> should receive message <fooResponse>

当 Citrus 验证器比较收到的和预期的消息内容时,所有元素的验证都会自动完成。

您还可以将消息创建提取到 Java POJO 消息创建者:

Background:
  Given message creator com.consol.citrus.FooMessageCreator

public class FooMessageCreator {
    @MessageCreator("fooResponse")
    public Message createEchoResponse() {
        return new DefaultMessage("Hi my name is Foo!")
                    .setHeader("operation", "sayHello");
    }
}

如果您需要处理大型 XML/Json 有效载荷,您还可以从外部文件模板资源加载该有效载荷。与其在单独的显式 Then 语句中设置每个元素,不如使用基于模板的比较验证,该验证还可以使用 @ignore@ 部分来忽略与验证无关的消息部分。