Virtual Service Response Scripting

Hi, I have wrote a Java code to generate a response, when tested in the Java Perspective in Parasoft tool code is executed without any errors got a valid output. When i loaded the same to the "Virtual Service" ---> Response section and tested in local Server getting below error. Could someone correct the code. Attached the code that i used. Thanks in Advance.

Error Message: com.parasoft.api.UserMethodException: argument type mismatch


  • williammccuskerwilliammccusker Posts: 453 ✭✭✭


    The issue is that the scripted mode for the "Response" section expects a method that takes either zero arguments, or a single argument of the type com.parasoft.api.ScriptingContext

    From your code the easier option would be to change to have a public method that takes zero arguments.

    For more information on scripting take a look at this documentations

  • benken_parasoftbenken_parasoft Posts: 935 ✭✭✭
  • Thanks William for the response, As you said i have made changes to the code and it worked. Any Idea how to add the .class file of the Java code to the parasoft remote server. As i see when deployed the .class file is not getting added to the server.

    Thanks benken for your response as well.

  • AvinashPanditiAvinashPanditi Posts: 32
    edited January 2020

    HI William, I have added below code to extract the URL parameters but getting below error when tested the Virtual Service. If i remove the code and tested the Virtual Service its working fine. Attached the .java file for reference.

    Code: -
    Object x = context.get(CorrelationScriptingHookConstants.URL_PARAMS);
    String y = (String) ((Context) x).get("last_name");

    Exception in thread "main" java.lang.NullPointerException
    at CreateXMLScriptingContext.CreateXMLScriptingContextNew(
    at CreateXMLScriptingContext.main(

  • williammccuskerwilliammccusker Posts: 453 ✭✭✭

    Is this script meant to be used in the "Responder Correlation" tab? It looks like from the txt file the error is saying the context is null? I also noticed that you are casting object x as a context when it should instead be a map. The script worked when I used it after fixing the class cast.

  • Thanks will, i will try this on Monday and will let you know the results.

  • Hi William, I am executing this code in "Response" tab. Scenario is to read the URL parameters and then form the response XML. Is there a way we can communicate directly via call or chat or any other media to meet and solve this issue.

    We fixed the casting but the Code is failing when tested the Virtual Service. Could you please attach the code that you have fixed.

  • williammccuskerwilliammccusker Posts: 453 ✭✭✭

    I think the issue is that in the "Response" tab the context may not have been populated with the correlation values that hook is meant for. I am attaching an alternative example where I use a REST URL Data bank to get the URL parameter then from the script I access the data banked parameter. I used groovy because it is easier to deploy and test, it is very close to java. I copied that main part of your method to create the xml document in as is.

  • Hi William, Thanks for sharing the code, now i am able understand how to script in Groovy. Thanks for all your help. :):)

  • williammccuskerwilliammccusker Posts: 453 ✭✭✭

    Happy to help!

  • Hi William, I have wrote a script similar to what we have discussed to generate a argument value in the JSON structure. I have went to Response --> Input Mode ---> Form JSON-->argument1---->Value is selected to "Script" and added the Groovy script here. When i press the evaluate button and i dont see any errros. The script is returning a value when tested in Java Perspective.

    When tested the Virtual Service from local machine i dont see the argument is populated with the value. Could you please share me a simple script with the following scenario.

    1. Script should get an URLParam value from the REST URL Data bank.
    2. Based on the URLParam value i am returning a new string value.

    It will be really great if you could share an example like you shared in our above conversation.

  • williammccuskerwilliammccusker Posts: 453 ✭✭✭

    It should work exactly the same as in the response tab. Are you trying to just return a simple string value that would be the text value of a json property? Or are you trying to replace the entire property with some json? Could you share more about what value you are trying to return?

  • HI William, I found out the issue and corrected that. Thanks.

  • williammccuskerwilliammccusker Posts: 453 ✭✭✭

    Glad to hear you were able to get it to work!

Sign In or Register to comment.