@Test public void callTheSpinAndWarmUpMethods() throws Exception { Method accessor = checkThatSingletonContainsStaticInstanceAccessorMethod(Earth.class); Earth earth = (Earth) accessor.invoke(null); earth.spin(); earth.warmUp(); }
@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); }
private void addClass(Class<?> c) { if (classes.add(c)) { if (c.getSuperclass() != null) { addClass(c.getSuperclass()); } for (Class<?> sc : c.getInterfaces()) { addClass(sc); } for (Class<?> dc : c.getDeclaredClasses()) { addClass(dc); } for (Method m : c.getDeclaredMethods()) { addClass(m.getReturnType()); for (Class<?> p : m.getParameterTypes()) { addClass(p); } } if (c != void.class && dimensions(c) < 2) { Class<?> arrayClass = Array.newInstance(c, 0).getClass(); arrayClasses.put(c, arrayClass); addClass(arrayClass); } } }
@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())); }
public static <T> Class<T> tellMeTheTypeOfThisThing() throws NoSuchMethodException { Method method = InferGenericTypeForAStaticMethodTest.class.getDeclaredMethod( "tellMeTheTypeOfThisThing", null); ParameterizedType type = (ParameterizedType) method.getGenericReturnType(); TypeVariableImpl typeImpl = (TypeVariableImpl) type.getActualTypeArguments()[0]; System.out.println(typeImpl); return null; }
private Method findPublicVoidMethod(Class<?> aClass, String methodName) { for (Method method : aClass.getDeclaredMethods()) { if (isPublic(method.getModifiers()) && method.getReturnType() == void.class && methodName.equals(method.getName())) { return method; } } return null; }
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. }
@Before public void setUp() { try { final Method m = MiniProjet.class.getDeclaredMethod("loadConfigFile", Path.class); m.setAccessible(true); m.invoke(null, Paths.get(MiniProjet.FOLDER, MiniProjet.CONFIG)); } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException | NoSuchMethodException | SecurityException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
private void invalidValueHelper(Object obj, String methodName, int i) { try { Method m = obj.getClass().getMethod(methodName, int.class); m.invoke(obj, i); fail("Didn't cause an invalid value exception."); } catch (NoSuchMethodException | SecurityException e) { System.err.println("Error when getting the method. Uh oh!"); e.printStackTrace(); fail(); } catch (IllegalAccessException | IllegalArgumentException e) { System.err.println("Error when invoking the method. Uh oh!"); e.printStackTrace(); fail(); } catch (InvocationTargetException e) { assertEquals(JsonTypeException.class, e.getTargetException().getClass()); } }
private void reportTestAsNotApplicableInCurrentTestRun(Method method) { Class<?> testClass = method.getDeclaringClass(); Description testDescription = Description.createTestDescription(testClass, method.getName()); runNotifier.fireTestAssumptionFailed(new Failure(testDescription, NOT_APPLICABLE)); }
private boolean isTestNotApplicableInCurrentTestRun( Class<? extends Annotation> testAnnotation, Method testMethod) { return (testAnnotation == null || testMethod.getAnnotation(testAnnotation) != null) && new TestFilter(coverageMap).shouldIgnoreTestInCurrentTestRun(testMethod); }