top of page

Useful: generating random strings with RandomStringUtils


When writing tests that require the generation of random strings, a very useful class can come in handy, namely RandomStringUtils from the Apache Commons Langs utilities library. It can be used for generating string that contain only letters, only numbers, both, these and other characters.

Using the class in your project

To use the RandomStringUtils class in your project, you will need to import it. In the dependencies section of your pom.xml file, enter the following entry, which will import the org,apache.commons.lang3 dependency into your project (the current latest dependency version is 3.3.2, as found at the Central Maven Repository):

<dependency>
   <groupId>org.apache.commons</groupId>
   <artifactId>commons-lang3</artifactId>
   <version>3.3.2</version>
</dependency>

In the class where you want to write the test, import the RandomStringUtils class (bear in mind that all the methods in this class are static, so in order to simplify usage in the tests, the 'import static' option is used):

import static org.apache.commons.lang3.RandomStringUtils.*;

Methods and usage

The methods available in this class are described below, together with some examples:

randomNumeric(int length)

  1. generates a string of only numeric characters, of the length passed as the method parameter.

Examples:

  1. randomNumeric(10) --> 1163361204

randomAlphabetic(int length)

  1. generates a string of only alphabetical characters, of the length passed as the method parameter. The string is most often generated as mixed case (both upper and lower letters), but not all the time. If you need a string that is always mixed case, you can use a concatenation of a random string that is only upper case and one that is only lower case. To achieve the upper and lower cases, you will apply the standard String .toLowerCase() and .toUpperCase() methods.

Examples:

  1. randomAlphabetic(10) --> KTtFdKyXtD

  2. randomAlphabetic(10).toLowerCase() --> beotzolkcg

  3. randomAlphabetic(10).toUpperCase() --> XACUPRWZWA

randomAscii(int lenght)

  1. generates a string consisting of characters that have ASCII codes between 32 and 126. The string has the length specified as the method's parameter

Examples:

  1. randomAscii(20) --> 6<eGb<iO@CAV8|OiIde`

random(int length)

  1. generates a string from all the available characters, having a length specified as the method's only parameter

Examples:

  1. random(16) --> 탟襾彋焏⅞ђ❀≷ﺤᵐ櫨凨瞘臛

random(int length, boolean letters, boolean numbers)

  1. generates a random string of the length specified as the first parameter in the method signature. The next parameter, letters, if true - adds letters to the generated string; if false - does not allow for letters to be present in the generated string. The number parameter, also boolean, if true - adds numbers to the generated string; if false - does not allow for numeric characters to be present in the generated string.

Examples:

  1. random(6, true, false) --> gRWMOx

  2.  random(10, true, true) --> L03K9CnyZ4

  3. random(10, false, true) --> 7453949743

random(int length, int start, int end, boolean letters, boolean numbers)

  1. generates a random string that contains characters having the ASCII code between the 'start' and 'end' parameters from the method signature. The total length of the string is given by the first parameter of the method. If the booleans for letters or numbers are true, such characters will also be included in the generated string

Examples:

  1. random(10, 32, 40, false, false) --> !!! !!!'%#

  2. random(10, 32, 120, true, false) --> FbQZhsBAUF

Recent Posts

See All

Creating an Architecture for Your Automated Tests Writing Automated Tests in Small Increments 4 Times Your Automation Passed While Bugs Were Present Now That You’ve Created Automated Tests, Run Them!

This week my newest article was released, this time on how to use Log4j to log relevant test automation information: https://blog.testproject.io/2021/05/05/logging-test-automation-information-with-log

bottom of page