Forum Discussion

jbiss's avatar
jbiss
Occasional Contributor
10 years ago

Having trouble defining context

Hi all, I am trying to use the context within a second class in my Groovy script. However, it keeps failing with the following message:

 

'groovy.lang.MissingPropertyException: No such property: context for class: Model'

 

My simplified code looks like this, I have used the same line to define GroovyUtils, which is now causing errors many times before :

I am aware I may have some fundamental misunderstanding.

 

class Controller implements java.awt.event.ActionListener {
	Model model;
	public void actionPerformed (java.awt.event.ActionEvent e) {
		model.doSomething()
	}
}

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

 

 

 

  • kondasamy's avatar
    kondasamy
    Regular Contributor

    Hi,

     

    We need more information on the question you posted. Could you please answer the below questions, so we will try reach a better solution,

    1) Where are you running this code? (in Groovy Test step/ External plugin development environment/ Junit/ Script Library  etc.,)?

    2) Please define the actual purpose of your requirement!

     

    Thanks,

    Samy

  • nmrao's avatar
    nmrao
    Champion Level 3

    Currently, your method of Second class does not have any parameters.

     

    So, it will be resolved if you have the context as parameter and when you call the method, pass the context as argument.

     

    class Model extends java.util.Observable {
    	public String doSomething(context){
    		def groovyUtils = new com.eviware.soapui.support.GroovyUtils( context )
    		//do stuff here
    	}
    }

     Call the above method in the script as:

     

    def model = new Model()
    model.doSomething(context)

     

    Note that, context is available in script only, but not in the class that you define. Hence, you need to pass it where it is needed.

    • jbiss's avatar
      jbiss
      Occasional Contributor

      I understand what you are saying - that the context needs to be passed as a parameter. However, my Groovy Test Step is quite large (~500 lines of code). Below is the actual 'Script', which is fairly small, but instantiates my classes to provide you some context (no pun intented):

       

      public class Run extends Script {
      	public static void main (String[] args){
      		Run mainRun = new Run()
      	}
      	public run(){
      		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)
      	}
      }

      Essentially, are you saying that the 'context' variable - which is only available here - needs to be passed throughout all the other methods and classes to make it available to the Model (which is the only part of the code needing it)?

       

      As you can probably tell - I am relativley new to Java/Groovy and OO type programming.

       

      Edit: Samy, this is a Groovy Test Step. Held within a Test Case alongside various XML Requests. This script provides a basic GUI and enables manipulation of the XML messages and automation of the testing.