Submit and vote on feature ideas.

Welcome to the new Parasoft forums! We hope you will enjoy the site and try out some of the new features, like sharing an idea you may have for one of our products or following a category.

Need assistance accessing a Java Class

TScheafer
TScheafer Posts: 15

I could use some guidance on how to access a Java method within SOATest. I have built a pretty basic class to get LocalDate, add days, subtract days and return the Epoch date.

When I select "script" for the element I select Java and put in the full path to the class. The methods appear but I can not figure out how I can pass a value into my add days method.

Comments

  • benken_parasoft
    benken_parasoft Posts: 1,143 ✭✭✭
    edited February 9

    I have built a pretty basic class to get LocalDate, add days, subtract days and return the Epoch date.

    As alternatives, just in case someone finds this helpful:

    There is a new "sv:date-math" XPath function that can also be referenced in most tool configuration fields as an inline variable expression like ${{=sv:date-math('1y', 'MM/yyyy', '2023-02-09', 'yyyy-MM-dd')}}. This was introduced to help avoid scripting such things. There is some detail about it here.

    The Data Generator tool can also be used to generate date/time values. There is detail about it here.

    When I select "script" for the element I select Java and put in the full path to the class. The methods appear but I can not figure out how I can pass a value into my add days method.

    You must select a java method in your class that matches a particular signature. Depending on the context, the method takes 0, 1, or 2 arguments and each argument must be of a particular type. To the right of the Method box, you will see an "Expected number of arguments" link label. Clicking this label will open a dialog which describes what arguments are expected for the Java method. One of the arguments is typically of type com.parasoft.api.ScriptingContext which provides access to test suite variables, environment variables, data source values, and data bank values.

    To call an arbitrary Java method then I would recommend using Groovy for the Language. Then you can write a small method in Groovy to call your Java method however you want:

    import com.example.MyClass
    
    void doSomething() {
        MyClass.myMethod("abc", 1, 2, 3)
    }
    

    There are also various scripting examples in Extensibility and Scripting Basics.

  • TScheafer
    TScheafer Posts: 15

    Benken, thanks for the tidbits. I have decided to go with the Data Generator option since I am the only one on my team that really understands Java and I think it will be the easiest option for future maitenance.