下面列出了org.junit.runners.model.FrameworkMethod#getMethod() 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
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);
}
private void testJVMBackendFrameworkMethod(final FrameworkMethod aFrameworkMethod, final RunNotifier aRunNotifier) {
if ("".equals(System.getProperty("BYTECODER_DISABLE_JVMTESTS", ""))) {
final TestClass testClass = getTestClass();
final Description theDescription = Description.createTestDescription(testClass.getJavaClass(), aFrameworkMethod.getName() + " JVM Target");
aRunNotifier.fireTestStarted(theDescription);
try {
// Simply invoke using reflection
final Object theInstance = testClass.getJavaClass().getDeclaredConstructor().newInstance();
final Method theMethod = aFrameworkMethod.getMethod();
theMethod.invoke(theInstance);
aRunNotifier.fireTestFinished(theDescription);
} catch (final Exception e) {
aRunNotifier.fireTestFailure(new Failure(theDescription, e));
}
}
}
@Override
protected void scanAnnotatedMembers(Map<Class<? extends Annotation>,List<FrameworkMethod>> methodsForAnnotations,
Map<Class<? extends Annotation>,List<FrameworkField>> fieldsForAnnotations)
{
super.scanAnnotatedMembers(methodsForAnnotations, fieldsForAnnotations);
for (Map.Entry<Class<? extends Annotation>,List<FrameworkMethod>> entry
: methodsForAnnotations.entrySet()) {
if (Test.class.equals(entry.getKey())) {
List<FrameworkMethod> methods = new ArrayList<>();
for (FrameworkMethod method : entry.getValue()) {
Method javaMethod = method.getMethod();
if (javaMethod.getParameterTypes().length > 0) {
methods.add(new BaratineFrameworkMethod(javaMethod));
}
else {
methods.add(method);
}
}
entry.setValue(methods);
}
}
}
private List<FrameworkMethod> explodeTestMethod(FrameworkMethod testMethod, Object data, DataProvider dataProvider) {
Method method = testMethod.getMethod();
List<Object[]> converted = dataConverter.convert(data, method.isVarArgs(), method.getParameterTypes(),
dataProvider);
if (converted.isEmpty()) {
throw new IllegalArgumentException("Could not create test methods using probably 'null' or 'empty' dataprovider");
}
int idx = 0;
List<FrameworkMethod> result = new ArrayList<FrameworkMethod>();
for (Object[] parameters : converted) {
result.add(new DataProviderFrameworkMethod(method, idx++, parameters, dataProvider.format(),
dataProvider.formatter()));
}
return result;
}
@VisibleForTesting
static List<NamedArgument> getNamedArguments( Statement base, FrameworkMethod method, Object target ) {
AccessibleObject constructorOrMethod = method.getMethod();
List<Object> arguments = Collections.emptyList();
if( DATAPROVIDER_FRAMEWORK_METHOD.equals( method.getClass().getCanonicalName() ) ) {
arguments = getArgumentsFrom( method, "parameters" );
}
if( JUNITPARAMS_STATEMENT.equals( base.getClass().getCanonicalName() ) ) {
arguments = getArgumentsFrom( base, "params" );
}
if( isParameterizedTest( target ) ) {
Constructor<?> constructor = getOnlyConstructor( target.getClass() );
constructorOrMethod = constructor;
arguments = getArgumentsFrom( constructor, target );
}
return ParameterNameUtil.mapArgumentsWithParameterNames( constructorOrMethod, arguments );
}
/**
* Checks the parameterization method for correctness.
* @param frameworkMethod the method
* @throws Exception if the annotation requirements are not met
*/
private static void checkParameterizationMethod(FrameworkMethod frameworkMethod) throws Exception
{
final Method method = frameworkMethod.getMethod();
if (method.getParameterTypes().length != 0)
{
throw new Exception(complainingThat(method, "must take no arguments"));
}
final int modifiers = frameworkMethod.getMethod().getModifiers();
if (! Modifier.isPublic(modifiers))
{
throw new Exception(complainingThat(method, "must be public"));
}
if (! Modifier.isStatic(modifiers))
{
throw new Exception(complainingThat(method, "must be static"));
}
final Type genericRet = method.getGenericReturnType();
final String mustReturnCorrectly = "must return Collection of " + Parameterization.class;
if (! (genericRet instanceof ParameterizedType))
{
throw new Exception(complainingThat(method, mustReturnCorrectly));
}
final ParameterizedType ret = (ParameterizedType) genericRet;
if (!(ret.getRawType() instanceof Class) && Collection.class.isAssignableFrom((Class)ret.getRawType()))
{
throw new Exception(complainingThat(method, mustReturnCorrectly));
}
if (ret.getActualTypeArguments().length != 1)
{
throw new Exception(complainingThat(method, mustReturnCorrectly + "; raw Collection is not allowed"));
}
if (!ret.getActualTypeArguments()[0].equals(Parameterization.class))
{
throw new Exception(complainingThat(method, mustReturnCorrectly));
}
}
/**
* Wrap the supplied {@link Statement} with a {@code RunAfterTestMethodCallbacks} statement.
* @see RunAfterTestMethodCallbacks
*/
private Statement withAfterTestMethodCallbacks(Statement statement, FrameworkMethod frameworkMethod,
Object testInstance, TestContextManager testContextManager) {
return new RunAfterTestMethodCallbacks(
statement, testInstance, frameworkMethod.getMethod(), testContextManager);
}
private static void validateParameterValueSource(
FrameworkMethod eachTestMethod, List<Throwable> errors) {
Method reflectMethod = eachTestMethod.getMethod();
long numOfParamsWithValueRequest =
Arrays.stream(reflectMethod.getParameters())
.filter(parameter -> parameter.isAnnotationPresent(FromParameterValueSource.class))
.count();
if (numOfParamsWithValueRequest > 0) {
List<ParameterValueSource> parameterValueSources = new ArrayList<>();
if (reflectMethod.isAnnotationPresent(ParameterValueSource.class)) {
parameterValueSources.add(reflectMethod.getDeclaredAnnotation(ParameterValueSource.class));
}
if (reflectMethod.isAnnotationPresent(ParameterValueSourceSet.class)) {
Collections.addAll(
parameterValueSources,
reflectMethod.getDeclaredAnnotation(ParameterValueSourceSet.class).value());
}
for (ParameterValueSource parameterValueSource : parameterValueSources) {
int valueSourceLength = parameterValueSource.value().length;
if (valueSourceLength != numOfParamsWithValueRequest) {
errors.add(
new Exception(
String.format(
"Parameter value source bundle (%s) and @FromParameterValueSource-annotated"
+ " parameters in Method (%s) mismatch in length.",
parameterValueSource, eachTestMethod)));
}
}
}
}
protected void invokeTestMethod(FrameworkMethod fMethod, Object test, TestContext context) throws InvocationTargetException, IllegalAccessException {
Method method = fMethod.getMethod();
Class<?>[] paramTypes = method.getParameterTypes();
if (paramTypes.length == 0) {
method.invoke(test);
} else {
method.invoke(test, context);
}
}
/**
* Wrap the supplied {@link Statement} with a {@code RunBeforeTestMethodCallbacks} statement.
* @see RunBeforeTestMethodCallbacks
*/
private Statement withBeforeTestMethodCallbacks(Statement statement, FrameworkMethod frameworkMethod,
Object testInstance, TestContextManager testContextManager) {
return new RunBeforeTestMethodCallbacks(
statement, testInstance, frameworkMethod.getMethod(), testContextManager);
}
/**
* Apply <em>instance-level</em> and <em>method-level</em> features of
* the <em>Spring TestContext Framework</em> to the supplied {@code base}
* statement.
* <p>Specifically, this method invokes the
* {@link TestContextManager#prepareTestInstance prepareTestInstance()},
* {@link TestContextManager#beforeTestMethod beforeTestMethod()}, and
* {@link TestContextManager#afterTestMethod afterTestMethod()} methods
* on the {@code TestContextManager}, potentially with Spring timeouts
* and repetitions.
* <p>In addition, this method checks whether the test is enabled in
* the current execution environment. This prevents methods with a
* non-matching {@code @IfProfileValue} annotation from running altogether,
* even skipping the execution of {@code prepareTestInstance()} methods
* in {@code TestExecutionListeners}.
* @param base the base {@code Statement} that this rule should be applied to
* @param frameworkMethod the method which is about to be invoked on the test instance
* @param testInstance the current test instance
* @return a statement that wraps the supplied {@code base} with instance-level
* and method-level features of the Spring TestContext Framework
* @see #withBeforeTestMethodCallbacks
* @see #withAfterTestMethodCallbacks
* @see #withPotentialRepeat
* @see #withPotentialTimeout
* @see #withTestInstancePreparation
* @see #withProfileValueCheck
*/
@Override
public Statement apply(Statement base, FrameworkMethod frameworkMethod, Object testInstance) {
Method testMethod = frameworkMethod.getMethod();
if (logger.isDebugEnabled()) {
logger.debug("Applying SpringMethodRule to test method [" + testMethod + "]");
}
Class<?> testClass = testInstance.getClass();
TestContextManager testContextManager = SpringClassRule.getTestContextManager(testClass);
Statement statement = base;
statement = withBeforeTestMethodCallbacks(statement, testMethod, testInstance, testContextManager);
statement = withAfterTestMethodCallbacks(statement, testMethod, testInstance, testContextManager);
statement = withTestInstancePreparation(statement, testInstance, testContextManager);
statement = withPotentialRepeat(statement, testMethod, testInstance);
statement = withPotentialTimeout(statement, testMethod, testInstance);
statement = withProfileValueCheck(statement, testMethod, testInstance);
return statement;
}
/**
* Wrap the {@link Statement} returned by the parent implementation with a
* {@code RunBeforeTestMethodCallbacks} statement, thus preserving the
* default functionality while adding support for the Spring TestContext
* Framework.
* @see RunBeforeTestMethodCallbacks
*/
@Override
protected Statement withBefores(FrameworkMethod frameworkMethod, Object testInstance, Statement statement) {
Statement junitBefores = super.withBefores(frameworkMethod, testInstance, statement);
return new RunBeforeTestMethodCallbacks(junitBefores, testInstance, frameworkMethod.getMethod(),
getTestContextManager());
}
/**
* Wraps the {@link Statement} returned by the parent implementation with a
* {@link RunBeforeTestMethodCallbacks} statement, thus preserving the
* default functionality but adding support for the Spring TestContext
* Framework.
*
* @see RunBeforeTestMethodCallbacks
*/
@Override
protected Statement withBefores(FrameworkMethod frameworkMethod, Object testInstance, Statement statement) {
Statement junitBefores = super.withBefores(frameworkMethod, testInstance, statement);
return new RunBeforeTestMethodCallbacks(junitBefores, testInstance, frameworkMethod.getMethod(),
getTestContextManager());
}
/**
* Wraps the {@link Statement} returned by the parent implementation with a
* {@link RunAfterTestMethodCallbacks} statement, thus preserving the
* default functionality but adding support for the Spring TestContext
* Framework.
*
* @see RunAfterTestMethodCallbacks
*/
@Override
protected Statement withAfters(FrameworkMethod frameworkMethod, Object testInstance, Statement statement) {
Statement junitAfters = super.withAfters(frameworkMethod, testInstance, statement);
return new RunAfterTestMethodCallbacks(junitAfters, testInstance, frameworkMethod.getMethod(),
getTestContextManager());
}
/**
* Return {@code true} if {@link Ignore @Ignore} is present for the supplied
* {@linkplain FrameworkMethod test method} or if the test method is disabled
* via {@code @IfProfileValue}.
* @see ProfileValueUtils#isTestEnabledInThisEnvironment(Method, Class)
*/
protected boolean isTestMethodIgnored(FrameworkMethod frameworkMethod) {
Method method = frameworkMethod.getMethod();
return (method.isAnnotationPresent(Ignore.class) ||
!ProfileValueUtils.isTestEnabledInThisEnvironment(method, getTestClass().getJavaClass()));
}
/**
* Wrap the supplied {@link Statement} with a {@code RunAfterTestExecutionCallbacks}
* statement, thus preserving the default functionality while adding support for the
* Spring TestContext Framework.
* @see RunAfterTestExecutionCallbacks
*/
protected Statement withAfterTestExecutionCallbacks(FrameworkMethod frameworkMethod, Object testInstance, Statement statement) {
return new RunAfterTestExecutionCallbacks(statement, testInstance, frameworkMethod.getMethod(), getTestContextManager());
}
/**
* Return {@code true} if {@link Ignore @Ignore} is present for the supplied
* {@linkplain FrameworkMethod test method} or if the test method is disabled
* via {@code @IfProfileValue}.
* @see ProfileValueUtils#isTestEnabledInThisEnvironment(Method, Class)
*/
protected boolean isTestMethodIgnored(FrameworkMethod frameworkMethod) {
Method method = frameworkMethod.getMethod();
return (method.isAnnotationPresent(Ignore.class) ||
!ProfileValueUtils.isTestEnabledInThisEnvironment(method, getTestClass().getJavaClass()));
}
/**
* Wrap the supplied {@link Statement} with a {@code RunAfterTestExecutionCallbacks}
* statement, thus preserving the default functionality while adding support for the
* Spring TestContext Framework.
* @see RunAfterTestExecutionCallbacks
*/
protected Statement withAfterTestExecutionCallbacks(FrameworkMethod frameworkMethod, Object testInstance, Statement statement) {
return new RunAfterTestExecutionCallbacks(statement, testInstance, frameworkMethod.getMethod(), getTestContextManager());
}
/**
* Wrap the {@link Statement} returned by the parent implementation with a
* {@code RunAfterTestMethodCallbacks} statement, thus preserving the
* default functionality while adding support for the Spring TestContext
* Framework.
* @see RunAfterTestMethodCallbacks
*/
@Override
protected Statement withAfters(FrameworkMethod frameworkMethod, Object testInstance, Statement statement) {
Statement junitAfters = super.withAfters(frameworkMethod, testInstance, statement);
return new RunAfterTestMethodCallbacks(junitAfters, testInstance, frameworkMethod.getMethod(), getTestContextManager());
}
/**
* Wrap the supplied {@link Statement} with a {@code SpringRepeat} statement.
* <p>Supports Spring's {@link org.springframework.test.annotation.Repeat @Repeat}
* annotation.
* @see TestAnnotationUtils#getRepeatCount(Method)
* @see SpringRepeat
*/
protected Statement withPotentialRepeat(FrameworkMethod frameworkMethod, Object testInstance, Statement next) {
return new SpringRepeat(next, frameworkMethod.getMethod());
}