How do I access a citrus http receive message body in Java?

喜夏-厌秋 提交于 2020-08-09 09:23:47

问题


I'm using cucumber and citrus together, and in my @Then definition, I have the citrus HTTP response:

@CitrusResource TestRunner runner;

runner.http(builder -> {
    final HttpClientResponseActionBuilder rab = 
        builder.client("citrusEndpointAPI").receive()
        .response(HttpStatus.OK).messageType(MessageType.JSON)
        .contentType(MediaType.APPLICATION_JSON_VALUE);

Is there a way to store the returned JSON message body into a java JSON object?


回答1:


You can use the local message store. Each message should be saved to that local in memory storage during the test. You can access the stored messages later in that test case via its name:

receive(action -> action.endpoint("sampleEndpoint")
    .name("sampleMessage")
    .payload("..."));

echo("citrus:message(sampleMessage.payload())");

Please note that we named the received message sampleMessage. You can access the message store via test context in custom test actions, too.

context.getMessageStore().getMessage("sampleMessage");

Besides that you could also use a custom message validation callback in Java DSL. Here you have full access to the received message content.

receive(action -> action.endpoint("sampleEndpoint")
    .validationCallback((message, context) -> {
        //Do something with message content
        message.getPayload(String.class);
    }));


来源:https://stackoverflow.com/questions/45596054/how-do-i-access-a-citrus-http-receive-message-body-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!