Forum Discussion

nkpalli's avatar
nkpalli
Contributor
7 years ago

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")

 

 

 

 

 

 

3 Replies

  • 05ten's avatar
    05ten
    Contributor
    Not sure if you have access to the workspace as such in the testrunner.

    I might be wrong but I've always run the test runner with an explicit project in mind. But the error you get tells me that workspace is null.

    Right here:
    testRunner.testCase.testSuite.project.workspace.getProjectByName("UserRights")

    Consider why you need to separate a run into several different projects. It feels a little awkward tbh. Rather out that functionality in another (perhaps disabled) testsuite.
  • nmrao's avatar
    nmrao
    Champion Level 3
    "workspace" is only available with in soapUI tool, but not to test runner.

    Using workspace is not good approach. Reason being, the automated projects utlimately going to run on different machines such as CI etc where there won't be any such object.

    I believe that you need to use SoapUI's API to create WsdlProject for other soapui project and call it from there.

    Please find some API sample in the below link to see you could get around to workwith other projects.
    https://www.soapui.org/test-automation/junit/junit-integration.html
    • nkpalli's avatar
      nkpalli
      Contributor

      I tried looking at the link attached and looks like this is more of  Junit integration link?  :https://www.soapui.org/test-automation/junit/junit-integration.html

       

      I am using SOAPUI free version and i am running the tests locally. Like you have mentioned earlier since workspace is available from SOAP API groovy script runs successful however when run from launch Test runner it fails . How do  wemake this script run from launchTestRunner   without changing our existing script ?