Annotation Interface RandomRange


@Target({PARAMETER,METHOD,FIELD}) @Retention(RUNTIME) public @interface RandomRange
JUnit annotation for generating random integer values within a specified range.

This annotation can be applied to test method parameters of type int to automatically inject random values during test execution. It can also be applied at the method level to configure default settings for all int parameters in that method.

Example usage:

 @ExtendWith(RandomRangeResolver.class)
 public class MyTest {
     // Parameter-level annotation
     @Test
     public void testMethod(@RandomRange(min = 1, max = 10) int randomValue) {
         // randomValue will be a random integer between 1 and 10 (inclusive)
     }

     // Method-level annotation for single parameter
     @Test
     @RandomRange(min = 5, max = 15)
     public void testWithMethodLevel(int randomValue) {
         // randomValue will be a random integer between 5 and 15 (inclusive)
     }

     // Method-level annotation applies to all int parameters
     @Test
     @RandomRange(min = 1, max = 100)
     public void testMultiple(int first, int second) {
         // Both first and second will be random integers between 1 and 100 (inclusive)
     }

     // Parameter-level annotation overrides method-level
     @Test
     @RandomRange(min = 1, max = 10)
     public void testMixed(int defaultRange, @RandomRange(min = 50, max = 60) int customRange) {
         // defaultRange: 1-10, customRange: 50-60
     }

     // Field injection
     @RandomRange(min = 1, max = 100)
     private int myRandomInt;

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

     // List of random integers
     @Test
     void test(@RandomRange(size = 5, min = 1, max = 10) List<Integer> randomList) { ... }

     // Set of random integers
     @Test
     void test(@RandomRange(size = 10, min = 0, max = 100) Set<Integer> randomSet) { ... }

     // Field injection for List
     @RandomRange(size = 5, min = 1, max = 50)
     private List<Integer> randomInts;

     // Field injection for Set
     @RandomRange(size = 10, min = 10, max = 20)
     private Set<Integer> randomIntSet;
 }
Since:
1.0
Author:
Erik C. Thauvin
See Also:
  • Optional Element Summary

    Optional Elements
    Modifier and Type
    Optional Element
    Description
    int
    The maximum value (inclusive) for the random number generation.
    int
    The minimum value (inclusive) for the random number generation.
    int
    The number of random integers to generate for List or Set parameters.
  • Element Details

    • max

      int max
      The maximum value (inclusive) for the random number generation.
      Returns:
      the maximum value, defaults to 100
      Default:
      100
    • min

      int min
      The minimum value (inclusive) for the random number generation.
      Returns:
      the minimum value, defaults to 0
      Default:
      0
    • size

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

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

      • List<Integer> parameters - generates a list of random integers
      • Set<Integer> parameters - generates a set of unique random integers

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

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