org.junit.runners.model.TestClass#getAnnotatedFields()源码实例Demo

下面列出了org.junit.runners.model.TestClass#getAnnotatedFields() 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: wildfly-core   文件: WildflyTestRunner.java
private void doInject(TestClass klass, Object instance) {

        try {

            for (FrameworkField frameworkField : klass.getAnnotatedFields(Inject.class)) {
                Field field = frameworkField.getField();
                if ((instance == null && Modifier.isStatic(field.getModifiers()) ||
                        instance != null)) {//we want to do injection even on static fields before test run, so we make sure that client is correct for current state of server
                    field.setAccessible(true);
                    if (field.getType() == ManagementClient.class && controller.isStarted()) {
                        field.set(instance, controller.getClient());
                    } else if (field.getType() == ModelControllerClient.class && controller.isStarted()) {
                        field.set(instance, controller.getClient().getControllerClient());
                    } else if (field.getType() == ServerController.class) {
                        field.set(instance, controller);
                    }
                }
            }

        } catch (Exception e) {
            throw new RuntimeException("Failed to inject", e);
        }
    }
 
源代码2 项目: buck   文件: BuckBlockJUnit4ClassRunner.java
/**
 * @return {@code true} if the test class has any fields annotated with {@code Rule} whose type is
 *     {@link Timeout}.
 */
static boolean hasTimeoutRule(TestClass testClass) {
  // Many protected convenience methods in BlockJUnit4ClassRunner that are available in JUnit 4.11
  // such as getTestRules(Object) were not public until
  // https://github.com/junit-team/junit/commit/8782efa08abf5d47afdc16740678661443706740,
  // which appears to be JUnit 4.9. Because we allow users to use JUnit 4.7, we need to include a
  // custom implementation that is backwards compatible to JUnit 4.7.
  List<FrameworkField> fields = testClass.getAnnotatedFields(Rule.class);
  for (FrameworkField field : fields) {
    if (field.getField().getType().equals(Timeout.class)) {
      return true;
    }
  }

  return false;
}
 
源代码3 项目: ion-java   文件: Injected.java
private static Dimension[] findDimensions(TestClass testClass)
throws Throwable
{
    List<FrameworkField> fields =
        testClass.getAnnotatedFields(Inject.class);
    if (fields.isEmpty())
    {
        throw new Exception("No fields of " + testClass.getName()
                            + " have the @Inject annotation");
    }

    BeanInfo beanInfo = Introspector.getBeanInfo(testClass.getJavaClass());
    PropertyDescriptor[] descriptors = beanInfo.getPropertyDescriptors();

    Dimension[] dimensions = new Dimension[fields.size()];

    int i = 0;
    for (FrameworkField field : fields)
    {
        int modifiers = field.getField().getModifiers();
        if (! Modifier.isPublic(modifiers) || ! Modifier.isStatic(modifiers))
        {
            throw new Exception("@Inject " + testClass.getName() + '.'
                                + field.getField().getName()
                                + " must be public static");
        }

        Dimension dim = new Dimension();
        dim.property = field.getField().getAnnotation(Inject.class).value();
        dim.descriptor = findDescriptor(testClass, descriptors, field, dim.property);
        dim.values = (Object[]) field.get(null);
        dimensions[i++] = dim;
    }

    return dimensions;
}
 
源代码4 项目: wildfly-core   文件: WildflyTestRunner.java
private FrameworkField findParameterField(final TestClass testClass) {
    final List<FrameworkField> fields = testClass.getAnnotatedFields(Parameter.class);
    if (fields.size() > 1) {
        throw new RuntimeException(String.format("More than one field was annotated with @%s: %s",
                Parameter.class.getSimpleName(), fields));
    } else if (fields.isEmpty()) {
        throw new RuntimeException(String.format("If the @%s annotation is present on a method a field must be annotated with @%s",
                Parameters.class.getSimpleName(), Parameter.class.getSimpleName()));
    }
    return fields.get(0);
}
 
源代码5 项目: neodymium-library   文件: ParameterStatement.java
@SuppressWarnings("unchecked")
@Override
public List<Object> createIterationData(TestClass testClass, FrameworkMethod method) throws Exception
{
    List<FrameworkMethod> parametersMethods = testClass.getAnnotatedMethods(Parameters.class);
    Iterable<Object> parameter = null;

    for (FrameworkMethod parametersMethod : parametersMethods)
    {
        if (parametersMethod.isPublic() && parametersMethod.isStatic())
        {
            // take the first public static method. invoke it and use the result as parameter
            try
            {
                Object parametersResult = parametersMethod.invokeExplosively(null);
                if (parametersResult instanceof Iterable)
                {
                    parameter = (Iterable<Object>) parametersResult;
                }
                else if (parametersResult instanceof Object[])
                {
                    parameter = Arrays.asList((Object[]) parametersResult);
                }
                else
                {
                    String msg = MessageFormat.format("{0}.{1}() must return an Iterable of arrays.",
                                                      testClass.getJavaClass().getName(), parametersMethod.getName());
                    throw new Exception(msg);
                }
                break;
            }
            catch (Throwable e)
            {
                throw new RuntimeException(e);
            }
        }
    }

    if (!parametersMethods.isEmpty() && parameter == null)
    {
        throw new Exception("No public static parameters method on class " + testClass.getJavaClass().getCanonicalName());
    }

    List<FrameworkField> parameterFrameworkFields = testClass.getAnnotatedFields(Parameter.class);
    LOGGER.debug("Found " + parameterFrameworkFields.size() + " parameter fields");

    List<Object> iterations = new LinkedList<>();

    if (parameter != null)
    {
        int parameterSetCounter = 0;
        for (Object para : parameter)
        {
            Object[] p;
            if (para instanceof Object[])
            {
                p = (Object[]) para;
            }
            else
            {
                p = new Object[]
                {
                  para
                };
            }

            iterations.add(new ParameterStatementData(parameterSetCounter, p, parameterFrameworkFields));
            parameterSetCounter++;
        }
    }

    return iterations;
}