Reputation: 35
I have a java program as below,
package test;
class HelloWorld {
public static void main(String[] args) {
System.out.println("Hi");
String a="World";
}
}
And groovy script as below,
import test.HelloWorld
HelloWorld.main(null)
And I am using this groovy script in SOAPUI after keeping the jar file in /bin/ext SOAPUI folder.
I am able to execute this groovy script in console.
But my requirement is,I need to pass variable say "a" in java program in SOAPUI test input.
For eg: Add test input in soapui
<add>
<a> "Variable a" </a>
<b>5</b>
</add>
I want to refer that variable coming out of java program in this test input.Please let me know the way.
Upvotes: 2
Views: 1332
Reputation: 18507
Java and Groovy integrates smooth. However to access the String a
value a
must be and attribute in object not a variable in a method. Let me explain; in your case for example in the Java part instead of a main
method create an object like:
package test;
class HelloWorld {
private String a;
public HelloWorld(){
this.a = "World";
}
public String getA(){
return this.a;
}
}
Compile and add the jars to SOAPUI_HOME/bin/ext
.
Then from groovy script in SOAPUI you can instantiate the class and get the value:
import test.HelloWorld
def a = new HelloWorld().getA()
log.info a // this prints --> World
If besides you want to use this value in a request you've to set the property value at some test level (for example in testCase):
import test.HelloWorld
def a = new HelloWorld().getA()
log.info a // this prints --> World
testRunner.testCase.setPropertyValue('myVar',a)
Now that your variable is set you can use the follow notation to use it in your requests: ${#TestCase#myVar}
, in your request sample:
<add>
<a>${#TestCase#myVar}</a>
<b>5</b>
</add>
Hope it helps,
Upvotes: 4