Class RandomStringResolver
- All Implemented Interfaces:
Extension,ParameterResolver,TestInstancePostProcessor,TestInstantiationAwareExtension
RandomString annotation.
This resolver automatically injects random string values into test method parameters that are annotated with
@RandomString or are part of test methods annotated with @RandomString at the method level.
Supported Types:
String- single random string (when size = 0)List<String>- list of random strings (when size > 0)Set<String>- set of unique random strings (when size > 0)
Resolution Priority:
Parameter-level annotation takes precedence over method-level.
Security:
Uses SecureRandom for cryptographically strong random number generation.
Default Configuration:
- Length: 10 characters
- Character Set: Alphanumeric (A-Z, a-z, 0-9)
- Size: 0 (single string)
- 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 withRandomString.resolveParameter(ParameterContext parameterContext, ExtensionContext extensionContext) Resolves the parameter by generating a random string, list, or set based on annotation configuration.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
-
RandomStringResolver
public RandomStringResolver()
-
-
Method Details
-
postProcessTestInstance
Processes fields of the test instance annotated withRandomString.Enables field injection for random strings. Field must be of type
String,List<String>, orSet<String>. Fields of unsupported types annotated with@RandomStringwill cause anIllegalArgumentExceptionto be thrown.- Specified by:
postProcessTestInstancein interfaceTestInstancePostProcessor- Parameters:
testInstance- the test class instancecontext- the current extension context- Throws:
IllegalArgumentException- if a@RandomString-annotated field has an unsupported typeException
-
supportsParameter
public boolean supportsParameter(ParameterContext parameterContext, ExtensionContext extensionContext) Determines if this resolver can resolve a parameter.- Specified by:
supportsParameterin interfaceParameterResolver- Parameters:
parameterContext- information about the parameter to be resolvedextensionContext- the current extension context- Returns:
trueif the parameter can be resolved by this extension,falseotherwise
-
resolveParameter
public Object resolveParameter(ParameterContext parameterContext, ExtensionContext extensionContext) Resolves the parameter by generating a random string, list, or set based on annotation configuration.Priority: Parameter-level > Method-level > Default (10, alphanumeric, size 0).
Note: The type has already been validated by
supportsParameter(org.junit.jupiter.api.extension.ParameterContext, org.junit.jupiter.api.extension.ExtensionContext), so the final branch ingenerateValue(java.lang.Class<?>, int, int, java.lang.String)is a defensive guard that should not be reachable in normal usage.- Specified by:
resolveParameterin interfaceParameterResolver- Parameters:
parameterContext- information about the parameter to be resolvedextensionContext- the current extension context- Returns:
- a randomly generated string, list, or set matching the annotation specifications
- Throws:
IllegalArgumentException- if the annotation specifies invalid parameters
-