@SuppressWarnings("unchecked") @Override public void validateInstanceMethods() { validateTestMethods(After.class, false); validateTestMethods(Before.class, false); validateTestMethods(Test.class, false); TestClass testClass = (TestClass) Whitebox.getInternalState(this, "fTestClass", MethodValidator.class); Class<?> classUnderTest = (Class<?>) Whitebox.getInternalState(testClass, "fClass"); List<Throwable> fErrors = (List<Throwable>) Whitebox.getInternalState(this, "fErrors", MethodValidator.class); List<Method> methods = getTestMethods(testClass, classUnderTest); if (methods.size() == 0) fErrors.add(new Exception("No runnable methods")); }
/** * This is a rip-off of the * {@link MethodValidator#validateInstanceMethods()} with the exception that * this method also searches for test methods if the class extends * {@link TestCase} and has methods that starts with test which are not * annotated. */ @SuppressWarnings("unchecked") private void validateTestMethods(Class<? extends Annotation> annotation, boolean isStatic) { TestClass testClass = (TestClass) Whitebox.getInternalState(this, "fTestClass", MethodValidator.class); Class<?> classUnderTest = (Class<?>) Whitebox.getInternalState(testClass, "fClass"); final List<Method> methods; if (TestCase.class.equals(classUnderTest.getSuperclass()) && !isStatic) { methods = getTestMethodsWithNoAnnotation(classUnderTest); } else { methods = testClass.getAnnotatedMethods(annotation); } List<Throwable> fErrors = (List<Throwable>) Whitebox.getInternalState(this, "fErrors", MethodValidator.class); for (Method each : methods) { if (Modifier.isStatic(each.getModifiers()) != isStatic) { String state = isStatic ? "should" : "should not"; fErrors.add(new Exception("Method " + each.getName() + "() " + state + " be static")); } if (!Modifier.isPublic(each.getDeclaringClass().getModifiers())) fErrors.add(new Exception("Class " + each.getDeclaringClass().getName() + " should be public")); if (!Modifier.isPublic(each.getModifiers())) fErrors.add(new Exception("Method " + each.getName() + " should be public")); if (each.getReturnType() != Void.TYPE) fErrors.add(new Exception("Method " + each.getName() + " should be void")); if (each.getParameterTypes().length != 0) fErrors.add(new Exception("Method " + each.getName() + " should have no parameters")); } }
protected void validate() throws InitializationError { MethodValidator methodValidator = new MethodValidator(fTestClass); methodValidator.validateMethodsForDefaultRunner(); methodValidator.assertValid(); }