Annotation Interface RandomString


@Target({PARAMETER,METHOD,FIELD}) @Retention(RUNTIME) public @interface RandomString
JUnit annotation for configuring random string generation in test method parameters.

Apply this annotation to String parameters in test methods to automatically inject randomly generated strings. The extension will resolve these parameters during test execution.

This annotation can also be applied at the method level to configure default settings for all String parameters in that method.

Usage examples:

 @ExtendWith(RandomStringResolver.class)
 public class MyTest {
     // Default: 10 alphanumeric characters
     @Test
     void test(@RandomString String str) { ... }

     // Custom length: 15 characters
     @Test
     void test(@RandomString(length = 15) String str) { ... }

     // Custom character set: only uppercase letters
     @Test
     void test(@RandomString(characters = TestingUtils.UPPERCASE_CHARACTERS) String str) { ... }

     // Both custom length and characters: 8-character hex string
     void test(@RandomString(length = 8, characters = TestingUtils.HEXADECIMAL_CHARACTERS) String hexStr) { ... }

     // Multiple parameters
     @Test
     void test(@RandomString(characters = "ABC123") String str1, @RandomString(length = 5) String str2) { ... }

     // Method-level annotation for single parameter
     @Test
     @RandomString(length = 5)
     void test(String random) { ... }

     // Method-level annotation applies to all String parameters
     @Test
     @RandomString(length = 8, characters = TestingUtils.URL_SAFE_CHARACTERS)
     void test(String url1, String url2) { ... }

     // Parameter-level annotation overrides method-level
     @Test
     @RandomString(length = 5)
     void test(String defaultRandom, @RandomString(length = 3) String shortRandom) { ... }

     // Field injection
     @RandomString
     private String myRandomString;

     @Test
     void test() {
         // myRandomString is initialized before test
     }

     // List of random strings
     @Test
     void test(@RandomString(size = 5) List<String> randomList) { ... }

     // Set of random strings with custom length
     @Test
     void test(@RandomString(size = 10, length = 8) Set<String> randomSet) { ... }

     // List with custom character set
     @Test
     void test(@RandomString(size = 3, characters = TestingUtils.HEXADECIMAL_CHARACTERS) List<String> hexList) { ... }

     // Field injection for List
     @RandomString(size = 5)
     private List<String> randomStrings;

     // Field injection for Set
     @RandomString(size = 10, length = 15)
     private Set<String> randomStringSet;
 }
Since:
1.0
Author:
Erik C. Thauvin
See Also:
  • Optional Element Summary

    Optional Elements
    Modifier and Type
    Optional Element
    Description
    The alphanumeric character set to use for random string generation.
    int
    The length of the generated random string.
    int
    The number of random strings to generate for List or Set parameters.
  • Element Details

    • characters

      String characters
      The alphanumeric character set to use for random string generation.
      Returns:
      the character set string
      Throws:
      IllegalArgumentException - if the character set is null or empty during resolution
      Default:
      "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
    • length

      int length
      The length of the generated random string.
      Returns:
      the desired length
      Throws:
      IllegalArgumentException - if length is 0 or negative during resolution
      Default:
      10
    • size

      int size
      The number of random strings to generate for List or Set parameters.

      When size is greater than 0, this annotation can be applied to:

      • List<String> parameters - generates a list of random strings
      • Set<String> parameters - generates a set of unique random strings

      When size is 0 (default), this annotation applies to single String parameters.

      Returns:
      the number of strings to generate, or 0 for single string generation
      Throws:
      IllegalArgumentException - if size is negative during resolution
      Default:
      0