Service Virtualization

Expand all | Collapse all

Access Java objects from Beanshell throughout script

Jump to Best Answer
  • 1.  Access Java objects from Beanshell throughout script

    Posted 03-04-2017 01:01 PM

    In Application Test, is it possible to access 3rd party objects throughout the script?

     

    Context:

    I'm using a 3rd party Java library, and have been creating new objects within individual scripted assertion steps in Beanshell. But, I want to limit the duplicate code and create one connection object and reuse it through my assertion steps. I've tried creating from Dynamic Java steps and from Beanshell scripts. I'm looking for (easy) ways to make my objects globally scoped so I can access them from any Beanshell scripted assertion from my script. It seems like scope is at the script/assertion step level. 

     

    Thanks,

    -Jason



  • 2.  Re: Access Java objects from Beanshell throughout script

    Broadcom Employee
    Posted 03-08-2017 10:59 AM

    What version of DevTest are you on?



  • 3.  Re: Access Java objects from Beanshell throughout script

    Posted 03-08-2017 01:26 PM

    8.2



  • 4.  Re: Access Java objects from Beanshell throughout script
    Best Answer

    Posted 03-08-2017 03:16 PM

    No guarantees, but you might try saving the object in testExec and recalling it when you need it.....  

    NOTE: I don't know what your objects are doing nor what they might be composed of so I cannot comment on the potential memory hit.

     

    For example - on first use, save it off:

    import com.my.object.myConnObject;

    myConnObject mo = new myConnObject();

    myConnObject.setSomething("something");

    //other work

    testExec.setStateObject( "prop_myConnObject", mo );

    return;

     

    Later when another step needs to use it...

    import com.my.object.myConnObject;

    myConnObject mo = (myConnObject)testExec.getStateObject( "prop_myConnObject" );

    if ( mo == null ) {

       // print in ITR Events, Workstation.log or one of the server logs 

       // where this step is running (i.e., vse.log, simulator.log, etc.)

       _logger.info( " <<<< Uh Oh! My Connection object is not available, something is wrong!" );

     

       // property does not exist, so instantiate it or take appropriate error action

    }

    // other work, then on the way out to keep it fresh

    testExec.setStateObject( "prop_myConnObject", mo );

    return;

     

    There may be other techniques if you are using Groovy.



  • 5.  Re: Access Java objects from Beanshell throughout script

    Broadcom Employee
    Posted 03-08-2017 03:24 PM

    I agree with Joel. Storing object in your beanshell script step using testExec should help you access those in the assertion on the step. Please let us know if that worked.



  • 6.  Re: Access Java objects from Beanshell throughout script

    Posted 03-08-2017 03:43 PM

    That looks like it did the trick, thanks Joel! This will hugely improve connection management, big win.