2

Please note : Don't delete this thread / post as There is no proper answer available an any of page of StackOverflow. My question is totally different.

I want to mock a private static final field OF A FINAL CLASS

I ahve tried with solution given on How to mock a static final variable using JUnit, EasyMock or PowerMock

static void setFinalStatic(Field field, Object newValue) throws IllegalAccessException, NoSuchFieldException{ 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);
}

But it is giving me ExceptionInInitializerError at "field.set(null, newValue);"

Please advice

Sushant
  • 145
  • 1
  • 2
  • 12
  • I have tried the code and it works as expected (taking into account that the value is assigned in compile time). Reading about the exception that you are getting, it seems like there is some problem in the static initialization. Can you edit with the static part of the code you are trying to change? – raven1981 Jan 14 '19 at 14:33

0 Answers0