Forum Discussion

harmans's avatar
harmans
Occasional Contributor
11 years ago

Database connection in 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

  • SmartBear_Suppo's avatar
    SmartBear_Suppo
    SmartBear Alumni (Retired)
    Hi,

    We usually don't provide support for groovy scripting, but a quick glance reveals the problem. The way properties in SoapUI work is that they are all strings, so you aren't able to save a database connection there.

    What you can do is set up a JDBC connection at the project level (Look for the "JDBC connections" tab in the Project view" and use that in your tests.

    Regards,
    Arian
    SmartBear Sweden
  • harmans's avatar
    harmans
    Occasional Contributor
    Hi,

    My question is SOAPUI specific and not about groovy. I can configure database under JDBC Connections but i will still need to create a separate instance for each test case (i want to create database connection only one time in the setup script). Please correct me if i am wrong.

    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 (in the 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")
    }
    }
  • SmartBear_Suppo's avatar
    SmartBear_Suppo
    SmartBear Alumni (Retired)
    Hi,
    We still suggest you set up a JDBC connection at the project level (Look for the "JDBC connections" tab in the Project view" ). Please refer this link http://www.soapui.org/Working-with-Proj ... tions.html
    It is no need to create a separate instance for each test case.Connections configured on a project level can later be used in JDBC DataSource, JDBC DataSink and JDBCRequest TestSteps.

    Regards,
    Li
    SmartBear Sweden