Forum Discussion

vihang's avatar
Occasional Contributor
9 years ago

how to pass parameters in groovy script using some java code


I am trying to invoke my groovy script using java code. I want to pass parameters into it can I do that? I saw some interface like soapui script or script engine. would it help?

7 Replies

  • nmrao's avatar
    Champion Level 3
    What are you trying to achieve? If you have java, then you can directly use it instead calling groovy again.
    • vihang's avatar
      Occasional Contributor

      Actually I want to automate my test. for this I have written a script in groovy in soapui. Now I am calling that script using java code. but I want to pass parameters given by user. Previously I was reading from file

      • nmrao's avatar
        Champion Level 3

        Still not sure what your use case is.

        If you have soapui project, you can run it from soapui tool, or testrunner.bat/sh utility or from Java code using Junit etc. In any of these cases, it does not arise a case where you need to execute a groovy script. Looks you doing something different. If you tell exactly, that will help better.


        Can you clarify?


        I would also like to give you another example where I use it exactly reverse way that you mentioned


        I use IDE create a class / methods in Java, this is entirely a different java project, compile, create jar, place it SOAPUI_HOME/bin/ext directory and soapui's groovy script call the java api like create object and call method.


        Now it comes to properties.


        If you see a groovy script test step editor's top right, you would read as:

        "script is invoked with context, log, testRunner variables"


        So, now i would just refine java class's Constructor which can take these parameters and have these variables available in java.

        Here is sample java code:


        * this class is initialized and called from groovy script
        import com.eviware.soapui.model.testsuite.TestCaseRunContext
        import com.eviware.soapui.model.testsuite.TestCaseRunner
        import org.apache.log4j.Logger
        public class ScriptHerlper {
          public TestCaseRunContext context;
          public Logger log;
          public TestCaseRunner testRunner;
          public ScriptHerlper(TestCaseRunContext context, Logger log) {
            this.context = context;
            this.log = log;
            this.testRunner = context.getTestRunner();
          public void execute() {
  "This is in execute method of ScriptHerlper class");
            String myPropertyValue = context.getTestCase().getPropertyValue("ACCESS_TOKEN");
  "TestCase property ACCESS_TOKEN value is :"+myPropertyValue);

        As mentioned, compile above class, create jar, place it under SOAPUI_HOME/bin/ext directory and restart soapui.


        A groovy script now will look:


        //creating a test case level property say ACCESS_TOKEN
        context.testCase.setPropertyValue('ACCESS_TOKEN', '1234-a3h-45ab-12dh2g')
        def myScriptHelper = new ScriptHelper(context, log)
        //ACCESS_TOKEN will be accessed in execute method.

        Hope this helps at least understanding of how properties can be accessed between groovy and java.


        Also note that, context is a variables which is something that varies at different levels of scripting such as Setup and Tear down scripts of test case, Test Suite, and Project.