Sergey Horban is a QA Engineer with 3+ years of experience in automation and manual testing, bug-tracking and test management.

Become a JMeter and Continuous Testing Pro

Start Learning

Test Your Website Performance NOW! |

arrowPlease enter a URL with http(s)
Jul 04 2018

How to Reuse Your JMeter Code with JAR Files and Save Time

When developing tests, it is often necessary to write code in the tests themselves. This code might be long, it might be repeated in several tests, or might be written only for a specific test. Also, the code written in the tests might have to change or be supplemented if the requirements change.


This can take a long time and is prone for errors. Suppose that we have 20 Apache JMeter™ tests in which the same code is written and at some point in time we decide to make changes to this code for all 20 tests. Changes must be made in each JMeter test. To get rid of such routine work, we can call codes to tests from a JAR file (JAR file is Java Archive).


The code that will be used in the tests that describes the test methods is written in the development environment (for example, IntelliJ IDEA) only once, a JAR file is created and only the call of the required code and the test request is performed in the tests themselves. We do not need to write the code directly in the test, and if for some reason we need to change the code, we change it only in one place.


The previous article “Sending HTTP and HTTPS Requests Using Groovy in JMeter” showed how to create methods for sending HTTP / HTTPS requests using Groovy. Based on these methods, this article will show how to create a JAR file and use such methods from a JAR file, in your tests.


In order to create such a JAR file, you must do the following:




1. Download and install Groovy and IntelliJ IDEA


2. Download the following JAR files:


The JAR files are required to create a method to send HTTP / HTTPS requests.


Setting Up Intellij IDEA


3. Run IntelliJ IDEA and create the project, as shown below.


Create New Project -> Groovy -> Next -> Set Project Name -> Set Project Location -> Finish


jmeter and jar files


setting intellij idea for jmeter jar files


reusing jmeter code with jar files


  • Project Name - any arbitrary name. I gave the name “Jmeter”.
  • Project Location - the directory where the project will be located


4. After clicking the "Finish" button, the created project will look like it is shown in the image below.


reusing jmeter groovy script


5. Add the downloaded JAR files to the project


File -> Project Structure


groovy code recycle jmeter


Modules -> Dependencies -> “+” -> JARs or directories


open source load testing with jmeter and intellij idea


6. From the opened list, select the JAR files that were previously downloaded and click "OK".


download jar files for jmeter


7. Click "OK" again.


jmeter jar files testing


8. Create a Groovy Class


src -> Right click -> New ->  Groovy Class - Set name -> Set Kind -> Click “Ok”


groovy in jar files, jmeter


load testing guide with groovy


9. Add the code you want to reuse, to the created class. In this case:


static List<String> sendRequest(String url, String method, Map<String,Object> body) {
   RequestConfig requestConfig = RequestConfig.custom()

   StringEntity entity = new StringEntity(new Gson().toJson(body), "UTF-8");

   HttpUriRequest request = RequestBuilder.create(method)
           .setHeader(HttpHeaders.CONTENT_TYPE, "application/json;charset=UTF-8")

   String req = "REQUEST:" + "\n" + request.getRequestLine() + "\n" + "Headers: " +
           request.getAllHeaders() + "\n" + EntityUtils.toString(entity) + "\n";

   HttpClientBuilder.create().build().withCloseable { httpClient ->

       httpClient.execute(request).withCloseable { response ->

           String res = "RESPONSE:" + "\n" + response.getStatusLine() + "\n" + "Headers: " +
                   response.getAllHeaders() + "\n" +
                   (response.getEntity() != null ? EntityUtils.toString(response.getEntity()) : "") + "\n";

           System.out.println(req + "\n" + res);

           return Arrays.asList(req, res);


groovy scripting in jmeter and intellij idea


The description of this code is given in “Sending HTTP and HTTPS Requests Using Groovy in JMeter”. The only difference is the "static" access modifier. Its description will be given later in the article. When you add your own code, make sure to add it as well.


10. Create another JAR file. This file will be added to JMeter.


File -> Project Structure


adding jar files to jmeter


Artifacts -> Click “+” -> JAR -> From module with dependencies -> OK


reuse test methods with groovy


jmeter.jar, intellij idea, groovy


11. Select and delete all files except Jmeter.jar, as shown in the image below. This name is generated according to the Project Name.


open source load testing jmeter groovy intelliJ idea


12. Add files from the "Available elements" section by double-clicking on each file, as shown in the image below. IntelliJ will show you the files. These are the JAR files from the beginning of the article.


adding jmeter files to intellij idea


13. After the addition, the files will be displayed, as shown in the image below. Click "OK".


building jar files for jmeter


14. Build -> Build Artifacts… -> Build


jmeter jar file build tutorial


jmeter groovy tutorial


After clicking "Build", a JAR file will be created in the directory ...\Jmeter\out\artifacts\Jmeter_jar, as shown in the image below.


jmeter guide to groovy


Setting Up JMeter


15. Adding the JAR file to JMeter


Copy the JAR file that was created to the JMeter directory ... \apache-jmeter-4.0\lib


copy jar files to jmeter directory


After the JAR file is created and added to JMeter, you can call the List <String> sendRequest (String url, String method, Map <String, Object> body) method in JMeter itself. This calls the code from the JAR file.


To do this, you need to do the following in JMeter:


16. Add a Thread Group


Right Click -> Add -> Threads(Users) -> Thread Group


guides to apache groovy


17. Add a JSR223 Sampler


Thread Group -> Right Click -> Add -> Sampler -> JSR223 Sampler


JSR223 Sampler -> Language Groovy


groovy language jmeter


18. In the JSR223 Sampler, add the following code example.


Map<String,Object> map = new LinkedHashMap<>();
map.put("Param_1", "Value_1");
map.put("Param_2", Arrays.asList(1,2,3,4));
map.put("Param_3", Arrays.asList("a","b","c"));

List test1 = Requests.sendRequest("","POST", map);;


groovy scripting in jmeter by building jar files


Update the test parameters according to your scenario. In this case, "","POST", map.


When calling the sendRequest () method, we do not need to import the Requests class (for example, import Requests;) to access the class and there is no need to create a class object (for example, Requests requests = new Requests ()) to access the method.


We only need to specify the name of the class (in our case, Requests) and specify the name of the method through the point with the corresponding parameter values. This form of method invocation became available because we created a static method in IntelliJ IDEA. A static method is a method before which the access modifier is "static"


19. After running the code for execution, we get the following result in the JMeter console:


code execution with jmeter


The image above shows the request being sent and the response received.


The cmd will also display the sent request and the received response, as shown in the image below.


sending requests with groovy in jmeter


That’s it! Now you don’t need to change your entire code every time you update a test. If you do change your test methods, make sure you update the correct JAR file in JMeter.


Running Your JMeter Tests in BlazeMeter


After creating your JMeter script, upload it to BlazeMeter and run it. You will be able to massively scale, share your tests and results, get advanced reports and add to continuous integration cycles. Learn more here.


To try out BlazeMeter, request a live demo, or put your URL in the box below and your test will start in minutes.

arrowPlease enter a URL with http(s)

Interested in writing for our Blog?Send us a pitch!