I am receiving this null pointer exception and not sure how to resolve/debug it. The script contains a class with two methods. Code is doing what it is supposed to do. Any pointers on geeting started to resolve this?
java.lang.NullPointerException
at com.eviware.soapui.impl.wsdl.teststeps.WsdlGroovyScriptTestStep.run(WsdlGroovyScriptTestStep.java:154)
at com.eviware.soapui.model.testsuite.TestStep$run.call(Unknown Source)
at Script48.run(Script48.groovy:22)
at com.eviware.soapui.support.scripting.groovy.SoapUIGroovyScriptEngine.run(SoapUIGroovyScriptEngine.java:92)
at com.eviware.soapui.impl.wsdl.teststeps.assertions.basic.GroovyScriptAssertion.assertScript(GroovyScriptAssertion.java:116)
at com.eviware.soapui.impl.wsdl.teststeps.assertions.basic.GroovyScriptAssertion.internalAssertResponse(GroovyScriptAssertion.java:128)
at com.eviware.soapui.impl.wsdl.teststeps.WsdlMessageAssertion.assertResponse(WsdlMessageAssertion.java:150)
at com.eviware.soapui.impl.wsdl.teststeps.WsdlTestRequest.assertResponse(WsdlTestRequest.java:176)
at com.eviware.soapui.impl.wsdl.teststeps.WsdlTestRequestStep.propertyChange(WsdlTestRequestStep.java:339)
at com.eviware.soapui.impl.wsdl.support.assertions.AssertionsSupport.propertyChange(AssertionsSupport.java:79)
at java.beans.PropertyChangeSupport.fire(Unknown Source
Script looks like this.
import com.eviware.soapui.impl.wsdl.testcase.WsdlTestCaseRunner
import com.eviware.soapui.support.types.StringToObjectMap
import com.eviware.soapui.impl.wsdl.testcase.WsdlTestRunContext
context.setProperty("searchChange", new searchChange())
class searchChange{
def search(a,b,TCRunner){
def search_TestCase = TCRunner.testCase.testSuite.getTestCaseByName("TestCaseName")
search_TestCase.setPropertyValue("a", a)
search_TestCase.setPropertyValue("b", b)
search_TestCase.run(new com.eviware.soapui.support.types.StringToObjectMap(), false)
}
def runner(tCase,mExchange){
new WsdlTestCaseRunner( tCase, new StringToObjectMap() );
}
}
When using the above class,the line(groovy:22) in code that throws exception is
scripts.testCases["Library"].testSteps["Lib"].run(context.getTestRunner(),context)
Related
I am trying to get my head around groovy scripting to make some changes to a jenkins pipeline and I keep getting this error:
groovy.lang.MissingPropertyException: No such property: credentials for class:
I have tried declaring the variable with def but I still get the exception, eclipse does not recognise that the property exists.
What am I doing wrong?!
#!/usr/bin/groovy
package common.pipeline
import common.pipeline.Credentials
Credentials credentials = new Credentials()
def withCredentials(steps) {
credentials.productionPipeline(steps)
}
This script will be compiled by groovy into a Script class with the field definition inside the run method, and with another method withCredentials that is trying to access the field (kinda like this):
import common.pipeline.Credentials
class Script1 extends Script {
def withCredentials(steps) {
credentials.productionPipeline(steps)
}
def run(args) {
Credentials credentials = new Credentials()
}
}
As you can see, this won't work, as the credentials aren't at Field level in the class...
Groovy has an annotation to make this happen:
#!/usr/bin/groovy
package common.pipeline
import common.pipeline.Credentials
import groovy.transform.Field
#Field Credentials credentials = new Credentials()
def withCredentials(steps) {
credentials.productionPipeline(steps)
}
Groovy RestClient throw the following Error when Getting the List of Object
this is the code
List<Code> codeList = restClient.get(path:"codes",headers: [Accept: 'application/json'])
Exception in thread "main" org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object 'groovyx.net.http.HttpResponseDecorator#7526515b' with class 'groovyx.net.http.HttpResponseDecorator' to class 'java.util.List'
at org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation.castToType(DefaultTypeTransformation.java:360)
at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.castToType(ScriptBytecodeAdapter.java:599)
After changing the code like below ,its working fine
def codeList = restClient.get(path:"codes",headers: [Accept: 'application/json'])
List<Code> codes = codeList.data
Using Groovy Binding to execute scripts from a main controller and attempting to pass a custom object, I get the error mentioned in the title.
Caught: org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object 'custompackage.CustomClass#60099951' with class 'custompackage.CustomClass' to class 'custompackage.CustomClass'
Here's the relevant code:
// Controller.groovy
import custompackage.CustomClass
CustomClass test = new CustomClass()
def binding = new Binding()
def engine = new GroovyScriptEngine('./src')
binding.setProperty("test", test)
engine.run("CustomScript.groovy", binding)
The file being run with above:
// CustomScript.groovy
import custompackage.CustomClass
CustomClass t
if(!binding.variables.containsKey("test")){
t = new CustomClass()
} else {
t = test
}
I'm defining the CustomClass t at the beginning for the purpose of autocomplete in my IDE. When running as def t it works fine.
I know the object is being passed correctly, due to the exception (and further printing of the object)
The error occurs on t = test
Why is Groovy trying to cast an object of the same type to it's type, and then failing to do so? And is there a fix that will still allow me to keep the statically typed t?
Thanks!
It seems custompackage.CustomClass in Controller.groovy is not the same as in CustomScript.groovy.
I checked the class instances in CustomScript.groovy with the debugger and found something interesting:
def a = CustomClass.class // Debugger: a={Class#1499} "class custompackage.CustomClass"
def b = test.class // Debugger: b={Class#1187} "class custompackage.CustomClass"
While when using GroovyShell instead of GroovyScriptEngine in Controller.groovy I get:
def a = CustomClass.class // Debugger: a={Class#1185} "class custompackage.CustomClass"
def b = test.class // Debugger: b={Class#1185} "class custompackage.CustomClass"
and the assignment t = test works without error.
The Controller.groovy file using GroovyShell looks like this:
// Controller.groovy
import custompackage.CustomClass
CustomClass test = new CustomClass()
def binding = new Binding()
def shell = new GroovyShell(binding)
binding.setProperty("test", test)
shell.evaluate(new File("CustomScript.groovy"))
I checked the documentation of GroovyScriptEngine and found a constructor which takes a ClassLoader as argument. Maybe that's the way to go but I don't know for sure.
In a soapui groovy script test step I've this.
context.setProperty("searchA", new searchA());
class searchA{
def testRunner
def searchA(testRunner){
this.testRunner=testRunner
}
def search(a,b){
def search_TestCase = testRunner.testCase.testSuite.getTestCaseByName("Search")
search_TestCase.setPropertyValue("ABC", a)
search_TestCase.setPropertyValue("DEF", b)
search_TestCase.run(new com.eviware.soapui.support.types.StringToObjectMap(), false)
}
}
and in an assertion script in a different test suite I am calling the above code like this.
scripts = messageExchange.modelItem.testStep.testCase.testSuite.project.testSuites["Test"]
scripts.testCases["Lib123"].testSteps["TestLib123"].run(context.getTestRunner(),context)
context.searchA.search("value1","value2")
but this gives me error "can not get property testCase on null object". whats wrong here?
I am not seeing null object error now. The issue was that testRunner is not available in script assertion so we need to create it like this in script assertion and then pass it in the caller method.
import com.eviware.soapui.impl.wsdl.testcase.WsdlTestCaseRunner
import com.eviware.soapui.support.types.StringToObjectMap
import com.eviware.soapui.impl.wsdl.testcase.WsdlTestRunContext
testCase = messageExchange.modelItem.testStep.testCase
tcRunner = new WsdlTestCaseRunner( testCase, new StringToObjectMap() );
context.searchA.search("value1","value2",tcRunner)
This thread helped me.
I want to play around with NekoHtml, in Groovy. I thought of adding it via Grape.
I tried this way :
#GrabResolver(root="http://net.sourceforge.nekohtml/nekohtml")
in my Groovy code.
But it is throwing an error like this :
org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
/home/anto/Groovy/webScrape/webFetch.groovy: 3: unexpected token: # line 3, column 1.
The NekoHtml can be found in Maven over here.
Edit:
Now I have a code like this :
#Grab('net.sourceforge.nekohtml:nekohtml:1.9.15')
import org.cyberneko.html.parsers.SAXParser
def url = 'http://java.sun.com'
def html = new XmlSlurper(new org.cyberneko.html.parsers.SAXParser()).parse(url)
def bolded = html.'**'.findAll{ it.name() == 'B' }
def out = bolded.A*.text().collect{ it.trim() }
out.removeAll([''])
out[2..5].each{ println it }
which throws the error like this :
Caught: java.lang.NoClassDefFoundError: org/apache/xerces/parsers/AbstractSAXParser
java.lang.NoClassDefFoundError: org/apache/xerces/parsers/AbstractSAXParser
Caused by: java.lang.ClassNotFoundException: org.apache.xerces.parsers.AbstractSAXParser
Couldn't able to figure out what this error states.
Thanks in advance.
Have you tried:
#Grab('net.sourceforge.nekohtml:nekohtml:1.9.15')
Then it should resolve from maven