A utility library to handle Java reflection with ease
This library will make it easier to get and set internal fields in classes using java reflection. The library is optimized for convenience and not for speed, so it works best with unit tests.
Mocking is great! Sometimes the easiest way to insert mocks are by Java reflection. Especially if you cannot use constructor arguments or setter methods.
@Test
public void storeMethodShouldSaveEntity() throws Exception {
// Setup entities
EntityHandler handler = new EntityHandlerImpl();
StoreDao storeDao = mock(StoreDao.class);
// Insert mock into handler
new ReflectionHelper(handler).setField(storeDao);
// Perform method
handler.store();
// Verify save method
verify(storeDao).save();
}
The ReflectionHelper will insert the mocked object into the handler
The Reflection helper have 5 methods:
ABC abc = ReflectionHelper.instantiatePrivateConstructor(ABC.class) will create a new instance of ABC by invoking a (private) constructor with no arguments. This method can be used for getting hard-to-reach coverage for utility classes.
new ReflectionHelper(abc).setField(value) will insert the value into the instance abc. This method can be used to insert stubs or mocks into other classes. ReflectionHelper will determine which field that should be set by comparing class types or assignable types. Setting an interface type to a concrete class value is no problem.
new ReflectionHelper(abc).setField("name", value) will insert the value into the instance abc for a named field. This method can be used to insert setup parameters where there are multiple fields that have the same type.
ValueClass value = new ReflectionHelper(abc).getField(ValueClass.class) will return the current value for the field of a specified class.
ValueClass value = (ValueClass) new ReflectionHelper(abc).getField("name") will return the current value for a named field.
Maven users can add this library with the following addition to their pom.xml file.
...
<dependencies>
...
<dependency>
<groupId>com.google.code.reflection-utils</groupId>
<artifactId>reflection-utils</artifactId>
<version>0.0.1</version>
<scope>test</scope>
</dependency>
</dependencies>
...
The visibility scope of the fields are like this:
This framework was created for the following reasons: