コード例 #1
0
 @Test
 public void callTheSpinAndWarmUpMethods() throws Exception {
   Method accessor = checkThatSingletonContainsStaticInstanceAccessorMethod(Earth.class);
   Earth earth = (Earth) accessor.invoke(null);
   earth.spin();
   earth.warmUp();
 }
コード例 #2
0
 @Test
 public void testThatAccessorAlwaysReturnsSameInstance() throws Exception {
   Method accessor = checkThatSingletonContainsStaticInstanceAccessorMethod(Earth.class);
   Earth earth1 = (Earth) accessor.invoke(null);
   Earth earth2 = (Earth) accessor.invoke(null);
   assertSame("Instances returned should be the same", earth1, earth2);
 }
コード例 #3
0
 @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()));
 }
コード例 #4
0
 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.
 }