Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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