Search code examples
citrus-framework

Using Java DSL TestRunner - How to access variable in my own Java-Code


I'm using the Java DSL with the TestRunner to define my tests and basically want to have the following steps:

  1. Load a JSON-Structure from a REST-Endpoint with the http-client
  2. Extract the received JSON-Structure into a Citrus variable
  3. Then I would like to manipulate this variable with some Java-Code for some reason - But I can't get it available in my Java-Code.

Basically like this:

http(builder -> builder.client("client").send()
    .get("/config").header("Content-Type", "application/json"));
http(builder -> builder.client("client").receive()
    .response(HttpStatus.OK).messageType(MessageType.JSON)
    .extractFromPayload("$", "myConfig"));

The variable is set:

echo("${myConfig}")

shows the desired output. But how can I get access to that variable to do something with it in pure Java?
I tried this approach:

variable("chgConfig", MyClassABC.myStaticFunctionXZY("${myConfig}");

And I already tried to use a Citrus Function, but I don't know how to call/include it using the Java DSL.
Is there perhaps any way, to get the actual TestContext and hand it over to my Java-Code?

My understanding was, when using the TestRunner, everything is immediately executed and I was thinking: Oh, that's cool, as I can just insert standard Java-Code when needed.


Solution

  • Yeah, I found the solution myself, just reading a second (or maybe a third ) time the documentation.

    I have changed my Test-Class like this:

        @CitrusTest
        @Test @Parameters("context")
        public void run(@Optional @CitrusResource TestContext context) {
    ...
    .....
    ...
        MyClassABC.myStaticFunctionXZY(context.getVariable("myConfig"));
    

    This injects me the actual Test-Context and allows me to access & manipulate variables from my Java-Code. Great stuff.