lundi 14 janvier 2019

How to mock static final variable in a final class

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





Aucun commentaire:

Enregistrer un commentaire