2

A SoapUI project can run random script upon load. Load Script is invoked with log and project variables.
In my shared lib I have method - addAsserts() that traverses the whole project and adds schema compliance assertions to SOAP test steps. In my Load Script I call shared method

addAsserts(this) 

passing 'this' as a parameter and set closure.delegate to it inside addAsserts method to make 'project' variable accessible within the closure scope

addAsserts method is defined in sharedUtil.groovy:

static def addAsserts(that){
        def closure={
            project.testSuites.each { testSuiteName, testSuiteObject -> 
                testSuiteObject.testCases.each { testCaseName, testCaseObject ->
                    testCaseObject.testSteps.each { testStepName, testStepObject -> 
                        if ("class com.eviware.soapui.impl.wsdl.teststeps.WsdlTestRequestStep" == testStepObject.getClass().toString() ) {
                            log.info "adding 'Schema Compliance' assertion to ${testSuiteName}/${testCaseName}/${testStepName}"
                            testStepObject.addAssertion('Schema Compliance')
                        }
                    }
                }
            }
        }//closure

    closure.delegate=that  // <--- i would like NOT to pass 'that' as parameter
                           // but rather detect in runtime with some kind of
                           // getCallerInstance() method
    return closure.call()
}

QUESTION:

Is it possible to detect caller instance in runtime with some kind of getCallerInstance() method ?

Kara
  • 6,115
  • 16
  • 50
  • 57
ludenus
  • 1,161
  • 17
  • 30

1 Answers1

1

No, I don't believe this is possible. Wasn't in Java either (you can find out the name/method of the calling class using some horrible stacktrace hacking, but not the instance of the class itself)


Edit...

It might be possible with a Category (but I am not experienced with SoapUI, so I don't know if this technique would fit)

Say we have a class Example defined like so:

class Example {
  String name
}

We can then write a class very similar to your example code, which in this case will set the delegate of the closure, and the closure will print out the name property of the delegate (as we have set the resolve strategy to DELEGATE_ONLY)

class AssetAddingCategory {
  static def addAsserts( that ) {
    def closure = {
      "Name of object: $name"
    }
    
    closure.delegate = that
    closure.resolveStrategy = Closure.DELEGATE_ONLY
    closure.call()
  }
}

Later on in our code, it is then possible to do:

def tim = new Example( name:'tim' )

use( AssetAddingCategory ) {
  println tim.addAsserts()
}

And this will print out

Name of object: tim
Community
  • 1
  • 1
tim_yates
  • 167,322
  • 27
  • 342
  • 338