@Test public void checkThatOtherMethodsAreNonStatic() throws NoSuchMethodException { Method spin = Earth.class.getDeclaredMethod("spin"); assertFalse("Method spin() should not be static", Modifier.isStatic(spin.getModifiers())); Method warmUp = Earth.class.getDeclaredMethod("warmUp"); assertFalse("Method warmUp() should not be static", Modifier.isStatic(warmUp.getModifiers())); }
@Test public void checkEarthIsAbstract() throws NoSuchMethodException { assertTrue("Earth class should be abstract", Modifier.isAbstract(Earth.class.getModifiers())); assertTrue( "Earth class spin() method should be abstract", Modifier.isAbstract(Earth.class.getMethod("spin").getModifiers())); assertTrue( "Earth class warmUp() method should be abstract", Modifier.isAbstract(Earth.class.getMethod("warmUp").getModifiers())); }
@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())); }
@Test public void checkThatConstructorIsProtected() { for (Constructor<?> constr : Earth.class.getDeclaredConstructors()) { assertTrue( "Constructors should be protected to allow subclasses of singleton", Modifier.isProtected(constr.getModifiers())); } }
private Method checkThatSingletonContainsStaticInstanceAccessorMethod(Class clazz) { for (Method method : clazz.getDeclaredMethods()) { if (method.getReturnType() == clazz && method.getParameterTypes().length == 0) { assertTrue( "Method returning the Earth instance should be static", Modifier.isStatic(method.getModifiers())); return method; } } fail("No static accessor method found."); return null; // will never be called. }