Class SilverTestEnv

java.lang.Object
org.silverpeas.kernel.test.extension.SilverTestEnv
All Implemented Interfaces:
org.junit.jupiter.api.extension.AfterEachCallback, org.junit.jupiter.api.extension.BeforeEachCallback, org.junit.jupiter.api.extension.Extension, org.junit.jupiter.api.extension.ParameterResolver, org.junit.jupiter.api.extension.TestInstancePostProcessor, org.junit.jupiter.api.extension.TestInstantiationAwareExtension

public class SilverTestEnv extends Object implements org.junit.jupiter.api.extension.TestInstancePostProcessor, org.junit.jupiter.api.extension.ParameterResolver, org.junit.jupiter.api.extension.BeforeEachCallback, org.junit.jupiter.api.extension.AfterEachCallback
Prepares the environment specific to Silverpeas to run unit tests.

Firstly, it bootstraps the container of beans to use for IoC/IoD and uses the SilverTestEnvContext object declared in the EnableSilverTestEnv annotation to preload it with some managed beans or mocks that can be required by the unit tests.

Secondly it scans for fields and parameters annotated with TestManagedMock and then TestManagedBean to register them automatically into the bean container used in tests. If the class of a TestManagedBean annotated field is qualified by a Qualifier annotated annotation, then it is registered under that qualifier also. For any method parameter annotated with TestManagedMock, it is first resolved by looking for an already registered mock in the bean container (otherwise it is mocked and registered as for fields). Any methods annotated with TestManagedBean are resolved in last.

Thirdly it scans for fields annotated with TestedBean to scan it for injection point in order to resolve those dependencies either by settings them with a bean already registered into the bean container or by mocking it.

Finally it scans for fields annotated with Inject to resolve the dependencies by valuing them with a matching bean or mock from the bean container.

The ordering of the declaration of the different such annotated fields in the test class is very important as they are treated sequentially in their declaration ordering. So, any bean that is required by others beans has to be declared before those others beans.

Author:
mmoquillon
  • Nested Class Summary

    Nested classes/interfaces inherited from interface org.junit.jupiter.api.extension.TestInstantiationAwareExtension

    org.junit.jupiter.api.extension.TestInstantiationAwareExtension.ExtensionContextScope
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    afterEach(org.junit.jupiter.api.extension.ExtensionContext ctx)
    Invokes SilverTestEnvContext.afterTest(TestExecutionContext) and then clear the IoC container.
    void
    beforeEach(org.junit.jupiter.api.extension.ExtensionContext ctx)
    void
    postProcessTestInstance(Object testInstance, org.junit.jupiter.api.extension.ExtensionContext ctx)
    Loads into the IoC container dedicated to the unit test all the classes or beans that are annotated with one of the following annotations: TestManagedBeans: the specified classes of objects are registered into the IoC container TestManagedMock: the annotated field of the test instance is set with a mock that has been registered into the IoC container in order to resolve the dependency on it of managed beans used by the tested class TestManagedBean: the annotated field of the test instance is set with a bean that was automatically put into the IoC container.
    resolveParameter(org.junit.jupiter.api.extension.ParameterContext parameterContext, org.junit.jupiter.api.extension.ExtensionContext extensionContext)
    Resolves the parameter referred by the parameter context by valuing it according to its annotation: if annotated with one of the following annotations TestManagedBean and TestManagedMock, the parameter type will be looked for in the bean container for value injection.
    boolean
    supportsParameter(org.junit.jupiter.api.extension.ParameterContext parameterContext, org.junit.jupiter.api.extension.ExtensionContext extensionContext)
    Is the parameter in a test's method is supported by this extension for value injection?

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

    Methods inherited from interface org.junit.jupiter.api.extension.TestInstantiationAwareExtension

    getTestInstantiationExtensionContextScope
  • Constructor Details

    • SilverTestEnv

      public SilverTestEnv()
  • Method Details

    • postProcessTestInstance

      public void postProcessTestInstance(Object testInstance, org.junit.jupiter.api.extension.ExtensionContext ctx)
      Loads into the IoC container dedicated to the unit test all the classes or beans that are annotated with one of the following annotations:
      • TestManagedBeans: the specified classes of objects are registered into the IoC container
      • TestManagedMock: the annotated field of the test instance is set with a mock that has been registered into the IoC container in order to resolve the dependency on it of managed beans used by the tested class
      • TestManagedBean: the annotated field of the test instance is set with a bean that was automatically put into the IoC container. If yet explicitly instantiated, the bean is put directly into the container and its dependencies are resolved.
      • TestedBean: the annotated field represents an instance of the class covered by the current test class. The field is set with a bean that was automatically put into the IoC container. If yet explicitly instantiated, the bean is put directly into the container and its dependencies are resolved .

      All the beans taken in charge by the IoC container will have their own dependencies resolved and their method annotated with PostConstruct invoked. This is why the order of declarations of the annotated fields is very important in the case an annotated field has a dependency on a bean referred by another field.

      Be caution: any TestedBean annotated fields should be declared lastly for their dependencies to have a change to be set with any previous declared TestManagedMock and TestManagedBean annotated field values.

      Specified by:
      postProcessTestInstance in interface org.junit.jupiter.api.extension.TestInstancePostProcessor
      Parameters:
      testInstance - the instance of the test class.
      ctx - the context of the extension.
    • supportsParameter

      public boolean supportsParameter(org.junit.jupiter.api.extension.ParameterContext parameterContext, org.junit.jupiter.api.extension.ExtensionContext extensionContext)
      Is the parameter in a test's method is supported by this extension for value injection?
      Specified by:
      supportsParameter in interface org.junit.jupiter.api.extension.ParameterResolver
      Parameters:
      parameterContext - the context of the parameter.
      extensionContext - the context of the extension.
      Returns:
      true if the parameter is either annotated with @TestManagedBean or with TestManagedMock
    • resolveParameter

      public Object resolveParameter(org.junit.jupiter.api.extension.ParameterContext parameterContext, org.junit.jupiter.api.extension.ExtensionContext extensionContext)
      Resolves the parameter referred by the parameter context by valuing it according to its annotation: if annotated with one of the following annotations TestManagedBean and TestManagedMock, the parameter type will be looked for in the bean container for value injection. If no such a bean exist in the container, then, according to the annotation, the value will be either instantiated with its default constructor (for TestManagedBean) or mocked (for TestManagedMock) before being put in the bean container and injected as a parameter value.
      Specified by:
      resolveParameter in interface org.junit.jupiter.api.extension.ParameterResolver
      Parameters:
      parameterContext - the context of the parameter.
      extensionContext - the context of the extension.
      Returns:
      the value of the parameter to inject.
    • beforeEach

      public void beforeEach(@NonNull org.junit.jupiter.api.extension.ExtensionContext ctx)
      Specified by:
      beforeEach in interface org.junit.jupiter.api.extension.BeforeEachCallback
      Parameters:
      ctx - the current extension context
    • afterEach

      public void afterEach(@NonNull org.junit.jupiter.api.extension.ExtensionContext ctx)
      Invokes SilverTestEnvContext.afterTest(TestExecutionContext) and then clear the IoC container.
      Specified by:
      afterEach in interface org.junit.jupiter.api.extension.AfterEachCallback
      Parameters:
      ctx - the current extension context