org.silverpeas.test.rule
Class MockByReflectionRule

java.lang.Object
  extended by 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:

This prevent from getting an unstable context of tests in case of chained execution.

Author:
Yohann Chastagnier

Constructor Summary
MockByReflectionRule()
           
 
Method Summary
 org.junit.runners.model.Statement apply(org.junit.runners.model.Statement base, org.junit.runner.Description description)
           
<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.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

MockByReflectionRule

public MockByReflectionRule()
Method Detail

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.