citrus icon indicating copy to clipboard operation
citrus copied to clipboard

How do I create a custom function in Spring (Boot).

Open hmmlopez opened this issue 2 years ago • 1 comments

Citrus Version Latest 4.0.0-M2

Question I'm using Citrus Cucumber and JUnit5. I would like to create a custom function, and found this on the website: https://citrusframework.org/docs/custom-functions/

  1. How do I create a bean in Spring and use that bean (whithout xml configuration) in Citrus as Citrus Custom Function?
  2. Is it necessary to create a custom function or I just create a Bean In Spring and inject that into my test and use that function to convert some values?

What I've tried so far I've looked at the following page: https://citrusframework.org/docs/custom-functions/

Additional information It expects some xml configuration. And I would like to use it without any xml configuration.

hmmlopez avatar Oct 09 '23 09:10 hmmlopez

You create that function in Citrus in order to have it resolved in any String template (e.g. message body, header and so on) like

{ "foo": "citrus:randomString()" }

now in case you like to add custom functions you can follow the XML based configuration and just use it as a Java Spring bean configuration instead. The mechanism stays exactly the same.

In general the idea is to add a new custom function library and add your custom functions to it. So you end up having

{ "foo": "custom:customFunc()" }

As an alternative to that you can add a resource type resolver for your function. For this you put a file into META-INF/citrus/function/foo and in this file you point to your custom function class like this

type=org.citrusframework.custom.function.FooFunction

YAKS is adding functions like this: https://github.com/citrusframework/yaks/blob/main/java/steps/yaks-http/src/main/resources/META-INF/citrus/function/resolveURL

Using this approach your custom function will directly be available in the citrus: function library

christophd avatar Oct 09 '23 13:10 christophd

stale issue.

bbortt avatar Aug 15 '24 06:08 bbortt