下面列出了org.junit.runners.model.FrameworkMethod#validatePublicVoid() 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
protected void validatePublicVoidNoArgMethods(Class<? extends Annotation> annotation, boolean isStatic, List<Throwable> errors) {
if (annotation == Test.class || annotation == Before.class || annotation == After.class ||
annotation == BeforeClass.class || annotation == AfterClass.class) {
List<FrameworkMethod> fMethods = getTestClass().getAnnotatedMethods(annotation);
for (FrameworkMethod fMethod : fMethods) {
fMethod.validatePublicVoid(isStatic, errors);
try {
validateTestMethod(fMethod);
} catch (Exception e) {
errors.add(e);
}
}
} else {
super.validatePublicVoidNoArgMethods(annotation, isStatic, errors);
}
}
/**
* Checks if the given {@code testMethod} is a valid test method depending on the dataprovider relevant annotation
* {@code @}{@link DataProvider} and {@code @}{@link UseDataProvider}. Adds {@link Exception}s to {@code errors} for
* each invalid property. A normal test method must be is public, void instance method with no arguments. A data
* provider test method must be is public and void instance method but have a least one argument.
*
* @param testMethod the test method to be validated
* @param errors to be "returned" and thrown as {@link InitializationError}
* @throws IllegalArgumentException if given {@code errors} is {@code null}
*/
public void validateTestMethod(FrameworkMethod testMethod, List<Throwable> errors) {
checkNotNull(testMethod, "testMethod must not be null");
checkNotNull(errors, "errors must not be null");
UseDataProvider useDataProvider = testMethod.getAnnotation(UseDataProvider.class);
DataProvider dataProvider = testMethod.getAnnotation(DataProvider.class);
if (useDataProvider != null && dataProvider != null) {
errors.add(new Exception(String.format("Method %s() should either have @%s or @%s annotation", testMethod
.getName(), useDataProvider.getClass().getSimpleName(), dataProvider.getClass().getSimpleName())));
} else if (useDataProvider == null && dataProvider == null) {
testMethod.validatePublicVoidNoArg(false, errors);
} else {
testMethod.validatePublicVoid(false, errors);
if (testMethod.getMethod().getParameterTypes().length <= 0) {
errors.add(new Exception(String.format("Method %s() must have at least one argument for dataprovider",
testMethod.getName())));
}
}
}
private void validateFuzzMethods(List<Throwable> errors) {
for (FrameworkMethod method : getTestClass().getAnnotatedMethods(Fuzz.class)) {
method.validatePublicVoid(false, errors);
if (method.getAnnotation(Property.class) != null) {
errors.add(new Exception("Method " + method.getName() +
" cannot have both @Property and @Fuzz annotations"));
}
}
}
private void validatePublicStaticVoidMethods(Class<? extends Annotation> annotation,
List<Throwable> errors) {
final List<FrameworkMethod> methods = getTestClass().getAnnotatedMethods(annotation);
for (FrameworkMethod eachTestMethod : methods) {
eachTestMethod.validatePublicVoid(true, errors);
}
}
private void validatePublicStaticVoidMethods(Class<? extends Annotation> annotation,
List<Throwable> errors) {
final List<FrameworkMethod> methods = getTestClass().getAnnotatedMethods(annotation);
for (FrameworkMethod eachTestMethod : methods) {
eachTestMethod.validatePublicVoid(true, errors);
}
}
/**
* In replacement of {@link BlockJUnit4ClassRunner#validatePublicVoidNoArgMethods} for @Test
* method signature validation.
*/
private void validatePublicVoidMethodsWithInjectableArgs(
Class<? extends Annotation> annotation, boolean isStatic, List<Throwable> errors) {
List<FrameworkMethod> methods = getTestClass().getAnnotatedMethods(annotation);
for (FrameworkMethod eachTestMethod : methods) {
eachTestMethod.validatePublicVoid(isStatic, errors);
validateInjectableParameters(eachTestMethod, errors);
validateParameterValueSource(eachTestMethod, errors);
}
}
@Override
protected void validatePublicVoidNoArgMethods(Class<? extends Annotation> annotation, boolean isStatic, List<Throwable> errors) {
for (FrameworkMethod testMethod : getTestClass().getAnnotatedMethods(annotation)) {
testMethod.validatePublicVoid(isStatic, errors);
}
}
/**
* Marks validation errors in errors if:
*
* <ul>
* <li>Any method marked by @Test is a not a public non-static void method.
* <li>Any method marked by @Test does not have exact args ({@link EndToEndTestDescriptor},
* {@link ProcessResult})
* </ul>
*/
private void validateTestMethods(List<Throwable> errors) {
List<FrameworkMethod> methods = getTestClass().getAnnotatedMethods(Test.class);
for (FrameworkMethod testMethod : methods) {
testMethod.validatePublicVoid(false, errors);
validateTestMethodArgs(testMethod, errors);
}
}