Package dev.orne.test.rnd.junit
Class RandomValueExtension
java.lang.Object
dev.orne.test.rnd.junit.RandomValueExtension
- All Implemented Interfaces:
org.junit.jupiter.api.extension.BeforeAllCallback
,org.junit.jupiter.api.extension.BeforeEachCallback
,org.junit.jupiter.api.extension.Extension
,org.junit.jupiter.api.extension.ParameterResolver
public class RandomValueExtension
extends Object
implements org.junit.jupiter.api.extension.BeforeAllCallback, org.junit.jupiter.api.extension.BeforeEachCallback, org.junit.jupiter.api.extension.ParameterResolver
JUnit Jupiter extension for automatic random value injection.
Supports injection in static fields (before all tests), in instance fields (before each test) and in constructor and test or callback method arguments.
- Since:
- 0.1
- Version:
- 1.0, 2023-11
- Author:
- (w) Iker Hernaez
- See Also:
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoid
beforeAll
(@NotNull org.junit.jupiter.api.extension.ExtensionContext context) void
beforeEach
(@NotNull org.junit.jupiter.api.extension.ExtensionContext context) protected Object
generateValue
(@NotNull Class<?> testClass, @NotNull Field field) Generates a random value for the specified field of the test class.protected void
injectField
(@NotNull Class<?> testClass, @NotNull Field field, Object testInstance, Object value) Injects to the target field of the test class a generated random value.protected void
injectFields
(@NotNull Class<?> testClass, Object testInstance) Injects to the fields annotated with@Random
of the test class generated random values.resolveParameter
(@NotNull org.junit.jupiter.api.extension.ParameterContext parameterContext, @NotNull org.junit.jupiter.api.extension.ExtensionContext extensionContext) boolean
supportsParameter
(@NotNull org.junit.jupiter.api.extension.ParameterContext parameterContext, @NotNull org.junit.jupiter.api.extension.ExtensionContext extensionContext)
-
Constructor Details
-
RandomValueExtension
public RandomValueExtension()Creates a new instance.
-
-
Method Details
-
beforeAll
public void beforeAll(@NotNull @NotNull org.junit.jupiter.api.extension.ExtensionContext context) throws GenerationException Injects static fields annotated with
@Random
with random values.- Specified by:
beforeAll
in interfaceorg.junit.jupiter.api.extension.BeforeAllCallback
- Throws:
GenerationException
-
beforeEach
public void beforeEach(@NotNull @NotNull org.junit.jupiter.api.extension.ExtensionContext context) throws GenerationException Injects instance fields annotated with
@Random
with random values.- Specified by:
beforeEach
in interfaceorg.junit.jupiter.api.extension.BeforeEachCallback
- Throws:
GenerationException
-
supportsParameter
public boolean supportsParameter(@NotNull @NotNull org.junit.jupiter.api.extension.ParameterContext parameterContext, @NotNull @NotNull org.junit.jupiter.api.extension.ExtensionContext extensionContext) throws org.junit.jupiter.api.extension.ParameterResolutionException - Specified by:
supportsParameter
in interfaceorg.junit.jupiter.api.extension.ParameterResolver
- Throws:
org.junit.jupiter.api.extension.ParameterResolutionException
-
resolveParameter
public Object resolveParameter(@NotNull @NotNull org.junit.jupiter.api.extension.ParameterContext parameterContext, @NotNull @NotNull org.junit.jupiter.api.extension.ExtensionContext extensionContext) throws org.junit.jupiter.api.extension.ParameterResolutionException Resolves method parameters annotated with
@Random
with random values.- Specified by:
resolveParameter
in interfaceorg.junit.jupiter.api.extension.ParameterResolver
- Throws:
org.junit.jupiter.api.extension.ParameterResolutionException
-
injectFields
Injects to the fields annotated with@Random
of the test class generated random values.If a test instance is provided annotated instance fields will be injected. Otherwise instance fields will be injected.
- Parameters:
testClass
- The test class.testInstance
- The test instance, if any.- Throws:
org.junit.jupiter.api.extension.ParameterResolutionException
- If an error occurs injecting the field values.
-
generateValue
Generates a random value for the specified field of the test class.- Parameters:
testClass
- The test class.field
- The target field.- Returns:
- The generated random value.
-
injectField
protected void injectField(@NotNull @NotNull Class<?> testClass, @NotNull @NotNull Field field, Object testInstance, Object value) Injects to the target field of the test class a generated random value.- Parameters:
testClass
- The test class.field
- The target field.testInstance
- The test instance, if any.value
- The random value.- Throws:
org.junit.jupiter.api.extension.ParameterResolutionException
- If an error occurs injecting the field value.
-