Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
hmmlopez opened this issue Oct 9, 2023 · 1 comment
Open

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

hmmlopez opened this issue Oct 9, 2023 · 1 comment

Comments

@hmmlopez
Copy link

hmmlopez commented Oct 9, 2023

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.

@christophd
Copy link
Member

christophd commented Oct 9, 2023

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants