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:
- generates a string of only numeric characters, of the length passed as the method parameter.
- randomNumeric(10) –> 1163361204
- 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.
- randomAlphabetic(10) –> KTtFdKyXtD
- randomAlphabetic(10).toLowerCase() –> beotzolkcg
- randomAlphabetic(10).toUpperCase() –> XACUPRWZWA
- 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
- randomAscii(20) –> 6<eGb<iO@CAV8|OiIde`
- generates a string from all the available characters, having a length specified as the method’s only parameter
- random(16) –> 탟襾彋焏⅞ђ❀≷ﺤᵐ櫨凨瞘臛
random(int length, boolean letters, boolean numbers)
- 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.
- random(6, true, false) –> gRWMOx
- random(10, true, true) –> L03K9CnyZ4
- random(10, false, true) –> 7453949743
random(int length, int start, int end, boolean letters, boolean numbers)
- 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
- random(10, 32, 40, false, false) –> !!! !!!’%#
- random(10, 32, 120, true, false) –> FbQZhsBAUF