Class RandomRangeResolver
- All Implemented Interfaces:
Extension,ParameterResolver,TestInstancePostProcessor,TestInstantiationAwareExtension
RandomRange annotation.
This resolver automatically injects random integer values into test method parameters that are annotated with
@RandomRange or are part of test methods annotated with @RandomRange at the method level.
Supported Types:
int- single random integer (when size = 0)List<Integer>- list of random integers (when size > 0)Set<Integer>- set of unique random integers (when size > 0)
Resolution Priority:
When both parameter-level and method-level @RandomRange annotations are present,
the parameter-level annotation takes precedence.
If only a method-level annotation exists, its configuration applies to all int parameters in that method.
The resolver validates that:
- The parameter is annotated with
@RandomRangeor the method is annotated with@RandomRange - The parameter type is
int,List<Integer>, orSet<Integer> - The minimum value is not greater than the maximum value
- Since:
- 1.0
- Author:
- Erik C. Thauvin
- See Also:
-
Nested Class Summary
Nested classes/interfaces inherited from interface org.junit.jupiter.api.extension.TestInstantiationAwareExtension
TestInstantiationAwareExtension.ExtensionContextScope -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidpostProcessTestInstance(Object testInstance, ExtensionContext context) Processes fields of the test instance annotated withRandomRange.resolveParameter(ParameterContext parameterContext, ExtensionContext extensionContext) Resolves a parameter by generating a random integer within the specified range.booleansupportsParameter(ParameterContext parameterContext, ExtensionContext extensionContext) Determines if this resolver can resolve a parameter.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface org.junit.jupiter.api.extension.TestInstantiationAwareExtension
getTestInstantiationExtensionContextScope
-
Constructor Details
-
RandomRangeResolver
public RandomRangeResolver()
-
-
Method Details
-
postProcessTestInstance
Processes fields of the test instance annotated withRandomRange.Enables field injection for random ints. The field must be of type
int,List<Integer>, orSet<Integer>. Fields of unsupported types are skipped with a warning.- Specified by:
postProcessTestInstancein interfaceTestInstancePostProcessor- Parameters:
testInstance- the test class instancecontext- the current extension context- Throws:
Exception
-
supportsParameter
public boolean supportsParameter(ParameterContext parameterContext, ExtensionContext extensionContext) Determines if this resolver can resolve a parameter.Supports int parameters annotated with
RandomRangeat parameter or method level.- Specified by:
supportsParameterin interfaceParameterResolver- Parameters:
parameterContext- the context for the parameter to be resolvedextensionContext- the extension context for the test being executed- Returns:
trueif this resolver can resolve the parameter,falseotherwise- Throws:
ParameterResolutionException- if an error occurs while determining support
-
resolveParameter
public Object resolveParameter(ParameterContext parameterContext, ExtensionContext extensionContext) Resolves a parameter by generating a random integer within the specified range.Priority: Parameter-level > Method-level > Default (0–100, for safety).
Note: the default (0–100) fallback is a safety net; in practice it is unreachable because
supportsParameter(org.junit.jupiter.api.extension.ParameterContext, org.junit.jupiter.api.extension.ExtensionContext)requires at least one@RandomRangeannotation to be present.- Specified by:
resolveParameterin interfaceParameterResolver- Parameters:
parameterContext- the context for the parameter to be resolvedextensionContext- the extension context for the test being executed- Returns:
- a random integer, list, or set within the specified range
- Throws:
ParameterResolutionException- ifmin>max- See Also:
-