With the new error popping up with java 7 & 8 when using Mockito and PowerMockRunner, Java will throw an Error in byte code exception when there is a static final variable involved. This is due to the now stricter byte code verification and mocking static final objects editing the byte code in order to successfully mock.
I have the following class that I am trying to mock:
public class ClassToBeMocked {
private static final int LIMIT_FROM_PROPERTIES = AnotherClazz.methodToRetrieveFromMap("String being called")
//more stuff
}
I have seen that you can get around this by by using reflection, seen here How to mock a static final variable using JUnit, EasyMock or PowerMock and here PowerMock: mock out private static final variable, a concrete example (not a great solution but it should work). However, using reflection requires that the object already be instantiated, and I am getting the bytecode exception when trying to instantiate ClassToBeMocked.
I have also tried mocking the AnotherClazz.methodToRetrieveFromMap(String) in the unit test (using correct syntax):
Mockito.when( AnotherClazz.methodToRetrieveFromMap("String being called") ).thenReturn(10);
However, This results in the byte code error again.
Is there a way around this catch-22 or a different framework or unit runner that would be better to use?