【问题标题】:how to detect caller instance in SoapUI groovy script?如何在 SoapUI groovy 脚本中检测调用者实例?
【发布时间】:2014-03-11 07:13:00
【问题描述】:

SoapUI 项目可以在加载时运行随机脚本。 使用日志和项目变量调用加载脚本。
在我的共享库中,我有方法 - addAsserts() 遍历整个项目并将模式合规性断言添加到 SOAP 测试步骤。在我的加载脚本中,我调用了共享方法

addAsserts(this) 

将“this”作为参数传递并在 addAsserts 方法中设置closure.delegate 以使“项目”变量在闭包范围内可访问

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() 方法检测调用者实例?

【问题讨论】:

    标签: groovy soapui


    【解决方案1】:

    不,我不相信这是可能的。也不在 Java 中(您可以使用 some horrible stacktrace hacking 找到调用类的名称/方法,但不是类本身的实例)


    编辑...

    Category 可能是可能的(但我没有使用 SoapUI 的经验,所以我不知道这种技术是否适合)

    假设我们有一个类 Example 定义如下:

    class Example {
      String name
    }
    

    然后我们可以编写一个与您的示例代码非常相似的类,在这种情况下,它将设置闭包的委托,并且闭包将打印出委托的名称属性(因为我们已将 resolve strategy 设置为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: tim
    

    【讨论】:

      猜你喜欢
      • 2010-12-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-24
      • 2022-08-18
      • 2016-08-14
      • 2014-12-26
      相关资源
      最近更新 更多