Java Code Examples for org.junit.runners.model.TestClass#getAnnotatedMethods()

The following examples show how to use org.junit.runners.model.TestClass#getAnnotatedMethods() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: NamedParameterizedRunner.java    From sql-layer with GNU Affero General Public License v3.0 6 votes vote down vote up
/**
 * 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;
}
 
Example 2
Source File: BurstJUnit4.java    From burst with Apache License 2.0 6 votes vote down vote up
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);
}
 
Example 3
Source File: DataProviderResolverAcceptanceTest.java    From junit-dataprovider with Apache License 2.0 6 votes vote down vote up
@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;
}
 
Example 4
Source File: SpringJUnit4ParameterizedClassRunner.java    From tds with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
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());
}
 
Example 5
Source File: PolySuite.java    From elk-reasoner with Apache License 2.0 5 votes vote down vote up
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;
}
 
Example 6
Source File: WildflyTestRunner.java    From wildfly-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
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()));
}
 
Example 7
Source File: ParameterizedMultiRunner.java    From cougar with Apache License 2.0 5 votes vote down vote up
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());
}
 
Example 8
Source File: DefaultDataProviderMethodResolver.java    From junit-dataprovider with Apache License 2.0 5 votes vote down vote up
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;
}
 
Example 9
Source File: ParameterStatement.java    From neodymium-library with MIT License 4 votes vote down vote up
@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;
}
 
Example 10
Source File: MetaRunner.java    From tomee with Apache License 2.0 4 votes vote down vote up
/**
 * 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);
}
 
Example 11
Source File: TransactionStatementDecoratorFactory.java    From deltaspike with Apache License 2.0 4 votes vote down vote up
@Override
public Statement createAfterStatement(Statement originalStatement, TestClass testClass, Object target)
{
    final List<FrameworkMethod> afters = testClass.getAnnotatedMethods(After.class);
    return new TransactionAwareRunAfters(originalStatement, afters, target);
}