Skip to content

Instantly share code, notes, and snippets.

Created October 3, 2016 11:45
What would you like to do?
Reset singleton field during JUnit tests, roboelectric junit tests
public void teaddown() {
resetSingleton(ErgoSettings.class, "sInstance");
public static void resetSingleton(Class clazz, String fieldName) {
Field instance;
try {
instance = clazz.getDeclaredField(fieldName);
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