如何访问(柑橘)静态响应适配器中的变量

时间:2017-02-22 14:17:55

标签: citrus-framework

跟进Can I use Citrus variable in Citrus static response adapter payload?

我正在使用Citrus 2.7,我的测试扩展了TestNGCitrusTestRunner:

@Test
@CitrusTest
public void testRequestOk() {
    variable("myTest", "baz");

    http(builder -> builder
        .client("fooClient")
        .send()
        .post("/foo/bar")
        .payload(new ClassPathResource("/foo/bar-baz.xml"))
        .messageType(MessageType.XML)
        .contentType("application/xml")
        .accept("application/xml"));

    http(builder -> builder
        .client("fooClient")
        .receive()
        .response(HttpStatus.OK)
        .validate("foo.bar", "baz"));
}

请求被发送到SUT,SUT又会触发对Citrus的两次http调用(对于mockOne和mockTwo)。

使用以下配置:

<citrus-http:server id="mockOne"
                    port="9090"
                    auto-start="true"
                    endpoint-adapter="staticResponseAdapter"
                    security-handler="securityHandlerOne"/>

<citrus-http:server id="mockTwo"
                    port="9080"
                    auto-start="true"
                    endpoint-adapter="dispatchingEndpointAdapter"
                    security-handler="securityHandlerTwo"/>

...

<citrus:static-response-adapter id="staticResponseAdapter">
    <citrus:payload>
        <![CDATA[
        <?xml version="1.0" encoding="UTF-8"?>
        <foo>
            <bar>${myTest}</bar>
        </foo>
        ]]>
    </citrus:payload>
</citrus:static-response-adapter>

我收到:com.consol.citrus.exceptions.CitrusRuntimeException: Unknown variable 'myTest'

在日志中,我看到变量已设置为:

15:02:48,696 DEBUG        citrus.Citrus| TEST STEP 1: create-variables
15:02:48,697 INFO  reateVariablesAction| Setting variable: myTest to value: foo
15:02:48,697 DEBUG  context.TestContext| Setting variable: myTest with value: 'foo'

但在变量替换发生之前,Citrus就是这样做的:

15:02:49,281 DEBUG ngHandlerInterceptor| Received Http request:
...
15:02:49,297 DEBUG t.TestContextFactory| Created new test context - using global variables: '{}'
15:02:49,299 DEBUG rusDispatcherServlet| Could not complete request
com.consol.citrus.exceptions.CitrusRuntimeException: Unknown variable 'myTest'

我做错了什么或这是预期的行为?

1 个答案:

答案 0 :(得分:0)

这是预期的行为,因为静态响应适配器会创建自己的测试上下文。你可以在日志中看到

15:02:49,297 DEBUG t.TestContextFactory| Created new test context - using global variables: '{}'

因此,通常静态响应适配器无法访问测试实例的测试变量。这只是因为传入请求与正在运行的测试之间没有可能的关联。这就是“静态”响应适配器中描述“静态”响应的原因。

如果您想获得更多动态响应消息,则应在测试中使用http(builder -> builder.server("mockOne").receive())http(builder -> builder.server("mockOne").send().response())包含服务器通信。

您可以使用测试行为(http://www.citrusframework.org/reference/html/behaviors.html)代替静态响应适配器。这样,您可以定义该服务器通信一次,并在多个测试中使用它。

public class MockOneServerBehavior extends AbstractTestBehavior {
    public void apply() {
        http(builder -> builder.server("mockOne")
                               .receive()
                               .post());

        http(builder -> builder.server("mockOne")
                               .send()
                               .response()
                               .payload("<foo><bar>${myTest}</bar></foo>"));
    }
}

行为 能够访问测试的测试变量,因为行为会在测试中明确应用。

@Test
@CitrusTest
public void testRequestOk() {
    variable("myTest", "baz");

    http(builder -> builder
        .client("fooClient")
        .send()
        .post("/foo/bar")
        .payload(new ClassPathResource("/foo/bar-baz.xml"))
        .messageType(MessageType.XML)
        .contentType("application/xml")
        .accept("application/xml"))
        .fork(true);

    MockOneServerBehavior mockOneBehavior = new MockOneServerBehavior();
    applyBehavior(mockOneBehavior);

    http(builder -> builder
        .client("fooClient")
        .receive()
        .response(HttpStatus.OK)
        .validate("foo.bar", "baz"));
}

请注意我在http客户端发送操作中添加了 fork 选项。这是因为Http协议本质上是同步的,Citrus同时充当客户端和服务器。