Connecting to DB inside Test Steps for various processes is required most often in any Test Suite. So how do we get a connection of DB2 database (for that matter any database) using Groovy in SOAP UI ?
Its simple, basically we will get an sql instance first using parameters like JDBC connection URL, driver class name etc..
Say if we use the generic method getInstance(url, user, pwd, driverName) to get the instance and thence execute some SQL queries, we could proceed this way:
The pre-setup for this would be placing the required jars for DB connectivity in lib folder of SOAP UI and setting their paths to the CLASSPATH variable in bin/soapui.bat file. (In case of DB2 connection, we need to place db2jcc.jar and db2jcc_license_cu.jar jars inside lib directory and set their paths to CLASSPATH variable in soapui.bat file of bin directory). Then,
1) Import groovy.sql.Sql in the Groovy Step, by including
2) Then get the required arguments for Sql.getInstance() method, and fetch the sql instance by
def sql = Sql.newInstance(dbPath, dbUserName, dbPassword, dbDriverName);
Choice of newInstance() method could be as per the options developer has.
3) Use this sql object for executing the queries to perform operation on DB, for example
res = sql.execute( “SELECT * FROM TABLE1 WHERE COL1=’123′” );