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

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

源代码1 项目: sql-layer   文件: NamedParameterizedRunner.java
/**
 * Gets the parameterization
 * @return the parameterization collection
 * @throws Throwable if the annotation requirements are not met, or if there's an error in invoking
 * the class's "get parameterizations" method.
 */
private Collection<Parameterization> getParameterizations() throws Throwable
{
    TestClass cls = getTestClass();
    List<FrameworkMethod> methods = cls.getAnnotatedMethods(TestParameters.class);

    if (methods.size() != 1)
    {
        throw new Exception("class " + cls.getName() + " must have exactly 1 method annotated with "
            + TestParameters.class.getSimpleName() +"; found " + methods.size());
    }

    FrameworkMethod method = methods.get(0);
    checkParameterizationMethod(method);

    @SuppressWarnings("unchecked")
    Collection<Parameterization> ret = (Collection<Parameterization>) method.invokeExplosively(null);
    checkParameterizations(ret);
    return ret;
}
 
源代码2 项目: burst   文件: BurstJUnit4.java
static List<Runner> explode(Class<?> cls) throws InitializationError {
  checkNotNull(cls, "cls");

  TestClass testClass = new TestClass(cls);
  List<FrameworkMethod> testMethods = testClass.getAnnotatedMethods(Test.class);

  List<FrameworkMethod> burstMethods = new ArrayList<>(testMethods.size());
  for (FrameworkMethod testMethod : testMethods) {
    Method method = testMethod.getMethod();
    for (Enum<?>[] methodArgs : Burst.explodeArguments(method)) {
      burstMethods.add(new BurstMethod(method, methodArgs));
    }
  }

  TestConstructor constructor = BurstableConstructor.findSingle(cls);
  Enum<?>[][] constructorArgsList = Burst.explodeArguments(constructor);
  List<Runner> burstRunners = new ArrayList<>(constructorArgsList.length);
  for (Enum<?>[] constructorArgs : constructorArgsList) {
    burstRunners.add(new BurstRunner(cls, constructor, constructorArgs, burstMethods));
  }

  return unmodifiableList(burstRunners);
}
 
@Override
protected List<FrameworkMethod> findDataProviderMethods(List<TestClass> locations, String testMethodName,
        String useDataProviderValue) {
    List<FrameworkMethod> result = new ArrayList<FrameworkMethod>();

    for (TestClass location : locations) {
        List<FrameworkMethod> dataProviderMethods = location.getAnnotatedMethods(DataProvider.class);
        for (FrameworkMethod dataProviderMethod : dataProviderMethods) {
            if (dataProviderMethod.getName().startsWith(testMethodName)) {
                result.add(dataProviderMethod);
            }
        }
    }
    Collections.sort(result, new Comparator<FrameworkMethod>() {
        @Override
        public int compare(FrameworkMethod a, FrameworkMethod b) {
            return a.getName().compareTo(b.getName());
        }
    });
    return result;
}
 
private FrameworkMethod getParametersMethod(TestClass testClass) throws Exception {
  List<FrameworkMethod> methods = testClass.getAnnotatedMethods(Parameters.class);
  for (FrameworkMethod each : methods) {
    int modifiers = each.getMethod().getModifiers();
    if (Modifier.isStatic(modifiers) && Modifier.isPublic(modifiers))
      return each;
  }

  throw new Exception("No public static parameters method on class " + testClass.getName());
}
 
源代码5 项目: elk-reasoner   文件: PolySuite.java
private static FrameworkMethod getConfigMethod(TestClass testClass) {
  List<FrameworkMethod> methods = testClass.getAnnotatedMethods(Config.class);
  if (methods.isEmpty()) {
    throw new IllegalStateException("@" + Config.class.getSimpleName() + " method not found");
  }
  if (methods.size() > 1) {
    throw new IllegalStateException("Too many @" + Config.class.getSimpleName() + " methods");
  }
  FrameworkMethod method = methods.get(0);
  int modifiers = method.getMethod().getModifiers();
  if (!(Modifier.isStatic(modifiers) && Modifier.isPublic(modifiers))) {
    throw new IllegalStateException("@" + Config.class.getSimpleName() + " method \"" + method.getName() + "\" must be public static");
  }
  return method;
}
 
源代码6 项目: wildfly-core   文件: WildflyTestRunner.java
private FrameworkMethod findParametersMethod(final TestClass testClass) {
    final List<FrameworkMethod> methods = testClass.getAnnotatedMethods(Parameters.class);
    if (methods.size() > 1) {
        throw new RuntimeException(String.format("More than one method was annotated with @%s: %s",
                Parameters.class.getSimpleName(), methods));
    } else if (methods.isEmpty()) {
        return null;
    }
    final FrameworkMethod method = methods.get(0);
    if (method.isPublic() && method.isStatic() && Collection.class.isAssignableFrom(method.getReturnType())) {
        return method;
    }
    throw new RuntimeException(String.format("Method %s must be public, static and have a return type assignable to Collection<Object>.",
            method.getName()));
}
 
源代码7 项目: cougar   文件: ParameterizedMultiRunner.java
private FrameworkMethod getParametersMethod(TestClass testClass)
        throws Exception {
    List<FrameworkMethod> methods= testClass
            .getAnnotatedMethods(Parameters.class);
    for (FrameworkMethod each : methods) {
        int modifiers= each.getMethod().getModifiers();
        if (Modifier.isStatic(modifiers) && Modifier.isPublic(modifiers))
            return each;
    }

    throw new Exception("No public static parameters method on class "
            + testClass.getName());
}
 
protected FrameworkMethod findDataProviderMethod(TestClass location, String testMethodName, String useDataProviderValue) {
    List<FrameworkMethod> dataProviderMethods = location.getAnnotatedMethods(DataProvider.class);
    for (FrameworkMethod dataProviderMethod : dataProviderMethods) {
        if (UseDataProvider.DEFAULT_VALUE.equals(useDataProviderValue)) {
            if (isMatchingNameConvention(testMethodName, dataProviderMethod.getName())) {
                return dataProviderMethod;
            }
        } else if (dataProviderMethod.getName().equals(useDataProviderValue)) {
            return dataProviderMethod;
        }
    }
    return null;
}
 
源代码9 项目: 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;
}
 
源代码10 项目: tomee   文件: MetaRunner.java
/**
 * By default JUnit includes all methods annotated with @Test. This method only allows methods annotated with @Keys to be included in the list of methods to be run in a
 * TestCase. Any @Test methods will be ignored
 */
@Override
protected List<FrameworkMethod> computeTestMethods() {
    final TestClass testClass = getTestClass();
    return testClass.getAnnotatedMethods(MetaTest.class);
}
 
@Override
public Statement createAfterStatement(Statement originalStatement, TestClass testClass, Object target)
{
    final List<FrameworkMethod> afters = testClass.getAnnotatedMethods(After.class);
    return new TransactionAwareRunAfters(originalStatement, afters, target);
}