How to resolve groovy script failure: Cannot invoke method getProjectByName() on null object
Encountering groovy script failures when trying to run soapUI tests that contain groovy scripts when executing from Launch Test runner. I am able to run just the groovy script with no issues but when running from launch test runner it fails with this error: Let me know how to get groovy script to run successfully from launch test runner . DO i need to import any class ?
Error:java.lang.NullPointerException: Cannot invoke method getProjectByName() on null object
----------------- Messages ------------------------------
java.lang.NullPointerException: Cannot invoke method getProjectByName() on null object
error at line: 22
Here below is sample groovy script example
import groovy.json.JsonSlurper
import groovy.xml.XmlUtil
import groovy.sql.Sql
// Get Project level attributes
def dbServer = context.expand( '${#Project#DatabaseServer}' )
def dbport = context.expand( '${#Project#port}' )
def dbInstance = context.expand( '${#Project#DatabaseInstance}' )
def dbname = context.expand( '${#Project#DatabaseName}' )
def userID = context.expand( '${#Project#UserID}' )
sql = Sql.newInstance("jdbc:sqlserver://$dbServer:$dbport;instanceName=$dbInstance;databaseName=$dbname;integratedSecurity=true")
//Exesute APIcall using testrunner varibale
def prj = testRunner.testCase.testSuite.project.workspace.getProjectByName("UserRights")
tCase = prj.testSuites['REC-UserRights'].testCases['REC-Userrights Testcase']
tStep = tCase.getTestStepByName("Validate RECUserrights")
def runner = tStep.run(testRunner, context)
log.info ("runner status ....... : " + runner.hasResponse())
// Using Json Slurper class method to read API response values
def Response = context.expand( '${Validate RECUserrights#Response#$data.user_rights}' )//creating instance of slurperclass
def slurperResponse = new JsonSlurper().parseText(Response)
log.info("RECAPI Response results:" + slurperResponse)
//Get rights tied to the user from DB
def dbproperty = sql.rows("""
Select right_id
from XYZ
where ($userID IS NULL OR user_id in ($userID))
order by right_id """)
log.info("DB results:" + dbproperty.right_id)
//Data compare
if (slurperResponse.toString() == dbproperty.right_id.toString())
log.info ("user rights in DBvalue match API response")
else
log.info ("user rights in DBValue do not match API response")