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
    Constructor
    Description
    Creates a new instance.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    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)

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • 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 interface org.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 interface org.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 interface org.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 interface org.junit.jupiter.api.extension.ParameterResolver
      Throws:
      org.junit.jupiter.api.extension.ParameterResolutionException
    • injectFields

      protected void injectFields(@NotNull @NotNull Class<?> testClass, Object testInstance)
      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

      protected Object generateValue(@NotNull @NotNull Class<?> testClass, @NotNull @NotNull Field field)
      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.