Execute Java Code in Neoload using JS

Introduction:
Neoload is an emerging excellent Performance testing tool, which is available in the market. It provides exceptional capability to support most of the protocols. Some of the renowned protocols were Web http/html, Mobile Protocols, SAP GUI, Citrix and many more.
Background:
Though Neoload supports multi-protocols, there might be situation arise to use some internal Java library within the organization to execute some function/methods. Which would provide some feature required for sending data or may be it would be an RMI call.
Neoload does not have an JSR223 or Java element to write your Java/groovy code directly into it. All it supports is JavaScript element to write custom code.
JavaScript element is meant to write your own JS custom code required in the script but you might think how to write Java code especially using custom libraries or 3rd party libraries in Neoload ?
The Answer is simple, You can still use JavaScript element in Neoload to write custom java code or code which invokes function/methods using java libraries.
Demo Code:
For Demo purpose, I have created a sample java code which uses two integers as inputs and will perform simple mathematical operations (add/subtract/divide/multiply). I created it in a maven project to build it as a jar (library).
The project will have a java Class “App” with math operations as methods within it.

You can download the sample code from the GitHub and build the jar by your own. All you need is git and maven in your machine.
git clone <repo-url>
cd neoload-examples/demo-app;
mvn clean install
Referencing Java library in Neoload:
As a matter of fact, the created library should be referenced/mapped to the Neoload script. The best way to achieve it is by copying the library to Neoload directory.
- Create a script:
First create an empty script by providing a name and save it in a desired path. - Where to place the library ?
You need to navigate to your script directory which you have created. The script directory will have the below directories

Navigate to lib/jslib/ → Place the required libraries inside it and restart the Neoload. This restarting operation will make the Neoload to pickup the new libraries.
Importing Java library and using it in the JS element:
Now, The tool is capable of identifying the packages and its methods. The difficulty is Neoload doesn’t have Java/JSR223/Groovy element to write the java code within the script.
Wondering how to achieve Java code execution ? — Yes JavaScript element is capable of Importing libraries and can execute Java methods.
Follow the below steps to achieve Java execution in Neoload Script.
1. Create a JS Element:
First you need to drag and drop the JavaScript element from actions

2. Import the required Java Package:
To import the java package in JavaScript element — you need to follow certain procedures.
- The Package import should be declared to variable
- Assigning the package to a variable should start with new keyword
- The package/class name should be given with a prefix as “Package.”
- If the class is used in the package path, it should end with → ();
3. Using the Java Object:
The variable now acts as a instantiated object, where you can use it to perform all the operations as you like.
4. How to make the Java Object as singleton:
To make the Java object as singleton for the whole script — It is recommended to import the Package in the Init section. Once the package is imported to variable, It has to be exported to a Context to Virtual User. The exported variable can be accessed anywhere in the script such as multiple JavaScript elements and different sections.
Refer the sample code below for import, Making singleton and logging
//Importing package
var appObj = new Packages.io.perfwise.demo.App();
try {
context.currentVU.put(“demoObj”, appObj); // Exporting variable to context
var result = appObj.add(5,5); // Accessing the method inside app class
logger.info(“THE OUTPUT IS :::: “ + result); //Logging the output
} catch (exception) {
logger.debug(“Exception occurred ::: \n “ + exception);
}
5. How to access the exported java object from context variable:
Here, We are not importing and instantiating the object again. Instead, we are using the existing object. Refer the below code:
//Accessing the exported Java object from the previous/Init section
var appObj = context.currentVU.get(“demoObj”);try {
var result = appObj.subtract(5,5); // Accessing the method inside app class
logger.info(“THE OUTPUT IS :::: “ + result); //Logging the output
} catch (exception) {
logger.debug(“Exception occurred ::: \n “ + exception);
}
Conclusion:
You can have many JavaScript elements inside the actions part to perform different functions as per you requirements. Hope this helps to understand how a java library can be invoked in Neoload.
Will come up with another unique article. Keep supporting, Click on follow button and hit the subscribe to get the notification on new article publish.
Stay positive, Happy engineering !