Class 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
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 -
Method Summary
Modifier and TypeMethodDescriptionvoidafterEach(org.junit.jupiter.api.extension.ExtensionContext ctx) InvokesSilverTestEnvContext.afterTest(TestExecutionContext)and then clear the IoC container.voidbeforeEach(org.junit.jupiter.api.extension.ExtensionContext ctx) voidpostProcessTestInstance(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 containerTestManagedMock: 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 classTestManagedBean: 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 annotationsTestManagedBeanandTestManagedMock, the parameter type will be looked for in the bean container for value injection.booleansupportsParameter(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, waitMethods 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 containerTestManagedMock: 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 classTestManagedBean: 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
Be caution: anyPostConstructinvoked. 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.TestedBeanannotated fields should be declared lastly for their dependencies to have a change to be set with any previous declaredTestManagedMockandTestManagedBeanannotated field values.- Specified by:
postProcessTestInstancein interfaceorg.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:
supportsParameterin interfaceorg.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 @
TestManagedBeanor withTestManagedMock
-
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 annotationsTestManagedBeanandTestManagedMock, 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 (forTestManagedBean) or mocked (forTestManagedMock) before being put in the bean container and injected as a parameter value.- Specified by:
resolveParameterin interfaceorg.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
- Specified by:
beforeEachin interfaceorg.junit.jupiter.api.extension.BeforeEachCallback- Parameters:
ctx- the current extension context
-
afterEach
InvokesSilverTestEnvContext.afterTest(TestExecutionContext)and then clear the IoC container.- Specified by:
afterEachin interfaceorg.junit.jupiter.api.extension.AfterEachCallback- Parameters:
ctx- the current extension context
-