Skip to content

Instantly share code, notes, and snippets.

@rwoloszyn
Created October 3, 2016 11:45
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rwoloszyn/adc289dd596283c2d635658af59ac3b2 to your computer and use it in GitHub Desktop.
Save rwoloszyn/adc289dd596283c2d635658af59ac3b2 to your computer and use it in GitHub Desktop.
Reset singleton field during JUnit tests, roboelectric junit tests
@After
public void teaddown() {
setting.clear();
resetSingleton(ErgoSettings.class, "sInstance");
}
public static void resetSingleton(Class clazz, String fieldName) {
Field instance;
try {
instance = clazz.getDeclaredField(fieldName);
instance.setAccessible(true);
instance.set(null, null);
} catch (Exception e) {
throw new RuntimeException();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment