下面列出了org.junit.runners.model.TestClass#getAnnotatedMethods() 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* 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;
}
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());
}
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;
}
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()));
}
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;
}
@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;
}
/**
* 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);
}