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 TypeMethodDescriptionvoidbeforeAll(@NotNull org.junit.jupiter.api.extension.ExtensionContext context) voidbeforeEach(@NotNull org.junit.jupiter.api.extension.ExtensionContext context) protected ObjectgenerateValue(@NotNull Class<?> testClass, @NotNull Field field) Generates a random value for the specified field of the test class.protected voidinjectField(@NotNull Class<?> testClass, @NotNull Field field, Object testInstance, Object value) Injects to the target field of the test class a generated random value.protected voidinjectFields(@NotNull Class<?> testClass, Object testInstance) Injects to the fields annotated with@Randomof the test class generated random values.resolveParameter(@NotNull org.junit.jupiter.api.extension.ParameterContext parameterContext, @NotNull org.junit.jupiter.api.extension.ExtensionContext extensionContext) booleansupportsParameter(@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
@Randomwith random values.- Specified by:
beforeAllin 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
@Randomwith random values.- Specified by:
beforeEachin 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:
supportsParameterin 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
@Randomwith random values.- Specified by:
resolveParameterin interfaceorg.junit.jupiter.api.extension.ParameterResolver- Throws:
org.junit.jupiter.api.extension.ParameterResolutionException
-
injectFields
Injects to the fields annotated with@Randomof 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.
-