/** * Returns the {@link FrameworkMethod} object for the given method in the provided class loader. */ private static FrameworkMethod getFrameworkMethodInClassLoader( FrameworkMethod method, ClassLoader classLoader) throws ClassNotFoundException, NoSuchMethodException { Method methodInClassLoader = getMethodInClassLoader(method.getMethod(), classLoader); if (methodInClassLoader.equals(method.getMethod())) { // The method was already loaded in the right class loader, return it as is. return method; } return new FrameworkMethod(methodInClassLoader); }
@Override public Statement apply(Statement base, Description description) { if (description.isEmpty()) { return base; } else { final Class<?> testClass = description.getTestClass(); final FrameworkMethod frameworkMethod; try { final Method method = testClass.getMethod(description.getMethodName()); frameworkMethod = new FrameworkMethod(method); } catch (NoSuchMethodException e) { throw new IllegalStateException(e); } return delegate.apply(base, frameworkMethod, testInstance); } } }
public static List<FrameworkMethod> computeTestMethodsForClass(final Class klass, final List<FrameworkMethod> defaultMethods) { List<FrameworkMethod> list = defaultMethods; String methodName = System.getProperty("test.method"); if (methodName == null) { LOG.info("No test.method specified. using default methods."); } else { LOG.info("Picked up test.method={}", methodName); try { list = Arrays.asList(new FrameworkMethod(klass.getMethod(methodName))); } catch (NoSuchMethodException nsme) { LOG.warn("{} does not have test.method={}. failing to default methods.", klass.getName(), methodName); } } return list; }
private void getInterfaceMethodsForAnnotations(Map<Class<? extends Annotation>, List<FrameworkMethod>> methodsForAnnotations, Class<?> clazz) { List<Class<?>> interfaces = Java8TestClass.getInterfaces(this.getJavaClass()); for (Class<?> eachInterface : interfaces) { for (Method eachMethod : MethodSorter.getDeclaredMethods(eachInterface)) { if (!Modifier.isAbstract(eachMethod.getModifiers())) { TestClass.addToAnnotationLists(new FrameworkMethod(eachMethod), methodsForAnnotations); } } } }
@Test public void getSpringTimeoutViaMetaAnnotation() throws Exception { SpringJUnit4ClassRunner runner = new SpringJUnit4ClassRunner(getClass()); long timeout = runner.getSpringTimeout(new FrameworkMethod(getClass().getDeclaredMethod( "springTimeoutWithMetaAnnotation"))); assertEquals(10, timeout); }
@Test public void getSpringTimeoutViaMetaAnnotationWithOverride() throws Exception { SpringJUnit4ClassRunner runner = new SpringJUnit4ClassRunner(getClass()); long timeout = runner.getSpringTimeout(new FrameworkMethod(getClass().getDeclaredMethod( "springTimeoutWithMetaAnnotationAndOverride"))); assertEquals(42, timeout); }
/** * Creates a {@code TestClass} wrapping {@code klass}. Each time this * constructor executes, the class is scanned for annotations, which can be * an expensive process (we hope in future JDK's it will not be.) Therefore, * try to share instances of {@code TestClass} where possible. */ public TestClass(Class<?> klass) { fClass = klass; if (klass != null && klass.getConstructors().length > 1) { throw new IllegalArgumentException( "Test class can only have one constructor"); } for (Class<?> eachClass : getSuperClasses(fClass)) { for (Method eachMethod : MethodSorter.getDeclaredMethods(eachClass)) { addToAnnotationLists(new FrameworkMethod(eachMethod), fMethodsForAnnotations); } for (Field eachField : eachClass.getDeclaredFields()) { addToAnnotationLists(new FrameworkField(eachField), fFieldsForAnnotations); } } }
private void getInterfaceMethodsForAnnotations(Map<Class<? extends Annotation>, List<FrameworkMethod>> methodsForAnnotations, Class<?> clazz) { List<Class<?>> interfaces = Java8TestClass.getInterfaces(this.getJavaClass()); for (Class<?> eachInterface : interfaces) { for (Method eachMethod : MethodSorter.getDeclaredMethods(eachInterface)) { if (!Modifier.isAbstract(eachMethod.getModifiers())) { TestClass.addToAnnotationLists(new FrameworkMethod(eachMethod), methodsForAnnotations); } } } }
protected void scanAnnotatedMembers(Map<Class<? extends Annotation>, List<FrameworkMethod>> methodsForAnnotations, Map<Class<? extends Annotation>, List<FrameworkField>> fieldsForAnnotations) { for (Class<?> eachClass : getSuperClasses(clazz)) { for (Method eachMethod : MethodSorter.getDeclaredMethods(eachClass)) { addToAnnotationLists(new FrameworkMethod(eachMethod), methodsForAnnotations); } // ensuring fields are sorted to make sure that entries are inserted // and read from fieldForAnnotations in a deterministic order for (Field eachField : getSortedDeclaredFields(eachClass)) { addToAnnotationLists(new FrameworkField(eachField), fieldsForAnnotations); } } }
final Statement statement = helperTestRunner.methodBlock(new FrameworkMethod(bootstrappedMethod));
/** * Returns the {@link FrameworkMethod} object for the given method in the provided class loader. */ private static FrameworkMethod getFrameworkMethodInClassLoader( FrameworkMethod method, ClassLoader classLoader) throws ClassNotFoundException, NoSuchMethodException { Method methodInClassLoader = getMethodInClassLoader(method.getMethod(), classLoader); if (methodInClassLoader.equals(method.getMethod())) { // The method was already loaded in the right class loader, return it as is. return method; } return new FrameworkMethod(methodInClassLoader); }
/** * Creates a {@code TestClass} wrapping {@code klass}. Each time this * constructor executes, the class is scanned for annotations, which can be * an expensive process (we hope in future JDK's it will not be.) Therefore, * try to share instances of {@code TestClass} where possible. */ public TestClass(Class<?> klass) { fClass = klass; if (klass != null && klass.getConstructors().length > 1) { throw new IllegalArgumentException( "Test class can only have one constructor"); } for (Class<?> eachClass : getSuperClasses(fClass)) { for (Method eachMethod : MethodSorter.getDeclaredMethods(eachClass)) { addToAnnotationLists(new FrameworkMethod(eachMethod), fMethodsForAnnotations); } for (Field eachField : eachClass.getDeclaredFields()) { addToAnnotationLists(new FrameworkField(eachField), fFieldsForAnnotations); } } }
@Override protected void scanAnnotatedMembers( Map<Class<? extends Annotation>, List<FrameworkMethod>> annotatedMethods, Map<Class<? extends Annotation>, List<FrameworkField>> annotatedFields) { ancestry().forEachOrdered(c -> { for (Method each : applicableMethodsOf(c)) { addToAnnotationLists(new FrameworkMethod(each), annotatedMethods); } for (Field each : applicableFieldsOf(c)) { addToAnnotationLists(new FrameworkField(each), annotatedFields); } }); }
private List<FrameworkMethod> getMethods(Class<?> cls, Class<? extends Annotation> a) { ArrayList<FrameworkMethod> methods = new ArrayList<FrameworkMethod>(); for (Method m : cls.getDeclaredMethods()) { Annotation result = m.getAnnotation(a); if (result != null) { methods.add(new FrameworkMethod(m)); } } return methods; }
private List<FrameworkMethod> getMethods(Class<?> cls, Class<? extends Annotation> a) { ArrayList<FrameworkMethod> methods = new ArrayList<FrameworkMethod>(); for (Method m : cls.getDeclaredMethods()) { Annotation result = m.getAnnotation(a); if (result != null) { methods.add(new FrameworkMethod(m)); } } return methods; }
@Override protected List<FrameworkMethod> computeTestMethods() { if (csvTestsProvider == null) { csvTestsProvider = CsvTestsProviderFactory.create(this); } List<FrameworkMethod> frameworkMethods = new ArrayList<FrameworkMethod>(); for (Method m : csvTestsProvider.getTestMethods()) { frameworkMethods.add(new FrameworkMethod(m)); } return frameworkMethods; }
protected List<FrameworkMethod> getAnnotatedModuleMethods(Module module, Class annotationClass) { List<FrameworkMethod> annotated = new ArrayList<FrameworkMethod>(); for (Method method : module.getClass().getMethods()) { if (method.getAnnotation(annotationClass) != null) { annotated.add(new FrameworkMethod(method)); } } return annotated; }
@Override protected void scanAnnotatedMembers( Map<Class<? extends Annotation>, List<FrameworkMethod>> annotatedMethods, Map<Class<? extends Annotation>, List<FrameworkField>> annotatedFields) { ancestry().forEachOrdered(c -> { for (Method each : applicableMethodsOf(c)) { addToAnnotationLists(new FrameworkMethod(each), annotatedMethods); } for (Field each : applicableFieldsOf(c)) { addToAnnotationLists(new FrameworkField(each), annotatedFields); } }); }
protected void scanAnnotatedMembers(Map<Class<? extends Annotation>, List<FrameworkMethod>> methodsForAnnotations, Map<Class<? extends Annotation>, List<FrameworkField>> fieldsForAnnotations) { for (Class<?> eachClass : getSuperClasses(clazz)) { for (Method eachMethod : MethodSorter.getDeclaredMethods(eachClass)) { addToAnnotationLists(new FrameworkMethod(eachMethod), methodsForAnnotations); } // ensuring fields are sorted to make sure that entries are inserted // and read from fieldForAnnotations in a deterministic order for (Field eachField : getSortedDeclaredFields(eachClass)) { addToAnnotationLists(new FrameworkField(eachField), fieldsForAnnotations); } } }
protected void scanAnnotatedMembers(Map<Class<? extends Annotation>, List<FrameworkMethod>> methodsForAnnotations, Map<Class<? extends Annotation>, List<FrameworkField>> fieldsForAnnotations) { for (Class<?> eachClass : getSuperClasses(clazz)) { for (Method eachMethod : MethodSorter.getDeclaredMethods(eachClass)) { addToAnnotationLists(new FrameworkMethod(eachMethod), methodsForAnnotations); } // ensuring fields are sorted to make sure that entries are inserted // and read from fieldForAnnotations in a deterministic order for (Field eachField : getSortedDeclaredFields(eachClass)) { addToAnnotationLists(new FrameworkField(eachField), fieldsForAnnotations); } } }