mardi 2 juin 2015

How to invoke method with sql request in separate class from Groovy script in SOAP UI?

Friends, hello. I'am trying to use elements of object oriented approach in SOAP UI groovy scripting. I read manual about "How to write a reusable script" here http://ift.tt/1FrzPVh and I am trying to write a separate class with method which get data from database and set up address properties (Address). My problem is that i receive the error message

groovy.lang.MissingPropertyException: No such property: sql1 for class Utils error at line: 138

when run "main" Groovy script.

"Main" Groovy Script looks like:

// get a reference to the library TestSuite
library = testRunner.testCase.testSuite.project.testSuites["Library"]
// find the module within the library 
module = library.testCases["module-name"].testSteps["Utils"]
// initialise the library; which places an instance of Example in the context
module.run(testRunner, context)
// get the instance of example from the context.
def utils = context.utils
// run the method, with parameter
log.info "utils.setAddress() = " + utils.setAddress("LivingPlace");

setAddress method is determined in class Utils which is looked like:

    import groovy.sql.Sql;
    class Utils
    {
       def log
       def context
       def testRunner
       // Class constructor with same case as Class name
       def Utils(logIn, contextIn, testRunnerIn)
       {
         this.log = logIn
          this.context = contextIn
          this.testRunner = testRunnerIn
       }

    def  setAddress (addressType)
    {
         log.info testRunner;
         log.info context;
         log.info "Call the DB" ;

com.eviware.soapui.support.GroovyUtils.registerJdbcDriver("com.microsoft.sqlserver.jdbc.SQLServerDriver");
sql1 = Sql.newInstance("jdbc:sqlserver://SRV1:1433; databaseName=DB;", "user", "123", "com.microsoft.sqlserver.jdbc.SQLServerDriver");


sql1.eachRow('select top 1 '+     
                  'Country '+    
       'from Address') 
    { row ->
        testRunner.testCase.testSteps['Address'].setPropertyValue("'country",  "$row.Country");

     }
      return addressType //just for test
    }
    }


context.setProperty ("utils", new Utils(log, context, testRunner) )
        log.info "Library Context:"+context;

If i delete everything related with sql it works fine. How to invoke the setAddress method without error described earlier and set Adress properties?

Aucun commentaire:

Enregistrer un commentaire