Groovy test class using #ContextHierarchy - groovy

I am using Groovy for my Spring application and I try to use multiple XML beans configurations inside my test. I tried using #ContextHierarchy but following usage example didn't work:
#RunWith(SpringRunner)
#SpringBootTest
#ContextHierarchy({#ContextConfiguration("a.xml"), ContextConfiguration("b.xml")})
public class MyTest {
...
}
I have also tried:
#ContextConfiguration(locations={"a.xml", "b.xml"})
but it didn't work as well.
As I understand Groovy doesn't like the "{" "}", because it has different mean to it....?
How can I write the Testclass in groovy with two conifiguration xmls defined?

You can define multiple XML configuration sources with #ContextConfiguration annotation. Let's say I have 2 XML config files located in src/main/resources - beans1.xml and beans2.xml. I can use them in my test with:
#ContextConfiguration(locations = ['classpath:beans1.xml', 'classpath:beans2.xml'])
The main difference comparing to Java is that Groovy uses [] for arrays instead of Java's {}, because {} represents Groovy's closure.

Related

JMeter JSR223 script - how do we load multiple groovy files?

I want to structure my code well to use in JMeter, so I plan to have the groovy scripts in separate groovy files. The JSR223 script can load the script file and execute it. So far so good.
The groovy scripts will get complex, so I want to split the files into multiple files and use them. For e.g. have a file utils.groovy with content
public class SharedStore {
// Initializes the store to store all information about all the data being worked upon
def initializeStore() {
HashMap allStore = new HashMap();
// def props = org.apache.jmeter.util.JMeterUtils.getJMeterProperties()
props.put("${storeHashSet}", allStore);
}
This class will grow with more methods and will be used by all scripts invoked from JMeter. There will be other similar classes.
One JMeter sampler called InitializeEnvironment.groovy wants to use this method thus:
evaluate(new File("<<path>>/Utils.groovy")); // This line is an attempt to include utils.groovy
var shr = new SharedStore().initializeStore();
The above attempt to include groovy file is based on this discussion - https://stackoverflow.com/a/9154553 (and I have tried other options from that discussion to no avail)
Using the above option throws the error:
Script70.groovy: 2: unable to resolve class SharedStore
# line 2, column 11.
var shr = new SharedStore().initializeStore();
I would prefer to wrap things utility methods a class but I can live with having keep methods in global space.
If I do manage to load the additional file like in this - https://stackoverflow.com/a/15904699 - suggestion, without using "class" and have groovy wrap it up in a class for me, props are not available in utils.groovy unless I include the line def props = org.apache.jmeter.util.JMeterUtils.getJMeterProperties() and even then ${storeHashSet} cannot be used
No such property: storeHashSet for class: SharedStore
I want to be able to decompose the scripts into more manageable files, and be able to access JMeter variables and structures in these files.
Any advise on how I can do that in JMeter?
The most straightforward solution would be:
Compiling these individual files into classes
Packaging them into .jar (this one you can skip)
Putting the .jar or (class files if you skipped step 2) in the JMeter Classpath
Using the .jar file in your JSR223 Test Elements

Spring Boot external configuration in Groovy

How to get Spring boot to load external properties for Groovy?
Need something similar to java mechanism (application.properties in resources and ConfigBean with #Value annotations)?
When trying to use the same mechanism as with java, I don't know how to annotate the ConfigBean
#Component
public class ConfigBean {
#Value("${seleniumAddress}")
private String seleniumAddress; ...
and then in application.properties
seleniumAddress=http://localhost:4444/wd/hub
but with groovy I cannot annotate the field with #Value("${seleniumAddress}"
It throws an error complaining about "${}" - this is a special sequence in groovy.
So what mechanism should I use here?
Thank you
If you use "${}" for Spring placeholders in Groovy you have to make sure it's a String (not a GString): i.e. use '${}' (single quotes).

Access tied files through groovy in jenkins

I'm building a Jenkins plugin, and am handling the UI components using Groovy. In jelly, you can use "${it.something}" to access information in the java file tied to the jelly file, as shown here:
class:
public String getMyString() {
return "Hello Jenkins!";
}
jelly:
<j:jelly xmlns:j="jelly:core" xmlns:st="jelly:stapler" xmlns:d="jelly:define" xmlns:l="/lib/layout"
xmlns:t="/lib/hudson" xmlns:f="/lib/form">
${it.myString}
</j:jelly>
from https://wiki.jenkins-ci.org/display/JENKINS/Basic+guide+to+Jelly+usage+in+Jenkins.
I'd like to do the same thing in groovy, but can't seem to find an example of how it's done. Any examples?
After even more searching and some luck, I found the correct way to do this. If I was to use the class in my question but wanted to use groovy instead of jelly, the groovy code would look like this (this puts the string in the textbox):
package something.something;
import lib.JenkinsTagLib
import lib.FormTagLib
def f = namespace(lib.FormTagLib)
t=namespace(JenkinsTagLib.class)
f.entry(title:"text", field:"text") {
f.textbox(value:instance?.text)
}

How to define a global class in SoapUI as a groovy script?

I want to define a class in a groovy script that i could reuse trough other groovy script inside SoapUI.
I alredy tried to define my class inside a TestSuite property but it doesn't work. I would like to avoid defining the class in a JAR because we work in team and every one would have to import the JAR in their SoapUI in order to run my tests. I use SoapUI 3.6.1
Here's how my TestSuite is made :
TestSuite
TestCase
TestSteps
Init (Groovy Script)
GetResponse1 (Test Request)
Test1 (Groovy Script)
GetResponse2 (Test Request)
Test2 (Groovy Script)
To simplify me tests, i defined a class in 'Test1' and i would like to reuse this class in 'Test2'. So ideally i would define that class in 'Init' and it would be accessible to any other groovy script.
How can i achieve that?
Based on #Abhey Rathore's link, here is how to define a global class in SoapUI's groovy script:
Step 1: Declare the global class
Create a project called MyRepository
Under this project create a disabled TestSuite called lib
Under this test suite create a TestCase called MyClass
Under this test case create a groovy script called class
Your project tree should look like the image below:
Step 2: Write the global class code
Inside the groovy script called class, copy and and paste the code below:
class MyClass {
// The three following fields are MANDATORY
def log
def context
def testRunner
// The constructor below is MANDATORY
def MyClass(logIn, contextIn, testRunnerIn) {
this.log = logIn
this.context = contextIn
this.testRunner = testRunnerIn
}
def myMethod() {
this.log.info 'Hello World!'
}
}
// Other scripts elsewhere in SoapUI will access the reused class through their context
context.setProperty( "MyClass", new MyClass(log, context, testRunner) )
Step 3: Import the global class
From any project, you can import the global class by using the following snippet:
// Import the class
def currentProject = testRunner.testCase.testSuite.project
currentProject
.testSuites["lib"]
.testCases["MyClass"]
.testSteps["class"]
.run(testRunner, context)
// Now use it
context.MyClass.myMethod()
SoapUI 5.2.1
try this, I think will help you in reusable code.
http://forum.soapui.org/viewtopic.php?t=15744
I am pretty sure that you will have to create a JAR file and put it in \bin\ext.
SoapUI will automatically pick it up on restart (you should see it mentioned in the startup stuff).
You basically just create a Java or Groovy project, Export it (with Eclipse) and it will work. SoapUI will probably have your dependencies covered, but if not you can add those JARs too (safer than creating a runnable JAR since SoapUI might use different versions of what you use).
If you need help, post related questions.
In SoapUI 5.3.0 you can use properties that can be set and get on the context variable. That variable is available in script assertion snippet.
For example:
context.setProperty("prop", "value");
And from different test script you can get the value by:
context.getProperty("prop");
You can use the property from when you define property as a step of test suite or as a value for field in header. You can do it by ${=context.getProperty("prop");}
Write this into your Project's load script, then reload your project or just run it.
com.eviware.soapui.support.ClasspathHacker.addFile( project.context.expand('${projectDir}') )
After it has run, you can now put .groovy files into the same folder as your soapui project xml. Plus, there is no need to compile anything.
e.g. in MyClass.groovy:
class MyClass {String name}
You can import those classes as normal in all your other scripts:
import MyClass
assert new MyClass(name:"me") instanceof MyClass
The only caveat is that your load script (the one setting the classpath), cannot have those imports, because the script will fail to compile. If you want to import something during your project load script, just have your load script 'evaluate' another groovy script (in other words, run it), in which you can use the imports:
evaluate (new File (project.context.expand('${projectDir}') + 'myProjectLoadScript.groovy'))
//in myProjectLoadScript.groovy:
import MyClass
/* Do things using MyClass... */
This seems the easiest way to me, so I'm not sure why I couldn't find this answer elsewhere online.

Inspect Groovy object properties with Java reflection

I have an Expando class which I need to inspect its properties from Java.
In Groovy:
def worker = new Expando()
worker.name = "John"
worker.surname = "Doe"
In Java:
Introspector.getBeanInfo(groovyObject.getClass())
Is it possible to compile at runtime the class from the object in Groovy?
The Expando is completely dynamic. It does not generate any bytecode getters or setters and therefore cannot be used as a JavaBean. What do you need to use the bean introspector for? You may be able to implement that logic using the expando directly if you write it in Groovy.
You might try the JSR 223 / Script engine with Groovy (example here) if you are using Java 6. It allows you to evaluate Groovy code from Java.
Depending on the location/definition of the Expando, you might be able to get its properties by evaluating getProperties() (as of Groovy 1.7).

Resources