org.silverpeas.test.rule
Class MockByReflectionRule
java.lang.Object
org.silverpeas.test.rule.MockByReflectionRule
- All Implemented Interfaces:
- org.junit.rules.TestRule
public class MockByReflectionRule
- extends Object
- implements org.junit.rules.TestRule
This rule handles field injections.
The mechanism is very simple:
- saving the previous value
- setting the given value
- at the end of the test, the old value is set again
This prevent from getting an unstable context of tests in case of chained execution.
- Author:
- Yohann Chastagnier
Method Summary |
org.junit.runners.model.Statement |
apply(org.junit.runners.model.Statement base,
org.junit.runner.Description description)
|
|
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. |
|
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. |
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
MockByReflectionRule
public MockByReflectionRule()
apply
public org.junit.runners.model.Statement apply(org.junit.runners.model.Statement base,
org.junit.runner.Description description)
- Specified by:
apply
in interface org.junit.rules.TestRule
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.
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 to
the field. If the fieldName path part starts with '.' character, it sets that the
field is static.
- Returns:
- the new mocked instance.
Copyright © 2016 Silverpeas. All Rights Reserved.