如何将上下文从脚本传递到另一个 Class groovy
How to pass context from Script to another Class groovy
下面是我的 Groovy 脚本,它实例化了我的 classes。它是更大 Groovy 脚本的一部分,由许多 class 组成,在 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' class 中,(如果我愿意的话),我可以参考 'context' - 来定义 GroovyUtils。如何将 'context' 传递给另一个 class 模型,以便在模型中使用 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"
}}
以上代码在尝试引用上下文时会导致错误,尽管它与 'Run' class 在相同的 Groovy 测试步骤中。任何帮助将不胜感激。
我不确定我是否正确理解了你模型的所有部分,但是正如@tim_yates 在他的评论中建议的那样,为什么你不简单地将 groovyUtils
传递给你的 Model
class。您可以修改 Model
class 添加 groovyUtils
变量:
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"
}
}
然后在 run()
方法中使用 groovy 默认映射构造函数将 groovyUtils
传递给 Model
class:
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
...
}}
希望对您有所帮助,
下面是我的 Groovy 脚本,它实例化了我的 classes。它是更大 Groovy 脚本的一部分,由许多 class 组成,在 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' class 中,(如果我愿意的话),我可以参考 'context' - 来定义 GroovyUtils。如何将 'context' 传递给另一个 class 模型,以便在模型中使用 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"
}}
以上代码在尝试引用上下文时会导致错误,尽管它与 'Run' class 在相同的 Groovy 测试步骤中。任何帮助将不胜感激。
我不确定我是否正确理解了你模型的所有部分,但是正如@tim_yates 在他的评论中建议的那样,为什么你不简单地将 groovyUtils
传递给你的 Model
class。您可以修改 Model
class 添加 groovyUtils
变量:
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"
}
}
然后在 run()
方法中使用 groovy 默认映射构造函数将 groovyUtils
传递给 Model
class:
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
...
}}
希望对您有所帮助,