下面列出了org.junit.runners.model.FrameworkMethod#getAnnotation() 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
protected Statement withBefores(FrameworkMethod method, Object target, Statement statement) {
if (method.getAnnotation(Ignore.class) == null && method.getAnnotation(CmmnDeployment.class) != null) {
List<FrameworkMethod> befores = getTestClass().getAnnotatedMethods(Before.class);
return new Statement() {
@Override
public void evaluate() throws Throwable {
for (FrameworkMethod before : befores) {
before.invokeExplosively(target);
}
deploymentId = deployCmmnDefinition(method);
statement.evaluate();
}
};
} else {
return super.withBefores(method, target, statement);
}
}
@Override
protected void runChild(FrameworkMethod method, RunNotifier notifier) {
Description description = describeChild(method);
// check Ignore first
if (method.getAnnotation(Ignore.class) != null) {
notify("@Ignore", description);
notifier.fireTestIgnored(description);
} else if (localTestsEnabled && method.getAnnotation(RemoteOnly.class) != null) {
// if running in local mode and @RemoteOnly annotation exists, ignore
notify("Skip @RemoteOnly", description);
notifier.fireTestIgnored(description);
} else if (remoteTestsEnabled && method.getAnnotation(LocalOnly.class) != null) {
// if running in remote mode and @LocalOnly annotation exists, ignore
notify("Skip @LocalOnly", description);
notifier.fireTestIgnored(description);
} else {
// default is run in either mode
notify("Test[" + mode + "]", description);
super.runChild(method, notifier);
}
}
/**
* Filter and sort
* @param fms The FrameworkMethods
* @param isStatic Filter static
* @return The filtered and sorted FrameworkMethods
* @exception Exception If an order definition is incorrect
*/
private Collection<FrameworkMethod> filterAndSort(List<FrameworkMethod> fms, boolean isStatic) throws Exception
{
SortedMap<Integer, FrameworkMethod> m = new TreeMap<>();
for (FrameworkMethod fm : fms)
{
SecurityActions.setAccessible(fm.getMethod());
if (Modifier.isStatic(fm.getMethod().getModifiers()) == isStatic)
{
Deployment deployment = (Deployment)fm.getAnnotation(Deployment.class);
int order = deployment.order();
if (order <= 0 || m.containsKey(Integer.valueOf(order)))
throw new Exception("Incorrect order definition '" + order + "' on " +
fm.getDeclaringClass().getName() + "#" + fm.getName());
m.put(Integer.valueOf(order), fm);
}
}
return m.values();
}
@Override
protected void runChild(final FrameworkMethod method, RunNotifier notifier) {
Description description = describeChild(method);
if (method.getAnnotation(Ignore.class) != null) {
notifier.fireTestIgnored(description);
} else {
EachTestNotifier eachNotifier = new EachTestNotifier(notifier, description);
eachNotifier.fireTestStarted();
try {
runTestMethod(method, eachNotifier);
} finally {
eachNotifier.fireTestFinished();
}
}
}
/**
* Flags an error if you have annotated a test with both @Test and @Keys. Any method annotated with @Test will be ignored anyways.
*/
@Override
protected void collectInitializationErrors(final List<Throwable> errors) {
super.collectInitializationErrors(errors);
final List<FrameworkMethod> methodsAnnotatedWithKeys = getTestClass().getAnnotatedMethods(Keys.class);
for (final FrameworkMethod frameworkMethod : methodsAnnotatedWithKeys) {
if (frameworkMethod.getAnnotation(Test.class) != null) {
final String gripe = "The method " + frameworkMethod.getName() + "() can only be annotated with @Keys";
errors.add(new Exception(gripe));
}
}
}
@Override
protected Description describeChild(FrameworkMethod method) {
if (method.getAnnotation(Repeat.class) != null &&
method.getAnnotation(Ignore.class) == null) {
return describeRepeatTest(method);
}
return super.describeChild(method);
}
@Override public Statement apply(Statement statement, FrameworkMethod method, Object o) {
Enqueue enqueue = method.getAnnotation(Enqueue.class);
if (enqueue != null) {
if (!(o instanceof BaseTest)) {
throw new RuntimeException("Test class must extend "
+ BaseTest.class.getName()
+ "when using @"
+ Enqueue.class.getSimpleName());
}
List<TestResponse> responses = new ArrayList<>(enqueueToTestResponse(enqueue));
((BaseTest) o).setResponseQueue(responses);
}
return statement;
}
/**
* Sorts the given array of methods in dependencies order.
*
* @param methods the methods to sort.
*/
private static void sortDependantTestsLast(final FrameworkMethod[] methods) {
final Set<String> dependencies = new HashSet<>();
int retryCount = methods.length;
for (int i=methods.length-1; --i>=0;) {
final FrameworkMethod method = methods[i];
final DependsOnMethod depend = method.getAnnotation(DependsOnMethod.class);
if (depend != null) {
dependencies.addAll(Arrays.asList(depend.value()));
for (int j=methods.length; --j>i;) {
if (dependencies.contains(methods[j].getName())) {
/*
* Found a method j which is a dependency of i. Move i after j.
* The order of other methods relative to j is left unchanged.
*
* As a result of this move, maybe some methods between i and j
* need to be revisited. We should restart the iteration from j.
* Over unlimited retries could cause an infinite loop, so we
* arbitrarily limit the amount of time we retry.
*/
System.arraycopy(methods, i+1, methods, i, j-i);
methods[j] = method;
if (--retryCount >= 0) {
i = j; // Revisit the methods between i and j.
}
break;
}
}
dependencies.clear();
}
}
}
@Override
public Statement apply(Statement base, FrameworkMethod method, Object target) {
return new Statement() {
@Override
public void evaluate() throws Throwable {
Retryable retryable = method.getAnnotation(Retryable.class);
if (retryable != null) {
for (int i = 1; i <= retryable.maxAttempts(); i++) {
try {
base.evaluate();
break;
} catch (Throwable t) {
if (retryable.value().isAssignableFrom(t.getClass())) {
logger.warn("Attempt {}/{} failed.", i, retryable.maxAttempts(), t);
if (i == retryable.maxAttempts()) {
throw t;
} else {
if (retryable.delay() > 0) {
sleepUninterruptibly(retryable.delay());
}
}
} else {
throw t;
}
}
}
} else {
base.evaluate();
}
}
};
}
private String getSampleName(FrameworkMethod method) {
String sampleName;
UsesSample annotation = method.getAnnotation(UsesSample.class);
if (annotation == null) {
sampleName = defaultSampleName;
} else {
sampleName = annotation.value();
}
return sampleName;
}
@Override
public Statement methodInvoker(final FrameworkMethod method,
Object test) {
final Statement singleTryStatement = super.methodInvoker(method, test);
return new Statement() {
/**
* Evaluate the statement.
* We attempt several runs for the test, at most MAX_ATTEMPTS.
* if one attempt succeeds, we succeed, if all attempts fail, we
* fail with the reason corresponding to the last attempt
*/
@Override
public void evaluate() throws Throwable {
Throwable failureReason = null;
final Retry retry = method.getAnnotation(Retry.class);
if (retry == null) {
// Do a single test run attempt.
singleTryStatement.evaluate();
} else {
final int numRetries = retry.value();
for (int i = 0; i < numRetries; ++i) {
try {
// Do a single test run attempt.
singleTryStatement.evaluate();
// Attempt succeeded, stop evaluation here.
return;
} catch (Throwable t) {
// Attempt failed, store the reason.
failureReason = t;
}
}
// All attempts failed.
throw failureReason;
}
}
};
}
@Override
public void runChild(FrameworkMethod method, RunNotifier notifier) {
MinVersion methodCondition = method.getAnnotation(MinVersion.class);
MinVersion classCondition = this.getTestClass().getJavaClass().getAnnotation(MinVersion.class);
String versionStr = VersionInfo.getVersion();
int version = VersionUtil.encodeVersion(versionStr);
if ( (methodCondition == null || version >= VersionUtil.encodeVersion(methodCondition.value()))
&& (classCondition == null || version >= VersionUtil.encodeVersion(classCondition.value()))) {
super.runChild(method, notifier);
} else {
notifier.fireTestIgnored(describeChild(method));
}
}
@Override
protected void runChild(FrameworkMethod method, RunNotifier notifier) {
String deploymentId = null;
if (method.getAnnotation(Ignore.class) == null && method.getAnnotation(AppDeployment.class) != null) {
deploymentId = deployAppDefinition(method);
}
super.runChild(method, notifier);
if (deploymentId != null) {
deleteDeployment(deploymentId);
}
assertDatabaseEmpty(method);
}
private JdkSuppress getJdkSuppress(FrameworkMethod child) {
JdkSuppress jdkSuppress = child.getAnnotation(JdkSuppress.class);
return jdkSuppress != null ? jdkSuppress : getTestClass().getAnnotation(JdkSuppress.class);
}
public Statement apply(Statement base, FrameworkMethod method, Object target) {
return !ClassInjector.UsingReflection.isAvailable() && method.getAnnotation(Enforce.class) != null
? new NoOpStatement()
: base;
}
@Override
protected Statement methodInvoker(FrameworkMethod method, Object test) {
Statement next = super.methodInvoker(method, test);
return new Statement() {
@Override
public void evaluate() throws Throwable {
LoginPage loginPage = injector.getInstance(LoginPage.class);
Login methodLogin = method.getAnnotation(Login.class);
// determine whether test should login first or not
// first check test method, then walk up hierarchy at class level only
if(methodLogin != null ) {
if(!methodLogin.disable()) {
loginPage.login();
}
} else {
Class<?> clazz = test.getClass();
while (clazz != null) {
Login classLogin = clazz.getAnnotation(Login.class);
if (classLogin != null) {
if (!classLogin.disable()) {
loginPage.login();
}
break;
}
clazz = clazz.getSuperclass();
}
}
try {
if (LocalDriver.isSauceLabsMode()) {
logger.info("SauceOnDemandSessionID=" + ((RemoteWebDriver) LocalDriver.getDriver()).getSessionId().toString());
}
next.evaluate();
outputConsoleLogs();
LocalDriver.executeSauce("job-result=passed");
} catch (Exception e) {
LocalDriver.executeSauce("job-result=failed");
writeScreenShotCause(e, test, method);
outputConsoleLogs();
throw e;
}
WebDriver driver = injector.getInstance(WebDriver.class);
driver.close();
driver.quit();
}
};
}
private static boolean hasConditionalIgnoreAnnotation(FrameworkMethod method) {
return method.getAnnotation(ConditionalIgnore.class) != null;
}
@Override public Statement methodBlock(FrameworkMethod method) {
if (method.getAnnotation(Fuzz.class) != null) {
return new FuzzStatement(method, getTestClass(), generatorRepository);
}
return super.methodBlock(method);
}
/**
* Get the {@code exception} that the supplied {@linkplain FrameworkMethod
* test method} is expected to throw.
* <p>Supports JUnit's {@link Test#expected() @Test(expected=...)} annotation.
* <p>Can be overridden by subclasses.
* @return the expected exception, or {@code null} if none was specified
*/
protected Class<? extends Throwable> getExpectedException(FrameworkMethod frameworkMethod) {
Test test = frameworkMethod.getAnnotation(Test.class);
return (test != null && test.expected() != Test.None.class ? test.expected() : null);
}
/**
* Returns if not yet implemented.
* @param method the method
* @return if not yet implemented
*/
protected boolean isNotYetImplemented(final FrameworkMethod method) {
final NotYetImplemented notYetImplementedBrowsers = method.getAnnotation(NotYetImplemented.class);
return notYetImplementedBrowsers != null && isDefinedIn(notYetImplementedBrowsers.value());
}