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
     }
 }
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.
  • 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