Hi,
Thanks for your suggestions!
What I finally did is using reflection to remove the final modifier. In my setup() method I just replace the var with the mock and in my teardown I replace the mock again with the real implementation.
Util method:
private static void setFinalStatic(Field field, Object newValue) throws Exception {
Field field = ClassWhereToMockStaticFinalVar.class.getDeclaredField("FieldName");
field.setAccessible(true);
// remove final modifier from field
Field modifiersField = Field.class.getDeclaredField("modifiers");
modifiersField.setAccessible(true);
modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL);
field.set(null, newValue);
}
Regards,
Timo
Op donderdag 31 mei 2012 08:16:40 UTC+2 schreef Johan Haleby het volgende:
Hi,
I don't think this is possible since static final variables are _compiled_ into constants and the reference to the constant will be inlined in your code. But I'm not really sure this is always the case.. I may not remember it correctly but I _think_ I was able to replace some static final fields using sun.misc.Unsafe in the PowerMock DeepCloner Objenesis project. This approach is NOT recommended though. You could try experimenting with "stub(field(..)).." or "replace(field(..))" in PowerMock (see org.powermock.api.support.membermodification.MemberModifier) or you could try suppressing the static initializer of the class using the @SuppressStaticInitializationFor annotation and then setting the field using reflection. But I'm not sure any of this will actually work for static final fields. Let us know :)
Regards,
/Johan