Strings, the most commonly used programming tool, are the main carrier of textual information in programs. This information is passed in the form of a sequence of characters. In Apache JMeter™, Strings are used to output messages to the JMeter console.
For example, when we send an HTTP request using JMeter, we get a reply in the form of a byte array. In order for this response to become readable, it is converted to strings. When an API is tested and the specific data is cut out from the API response, for example when using the "Regular Expression Extractor", the cut out data will be represented as strings. These two examples are a small part of how widely strings are used, both in programming languages and in JMeter.
In testing, Strings are most often used when writing automated scripts, in the following cases:
- Using Variables with Data Type String in Autotests
- Obtaining specific data from the API response and comparing it with the expected result
- Outputting messages to the JMeter console
- Converting primitive data type variables to data type String, and vice versa
This article shows how to to create Strings in JMeter in two of the most popular ways: by using the String Literal and by using the “new” operator. The most common and productive way to create variables with data type String is to use a string literal (String a = "Test"). The "new" operator option is mainly used if you want to create a string from a byte array or an array of characters.
What are Strings?
Strings are a reference data type. This means they reference an object. The mechanism for creating String objects in the JVM (Java Virtual Machine) memory is slightly different from creating all other objects in Java (this difference is associated with memory allocation). Since JMeter is written in the Java programming language, all the possibilities of working with Strings in Java are also available in JMeter. Accordingly, the mechanism for creating Strings in JMeter is similar to Java.
When you create a variable with a String type, an object is created in the memory and a reference is returned. Thus, any reference variable stores not the object that was created, but a reference to the memory area in which the object was created. All references, regardless of the object, for the same version of the JVM are the same size: 32 bits for 32-bit JVM and 64 bits for 64-bit JVMs.
After creating a String object, the value that stores the object cannot be changed. This feature is called the immutability of Strings and makes the Strings thread safe. Immutability and thread safety is a fairly extensive topic that is not included in this article, but its understanding is necessary for complex tests in which many String objects are created. Learn more from here.
How to Create Strings in JMeter
To be able to talk about the mechanism for creating strings, we need to explain the concepts Class and Object. We all know that before creating any product in production, a drawing of this product is initially created. Then, on the basis of this drawing, the product becomes tangible. A similar situation occurs in the Java programming language. The analog of the drawing is the Class, and the Object becomes the analog of the material object. Thus, the Class is the template on which the Object is created. Classes form the basis of the Java programming language.
In other words, an Object is an instance of a class. An example of what a class looks like in Java and in JMeter is shown below:
To create strings in Java, there is a class called String, on the basis of which strings are created. The simplest way to create a variable with a String type (in other words, creating strings) is to use a string literal. A string literal is the value of the variable and appears as a sequence of characters enclosed in quotation marks, for example "Jmeter".
In order to create your first String, you need to do the following:
1. Test Plan -> Add -> Threads (Users) -> Thread Group
2. Thread Group -> Add -> Sampler -> JSR223 Sampler
Note: The JSR223 Sampler is where we write code to create the strings. You can create strings in JMeter in any element that supports BeanShell or Groovy.
In order to visually see that a string was created, the JMeter console will be used. To do this, click the triangle icon with an exclamation point in the upper right corner:
1. Create Strings Using a String Literal
In order to create a variable with the String type, you need to add the following code to the JSR223 Sampler. Just copy this code and change what appears in quotation marks, according to your needs. If you’re writing in Groovy, add the code without setStrictJava (true);. Learn more about creating Strings in Groovy from here.
- String - the type of data that is used to create the string
- nameSoftware - string variable name
- “Jmeter” - string value (String literal)
- log.info() - A method whose incoming parameter is a variable of String type. This method takes a String type variable (in our case it's "nameSoftware") and outputs the value of the variable (in our case "Jmeter") to the JMeter console
The image above shows how to create a variable with a String type using a string literal.
Since the String class is the most used class, the mechanism for creating strings has its own characteristics. All objects that are created in Java are placed in the Heap memory (Heap - the memory allocated inside the JVM). But for strings, inside the Heap there is a memory area called Constant Pool. The constant pool holds all the objects created from the string literal.
When we create Strings using a string literal, the JVM checks the constant pool and if it finds an object with a similar literal, it returns a reference to that object (in other words, it is a reference to the memory area in which this object was created). Otherwise, JVM creates a new object in the constant pool with a string literal value and returns a reference to the newly created object. Below is a graphical model for better understanding.
So, we can arrive at the following conclusion: all variables with String type that are created using a string literal, have one and the same link to the object, if the values of these variables are the same.
2. Create Strings Using the “New” Operator
The second way to create strings is to use the “new” operator. This operator calls the constructor of the String class.
A constructor is a special method that is designed to create and initialize an instance of a class. The constructor is inside the class, but there can also be no constructor in the class. If there is no constructor in the class, JVM calls the default constructor that exists for each class that is created.
Here is an example of how constructors are called when the “New” operator doesn’t appear in the class.
To create Strings using the “new” operator in JMeter, just copy this code and change what appears in quotation marks, according to your needs.
- String - the type of data that is used to create the rows
- software - string variable name
- new - the operator to call the constructor of the String class
- String - The name of the class on which to base the string
- “Jmeter” - string value
Every time we create a variable with a String type using the "new" operator, we call the constructor from the String class. This always creates a new object to which the memory reference returns. The created object gets to the Heap, but not to the constant pool. To transfer such an object to the constant pool, use the intern() method. This method is mainly used for writing complex programs.
Below is a graphical model for better understanding.
Now let’s look at some more options for creating different types of Strings with the “new” operator:
Creating a String that Does Not Contain Characters
A string that does not contain a single character is called an empty string.
Creating a String With an Initial Value
Creating a String From an Array of Characters
In the code example below, symbols are used to create the string (variables with data type "char"). Based on these symbols, a variable is created with the data type String. In other words, if there is any number of characters, you can form a single string of these symbols.
Creating a String From a Part of an Array of Characters
This example is similar to the previous one, but with one difference, an initial and final character is specified from the symbol array, which must be used when forming the string.
Creating a String From Another String
Creating a String From a Byte Array
Each number in the array corresponds to a character in Unicode, as already mentioned in the article “Creating JMeter Variables in Java - The Ultimate Guide”.
Creating a String With a "Null" Value
null is a special value that indicates that the variable is not related to any object. In other words, when a variable is created with a value of null, then in the memory does not create an object and, accordingly, the variable does not store any reference. An example of creating a string with a value of “null” is shown below.
The result that is displayed in the console indicates that the variable a is not associated with any object.
This blog post covered the most common options for creating Strings in JMeter. For additional options, you can refer to the Java API documentation, but this should be enough to write professional automated tests with JMeter.
Nicely done! You can learn more advanced JMeter from our JMeter academy.
You might also find these useful:
Interested in writing for our Blog? Send us a pitch!