Class FieldMocker
- java.lang.Object
-
- org.silverpeas.core.test.unit.extention.FieldMocker
-
- All Implemented Interfaces:
org.junit.jupiter.api.extension.AfterEachCallback
,org.junit.jupiter.api.extension.Extension
public class FieldMocker extends Object implements org.junit.jupiter.api.extension.AfterEachCallback
Provides mocking facilities on the fields of beans used in unit tests.- Author:
- mmoquillon
-
-
Constructor Summary
Constructors Constructor Description FieldMocker()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
afterEach(org.junit.jupiter.api.extension.ExtensionContext context)
<T> T
mockField(Object instanceOrClass, Class<T> classToMock, String fieldNames)
Mocks a field specified by the given field name of the given instance with a new mock instance of the specified class.<T> T
setField(Object instanceOrClass, T value, String fieldNames)
Sets a field specified by the given field name of the given instance with a given value of the specified class.<T> T
spyField(Object instanceOrClass, Class<T> classToMock, String fieldNames)
Spies a field specified by the given field name of the given instance with a new mock instance of the specified class.<T> T
spyField(Object instanceOrClass, T value, String fieldNames)
Spies a field specified by the given field name of the given instance with a new mock instance of the specified class.protected void
unsetMockedFields()
-
-
-
Method Detail
-
afterEach
public void afterEach(org.junit.jupiter.api.extension.ExtensionContext context) throws Exception
- Specified by:
afterEach
in interfaceorg.junit.jupiter.api.extension.AfterEachCallback
- Throws:
Exception
-
unsetMockedFields
protected void unsetMockedFields() throws IllegalAccessException
- Throws:
IllegalAccessException
-
mockField
public <T> T mockField(Object instanceOrClass, Class<T> classToMock, String fieldNames)
Mocks a field specified by the given field name of the given instance with a new mock instance of the specified class.- Type Parameters:
T
- the type of the mocked instance.- Parameters:
instanceOrClass
- the instance or class into which the field will be mocked.classToMock
- the class to get a new mock instance.fieldNames
- the aimed field name. If several, then it represents a path to access to the field. If the fieldName path part starts with '.' character, it sets that the field is static.- Returns:
- the new mocked instance.
-
spyField
public <T> T spyField(Object instanceOrClass, Class<T> classToMock, String fieldNames)
Spies a field specified by the given field name of the given instance with a new mock instance of the specified class.- Type Parameters:
T
- the type of the mocked instance.- Parameters:
instanceOrClass
- the instance or class into which the field will be mocked.classToMock
- the class to get a new mock instance.fieldNames
- the aimed field name. If several, then it represents a path to access to the field. If the fieldName path part starts with '.' character, it sets that the field is static.- Returns:
- the new mocked instance.
-
spyField
public <T> T spyField(Object instanceOrClass, T value, String fieldNames)
Spies a field specified by the given field name of the given instance with a new mock instance of the specified class.- Type Parameters:
T
- the type of the mocked instance.- Parameters:
instanceOrClass
- the instance or class into which the field will be mocked.value
- the value to spy.fieldNames
- the aimed field name. If several, then it represents a path to access to the field. If the fieldName path part starts with '.' character, it sets that the field is static.- Returns:
- the new mocked instance.
-
setField
public <T> T setField(Object instanceOrClass, T value, String fieldNames)
Sets a field specified by the given field name of the given instance with a given value of the specified class.- Type Parameters:
T
- the type of the mocked instance.- Parameters:
instanceOrClass
- the instance or class into which the field will be mocked.value
- the value to set.fieldNames
- the aimed field name. If several, then it represents a path to access the field. If the fieldName path part starts with '.' character, it sets that the field is static.- Returns:
- the new mocked instance.
-
-