如何从脚本传递上下文到另一个类groovy [英] How to pass context from Script to another Class groovy
问题描述
下面是我的Groovy脚本,它实例化了我的类。它是一个更大的Groovy脚本的一部分,由许多类组成,这些类作为SoapUI测试套件中的测试用例。
<$ c $ b $ public static(){$ b $>公共类运行扩展脚本{
公共静态无效的主要(字符串[] args){
运行mainRun =新的运行()
} b $ b def groovyUtils = new com.eviware.soapui.support.GroovyUtils(context); //不会错误
Model myModel = new Model()
查看myView = new View()
控制器myController = new Controller()
myModel.addObserver(myView)
myController.addModel(myModel)
myController.addView(myView)
myController.initModel(Init Message)
myView.addController(myController)
}}
在上面的'Run'类中(如果我想的话),我可以引用'context ' - 为了定义GroovyUtils。如何将'context'传递给另一个类Model,以便在Model中使用GroovyUtils?即:
类模型extends java.util.Observable {
public String doSomething(){
def groovyUtils = new com.eviware.soapui.support.GroovyUtils(context); //错误发生在这里
返回需要groovyUtils的东西
}}
上述代码在尝试引用上下文时会导致错误,尽管它与Run类处于相同的Groovy测试步骤内。任何帮助将不胜感激。
我不确定我是否正确理解模型的所有部分,但是@tim_yates在他的评论中建议你为什么不简单地将 groovyUtils
传递给你的 Model
类。您可以修改您的 Model
类,并添加一个 groovyUtils
变量:
class Model扩展java.util.Observable {
com.eviware.soapui.support.GroovyUtils groovyUtils
public String doSomething( ){
println this.groovyUtils //这里你有groovy utils
return需要groovyUtils的东西
}
}
然后在 run()
方法中传递 groovyUtils
到
Model
类,使用groovy默认映射构造函数:
<$ c $ b $ public static(){$ b $>公共类运行扩展脚本{
公共静态无效的主要(字符串[] args){
运行mainRun =新的运行()
} b $ b def groovyUtils = new com.eviware.soapui.support.GroovyUtils(context); //不会错误
Model myModel = new Model('groovyUtils':groovyUtils)//将groovyUtils传递给您的Model
断言需要groovyUtils的东西== myModel.doSomething()//仅限检查groovyUtils是否传递给您的模型类
断言myModel.groovyUtils == groovyUtils
...
}}
希望它有帮助,
Below is my Groovy Script which instantiates my classes. It is a part of a much larger Groovy Script, consisting of many classes, which sits as a Test Case within a Test Suite in 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)
}}
Within the above 'Run' class, (if I wanted to), I am able to refer to the 'context' - in order to define GroovyUtils. How do I pass the 'context' to another class, the Model, in order to use GroovyUtils in the Model? I.e:
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"
}}
The above code would cause an error when trying to refer to the context, despite it being within the same Groovy Test Step as the 'Run' class. Any help would be greatly appreciated.
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,
这篇关于如何从脚本传递上下文到另一个类groovy的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!