Forum Discussion

harmans's avatar
harmans
Occasional Contributor
13 years ago

database connection in the Setup script

Hi,

I am trying to create database connection one time in the Setup script (under TestSuite) instead of creating a new connection in all of the assertions / testcases.

So below is the groovy script in the Setup (at TestSuite Level)

import groovy.sql.Sql
com.eviware.soapui.support.GroovyUtils.registerJdbcDriver("com.microsoft.sqlserver.jdbc.SQLServerDriver")
def db = [url:'jdbc:sqlserver://127.0.0.1:1433;databaseName=Test123',user:'user',password:'password',driver:'com.microsoft.sqlserver.jdbc.SQLServerDriver']
try{
def connection = Sql.newInstance(db.url, db.user, db.password, db.driver)
context.setProperty("dbconn",connection)
log.info "connected to the database"

}catch(Exception e){
log.error "Could not connect to the database"
}

And the script in my assertion is. But the below code does not work. How do i retrieve context property dbconn in the assertion ?

if(context.dbconn){
def sql = context.dbconn
sql.eachRow("select ua.[contact_name], ua.[contact_last_name] from [dbo].[USER_ACCOUNT] where [user_id] = 'abc' ")
{row ->
log.info("$row.contact_name")
}
}

3 Replies

  • nmrao's avatar
    nmrao
    Community Hero
    Understand that you wanted to reuse the connection which is good, do not have direct anwer though now.
    But you were setting into a property, and i believe that a property can simple store key, value pairs, but not the objects.
  • harmans's avatar
    harmans
    Occasional Contributor
    The syntax for setProperty is setProperty(String name, Object value), so i can store an Object.
    Please refer http://www.soapui.org/apidocs/com/eviwa ... .Object%29


    If i put complete code in setup script, it works ( put the complete code below in setup script and it will print the value of contact_name ). Its just that the i cannot retrieve the context of the testsuite (setup script at testsuite level) in the Groovy Teststep.


    import groovy.sql.Sql
    com.eviware.soapui.support.GroovyUtils.registerJdbcDriver("com.microsoft.sqlserver.jdbc.SQLServerDriver")
    def db = [url:'jdbc:sqlserver://127.0.0.1:1433;databaseName=Test123',user:'user',password:'password',driver:'com.microsoft.sqlserver.jdbc.SQLServerDriver']
    try{
    def connection = Sql.newInstance(db.url, db.user, db.password, db.driver)
    context.setProperty("dbconn",connection)
    log.info "connected to the database"
    }catch(Exception e){
    log.error "Could not connect to the database"
    }

    if(context.dbconn){
    def sql = context.dbconn
    sql.eachRow("select ua.[contact_name], ua.[contact_last_name] from [dbo].[USER_ACCOUNT] where [user_id] = 'abc' ")
    {row ->
    log.info("$row.contact_name")
    }
    }
  • nmrao's avatar
    nmrao
    Community Hero
    Apologies for my poor understanding for setProperty --> K, V. At least, haven't used it that way. And thank you for point API link.

    Still looking for a way to do it?