Class SilverTestEnv
- java.lang.Object
-
- org.silverpeas.core.test.extention.SilverTestEnv
-
- All Implemented Interfaces:
org.junit.jupiter.api.extension.BeforeEachCallback
,org.junit.jupiter.api.extension.Extension
,org.junit.jupiter.api.extension.ParameterResolver
,org.junit.jupiter.api.extension.TestInstancePostProcessor
public class SilverTestEnv extends Object implements org.junit.jupiter.api.extension.TestInstancePostProcessor, org.junit.jupiter.api.extension.ParameterResolver, org.junit.jupiter.api.extension.BeforeEachCallback
Prepares the environment specific to Silverpeas to run unit tests.Firstly, it mocks the container of beans and set ups it for the tests with some of the common beans in Silverpeas:
UserProvider
,GroupProvider
,SystemWrapper
,I18n
,ManagedThreadPool
, and the logging system.Secondly it scans for fields and parameters annotated with
TestManagedBean
andTestManagedMock
to register them automatically into the bean container used in tests. If the class of aTestManagedBean
annotated field is qualified by aQualifier
annotated annotation, then it is registered under that qualifier also. For any parameter annotated withTestManagedMock
, 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 withTestManagedBean
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 looks for a method in the test class that is annotated with
RequesterProvider
to execute it and to set the returnedUser
instance as the default requester to use in all the tests of the class.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
-
-
Constructor Summary
Constructors Constructor Description SilverTestEnv()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
beforeEach(org.junit.jupiter.api.extension.ExtensionContext context)
Prepares the unit test environment before executing any test.void
postProcessTestInstance(Object testInstance, org.junit.jupiter.api.extension.ExtensionContext context)
Injects in the unit test class all the fields that are annotated with one of the supported annotations bySilverTestEnv
extension (TestManagedMock
,TestManagedBean
,TestedBean
, ...).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 withTestManagedBean
, the parameter will be instantiated with its default constructor; if annotated withTestManagedMock
, the parameter will be mocked.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?
-
-
-
Method Detail
-
postProcessTestInstance
public void postProcessTestInstance(Object testInstance, org.junit.jupiter.api.extension.ExtensionContext context) throws Exception
Injects in the unit test class all the fields that are annotated with one of the supported annotations bySilverTestEnv
extension (TestManagedMock
,TestManagedBean
,TestedBean
, ...). Each of such annotated beans will be either mocked or instantiated with their default constructor and then registered into the bean container used for the unit tests.Be caution: any
TestedBean
annotated fields should be declared lastly for their dependencies to have a change to be set with any previous declaredTestManagedMock
andTestManagedBean
annotated field values.- Specified by:
postProcessTestInstance
in interfaceorg.junit.jupiter.api.extension.TestInstancePostProcessor
- Parameters:
testInstance
- the instance of the test class.context
- the context of the extension.- Throws:
Exception
- if an error occurs while injecting the fields.
-
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 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 @
TestManagedBean
or 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 withTestManagedBean
, the parameter will be instantiated with its default constructor; if annotated withTestManagedMock
, the parameter will be mocked.- Specified by:
resolveParameter
in 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
public void beforeEach(org.junit.jupiter.api.extension.ExtensionContext context) throws Exception
Prepares the unit test environment before executing any test. Some beans are mocked by default (GroupProvider
,UserProvider
,ManagedThreadFactory
, and so on.) If the unit test defines a method annotated withRequesterProvider
, then it is invoked to get the user to set as the default requester.- Specified by:
beforeEach
in interfaceorg.junit.jupiter.api.extension.BeforeEachCallback
- Parameters:
context
- the context of the extension.- Throws:
Exception
- if an error occurs while preparing the test environement.
-
-