public Field findSingletonStaticPrivateInstanceField() { for (Field field : Earth.class.getDeclaredFields()) { if (field.getType() == Earth.class) { return field; } } return null; }
@Test public void checkThatSingletonContainsStaticPrivateInstanceField() { Field field = findSingletonStaticPrivateInstanceField(); assertNotNull("No static private instance field found.", field); assertTrue( "Field pointing to the Earth instance should be static", Modifier.isStatic(field.getModifiers())); assertTrue( "Field pointing to the Earth instance should be private", Modifier.isPrivate(field.getModifiers())); }
private void replaceEarth(String earthclass) throws Exception { System.setProperty("earthclass", earthclass); Field instance = findSingletonStaticPrivateInstanceField(); instance.setAccessible(true); instance.set(null, null); }