SoapUI项目可以在加载时随机运行脚本。使用日志和项目变量调用Load脚本。
在我的共享库中,我有一个方法- addAsserts(),它遍历整个项目并将模式遵从性断言添加到SOAP测试步骤中。在我的加载脚本中,我调用了共享方法
addAsserts(this) 将“this”作为参数传递,并将closure.delegate设置为addAsserts方法中的“project”变量,使其可在闭包范围内访问
addAsserts方法在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()
}问题:
是否可以使用某种getCallerInstance()方法在运行时检测调用实例?
发布于 2011-11-30 16:12:59
不我不相信这是可能的。也不在Java中(您可以使用一些可怕的黑客攻击查找调用类的名称/方法,但不能找到类本身的实例)
编辑...
使用类别可能是可能的(但我对SoapUI没有经验,所以我不知道这种技术是否适合)
假设我们有一个定义如下的类Example:
class Example {
String name
}然后,我们可以编写一个非常类似于示例代码的类,在本例中,该类将设置闭包的委托,闭包将打印出委托的name属性(因为我们已经将解决策略设置为DELEGATE_ONLY)。
class AssetAddingCategory {
static def addAsserts( that ) {
def closure = {
"Name of object: $name"
}
closure.delegate = that
closure.resolveStrategy = Closure.DELEGATE_ONLY
closure.call()
}
}稍后,在我们的代码中,可以这样做:
def tim = new Example( name:'tim' )
use( AssetAddingCategory ) {
println tim.addAsserts()
}这个会打印出来
Name of object: timhttps://stackoverflow.com/questions/8329029
复制相似问题