Search in sources :

Example 1 with TestClass

use of org.junit.internal.runners.TestClass in project powermock by powermock.

the class PowerMockJUnit4MethodValidator method validateTestMethods.

/**
     * 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 = Whitebox.getInternalState(this, TEST_CLASS_FIELD, MethodValidator.class);
    Class<?> classUnderTest = Whitebox.getInternalState(testClass, CLASS_UNDER_TEST_FIELD);
    final List<Method> methods;
    if (TestCase.class.equals(classUnderTest.getSuperclass()) && !isStatic) {
        methods = getTestMethodsWithNoAnnotation(classUnderTest);
    } else {
        methods = testClass.getAnnotatedMethods(annotation);
    }
    List<Throwable> fErrors = Whitebox.getInternalState(this, ERRORS_FIELD, 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"));
    }
}
Also used : TestCase(junit.framework.TestCase) TestClass(org.junit.internal.runners.TestClass) Method(java.lang.reflect.Method)

Example 2 with TestClass

use of org.junit.internal.runners.TestClass in project powermock by powermock.

the class PowerMockJUnit4MethodValidator method validateInstanceMethods.

@SuppressWarnings("unchecked")
@Override
public void validateInstanceMethods() {
    validateTestMethods(After.class, false);
    validateTestMethods(Before.class, false);
    validateTestMethods(Test.class, false);
    TestClass testClass = Whitebox.getInternalState(this, TEST_CLASS_FIELD, MethodValidator.class);
    Class<?> classUnderTest = Whitebox.getInternalState(testClass, CLASS_UNDER_TEST_FIELD);
    List<Throwable> fErrors = Whitebox.getInternalState(this, ERRORS_FIELD, MethodValidator.class);
    List<Method> methods = getTestMethods(testClass, classUnderTest);
    if (methods.size() == 0)
        fErrors.add(new Exception("No runnable methods"));
}
Also used : TestClass(org.junit.internal.runners.TestClass) Method(java.lang.reflect.Method)

Aggregations

Method (java.lang.reflect.Method)2 TestClass (org.junit.internal.runners.TestClass)2 TestCase (junit.framework.TestCase)1