如何将脚本从脚本传递到另一个类groovy

时间:2015-10-30 13:40:48

标签: groovy soapui

下面是我的Groovy脚本,它实例化了我的类。它是一个更大的Groovy脚本的一部分,由许多类组成,它们作为测试用例位于SoapUI的测试套件中:

public class Run extends Script {
public static void main (String[] args){
    Run mainRun = new Run()
}
public run(){
    def groovyUtils = new com.eviware.soapui.support.GroovyUtils( context ); // Would not error
    Model myModel = new Model()
    View myView = new View()
    Controller myController = new Controller()
    myModel.addObserver (myView)
    myController.addModel(myModel)
    myController.addView(myView)
    myController.initModel("Init Message")
    myView.addController(myController)
}}

在上面的' Run'上课,(如果我想),我可以参考上下文' - 为了定义GroovyUtils。如何通过'上下文'到另一个类,模型,以便在模型中使用GroovyUtils?即:

class Model extends java.util.Observable {
public String doSomething(){
    def groovyUtils = new com.eviware.soapui.support.GroovyUtils( context );// Error occurs here
    return "Stuff that needs groovyUtils"
}}

上述代码在尝试引用上下文时会导致错误,尽管它与“运行”中的Groovy测试步骤相同。类。任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:2)

I'm not sure if I understand correctly all the pieces of your model, however as @tim_yates suggest in his comment why you don't simply pass the groovyUtils to your Model class. You can modify your Model class adding a groovyUtils variable:

class Model extends java.util.Observable {

    com.eviware.soapui.support.GroovyUtils groovyUtils

    public String doSomething(){
            println this.groovyUtils// here you've groovy utils
            return "Stuff that needs groovyUtils"
    }   
}

And then in the run() method pass the groovyUtils to the Model class using the groovy default map constructor:

public class Run extends Script {
public static void main (String[] args){
    Run mainRun = new Run()
}
public run(){
    def groovyUtils = new com.eviware.soapui.support.GroovyUtils( context ); // Would not error
    Model myModel = new Model('groovyUtils':groovyUtils) // pass the groovyUtils to your Model
    assert "Stuff that needs groovyUtils" == myModel.doSomething() // only to check the groovyUtils is passed to your model class
    assert myModel.groovyUtils == groovyUtils
    ...
}}

Hope it helps,