下面列出了怎么用org.junit.runners.model.FrameworkMethod的API类实例代码及写法,或者点击链接到github查看源代码。
/**
* Marks validation errors in errors if 2 methods marked by @EnvironmentFor contain the same test
* names
*
* <p>Note: Adds an error to the list for each pair of duplicates found
*/
private void validateEnvironmentMapContainsNoDuplicates(List<Throwable> errors) {
List<FrameworkMethod> environmentForMethods =
getTestClass().getAnnotatedMethods(EnvironmentFor.class);
Map<String, String> seenTestNames = new HashMap<>();
for (FrameworkMethod environmentForMethod : environmentForMethods) {
String[] environmentForTestNames =
environmentForMethod.getAnnotation(EnvironmentFor.class).testNames();
for (String testName : environmentForTestNames) {
if (seenTestNames.containsKey(testName)) {
errors.add(
new AnnotationFormatError(
String.format(
"EnvironmentFor methods %s and %s are both marked as for %s",
environmentForMethod.getName(), seenTestNames.get(testName), testName)));
}
seenTestNames.put(testName, environmentForMethod.getName());
}
}
}
public Statement apply(final Statement base, FrameworkMethod method, Object target) {
final String sampleName = getSampleName(method);
sampleDir = sampleName == null ? null : testDirectoryProvider.getTestDirectory().file(sampleName);
return new Statement() {
@Override
public void evaluate() throws Throwable {
if (sampleName != null) {
TestFile srcDir = new IntegrationTestBuildContext().getSamplesDir().file(sampleName).assertIsDir();
logger.debug("Copying sample '{}' to test directory.", sampleName);
srcDir.copyTo(sampleDir);
} else {
logger.debug("No sample specified for this test, skipping.");
}
base.evaluate();
}
};
}
public Karate(Class<?> clazz) throws InitializationError, IOException {
super(clazz);
List<FrameworkMethod> testMethods = getTestClass().getAnnotatedMethods(Test.class);
if (!testMethods.isEmpty()) {
logger.warn("WARNING: there are methods annotated with '@Test', they will NOT be run when using '@RunWith(Karate.class)'");
}
RunnerOptions options = RunnerOptions.fromAnnotationAndSystemProperties(clazz);
List<Resource> resources = FileUtils.scanForFeatureFiles(options.getFeatures(), clazz.getClassLoader());
children = new ArrayList(resources.size());
featureMap = new HashMap(resources.size());
for (Resource resource : resources) {
Feature feature = FeatureParser.parse(resource);
feature.setCallName(options.getName());
feature.setCallLine(resource.getLine());
children.add(feature);
}
tagSelector = Tags.fromKarateOptionsTags(options.getTags());
}
@Override
protected Statement withAfterClasses(final Statement statement) {
// add test environment tear down
try {
final Statement newStatement = super.withAfterClasses(statement);
final Method tearDownMethod = GeoWaveITRunner.class.getDeclaredMethod("tearDown");
tearDownMethod.setAccessible(true);
return new RunAfters(
newStatement,
Collections.singletonList(new FrameworkMethod(tearDownMethod)),
this);
} catch (NoSuchMethodException | SecurityException e) {
LOGGER.warn("Unable to find tearDown method", e);
}
return super.withAfterClasses(statement);
}
private List<String> getSmartChildrenList() {
List<FrameworkMethod> children = getChildren();
children.forEach(
frameworkMethod -> {
JsonTestCase jsonTestCaseAnno = frameworkMethod.getAnnotation(JsonTestCase.class);
if(jsonTestCaseAnno == null){
jsonTestCaseAnno = evalScenarioToJsonTestCase(frameworkMethod.getAnnotation(Scenario.class));
}
if (jsonTestCaseAnno != null) {
smartTestCaseNames.add(jsonTestCaseAnno.value());
} else {
smartTestCaseNames.add(frameworkMethod.getName());
}
}
);
return smartTestCaseNames;
}
/**
* {@inheritDoc}
*/
@Override
protected void scanAnnotatedMembers(final Map<Class<? extends Annotation>,
List<FrameworkMethod>> methodsForAnnotations,
final Map<Class<? extends Annotation>, List<FrameworkField>> fieldsForAnnotations) {
for (final Class<?> eachClass : getSuperClasses(getJavaClass())) {
for (final Method eachMethod : MethodSorter.getDeclaredMethods(eachClass)) {
addToAnnotationLists(new FrameworkMethod(eachMethod), methodsForAnnotations);
}
// Fields are ignored
}
for (final Map.Entry<Class<? extends Annotation>,
List<FrameworkMethod>> methodsEntry : methodsForAnnotations.entrySet()) {
final Class<? extends Annotation> key = methodsEntry.getKey();
if (key == Test.class) {
final List<FrameworkMethod> methods = methodsEntry.getValue();
final List<FrameworkMethod> newMethods = new ArrayList<>(methods.size() * 2);
for (final FrameworkMethod m : methods) {
newMethods.add(new StandardsFrameworkMethod(m.getMethod(), false));
newMethods.add(new StandardsFrameworkMethod(m.getMethod(), true));
}
methodsForAnnotations.put(key, newMethods);
}
}
}
@Override
protected Statement methodInvoker(FrameworkMethod method, Object test) {
return new LambdaStatement(() -> {
// create args before running the service graph so that those args that are services are found by service graph
Object[] args = getArgs(method);
ServiceGraph serviceGraph = currentInjector.getInstanceOrNull(ServiceGraph.class);
if (serviceGraph == null) {
method.invokeExplosively(test, args);
return;
}
serviceGraph.startFuture().get();
method.invokeExplosively(test, args);
serviceGraph.stopFuture().get();
});
}
public static boolean shouldRunOnUiThread(FrameworkMethod method) {
Class<? extends Annotation> deprecatedUiThreadTestClass =
loadUiThreadClass("android.test.UiThreadTest");
if (hasAnnotation(method, deprecatedUiThreadTestClass)) {
return true;
} else {
// to avoid circular dependency on Rules module use the class name directly
@SuppressWarnings("unchecked") // reflection
Class<? extends Annotation> uiThreadTestClass =
loadUiThreadClass("androidx.test.annotation.UiThreadTest");
if (hasAnnotation(method, deprecatedUiThreadTestClass)
|| hasAnnotation(method, uiThreadTestClass)) {
return true;
}
}
return false;
}
@Override
protected Statement methodBlock(FrameworkMethod method) {
final Statement fromSuper = super.methodBlock(method);
final boolean manageTraceObject = this.manageTraceObject && (method.getAnnotation(TraceObjectManagable.class) == null);
return new Statement() {
@Override
public void evaluate() throws Throwable {
PluginTestVerifier verifier = PluginTestVerifierHolder.getInstance();
verifier.initialize(manageTraceObject);
try {
fromSuper.evaluate();
} finally {
verifier.cleanUp(manageTraceObject);
}
}
};
}
@Override
protected void runChild(final FrameworkMethod method, final RunNotifier notifier)
{
LOGGER.info("========================= executing test : {}", _testClass.getSimpleName() + "#" + method.getName());
setClassQualifiedTestName(_testClass.getName() + "." + method.getName());
LOGGER.info("========================= start executing test : {}", _testClass.getSimpleName() + "#" + method.getName());
try
{
super.runChild(method, notifier);
}
finally
{
LOGGER.info("========================= stop executing test : {} ", _testClass.getSimpleName() + "#" + method.getName());
setClassQualifiedTestName(_testClass.getName());
LOGGER.info("========================= cleaning up test environment for test : {}", _testClass.getSimpleName() + "#" + method.getName());
}
}
@Test
public void testExplodeTestMethodsUseDataProviderShouldReturnFrameworkMethodInjectedToUseDataProviderMethodIfExists() throws Throwable {
// Given:
final Method method = getMethod("dataProviderMethod");
when(dataProviderMethod.getMethod()).thenReturn(method);
List<Object[]> dataConverterResult = listOfArrays(new Object[] { null });
when(dataConverter.convert(any(), anyBoolean(), any(Class[].class), any(DataProvider.class))).thenReturn(dataConverterResult);
when(dataProviderMethod.getAnnotation(DataProvider.class)).thenReturn(dataProvider);
when(dataProvider.format()).thenReturn(DataProvider.DEFAULT_FORMAT);
when(dataProvider.cache()).thenReturn(true);
// When:
List<FrameworkMethod> result = underTest.explodeTestMethod(testMethod, dataProviderMethod);
// Then:
assertThat(TestGenerator.dataProviderDataCache).hasSize(1).containsKey(dataProviderMethod);
assertThat(result).hasSize(1);
verify(dataProviderMethod).invokeExplosively(null, testMethod);
}
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"));
}
}
}
/**
* {@inheritDoc}
* <p>
* Look at the classes java doc for detailed description of the applied strategies.
*
* @return the resolved dataprovider method or an empty {@link List} if dataprovider could not be found (never null)
* @see DefaultDataProviderMethodResolver
*/
@Override
public List<FrameworkMethod> resolve(FrameworkMethod testMethod, UseDataProvider useDataProvider) {
checkNotNull(testMethod, "testMethod must not be null");
checkNotNull(useDataProvider, "useDataProvider must not be null");
List<TestClass> dataProviderLocations = findDataProviderLocations(testMethod, useDataProvider.location());
return findDataProviderMethods(dataProviderLocations, testMethod.getName(), useDataProvider.value());
}
/**
* 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);
}
}
private FrameworkMethod getParametersMethod() throws Exception {
List<FrameworkMethod> methods = testClass
.getAnnotatedMethods(Parameters.class);
for (FrameworkMethod each : methods) {
if (each.isStatic() && each.isPublic()) {
return each;
}
}
throw new Exception("No public static parameters method on class "
+ testClass.getName());
}
@Override
protected void runChild(final FrameworkMethod method, RunNotifier notifier) {
Description description = describeChild(method);
if (isIgnored(method)) {
notifier.fireTestIgnored(description);
} else {
//read the type of testExecutor from system property. This is sent by the gradle task. By default
//the tests are executed locally.
TestExecutorType executionType = TestExecutorType.valueOf(getConfig("execType", "LOCAL"));
//sleep for 15 seconds before running tests, remove once pravega/pravega/issues/1665 is resolved
Exceptions.handleInterrupted(() -> TimeUnit.SECONDS.sleep(15));
invokeTest(notifier, executionType, method);
}
}
boolean onlyIfFilter(FrameworkMethod method) throws OnlyIfException {
OnlyIf onlyIf = method.getAnnotation(OnlyIf.class);
if (onlyIf != null) {
if (!runOnlyIf(onlyIf.value(), true)) {
return false;
}
}
OnlyIfNot onlyIfNot = method.getAnnotation(OnlyIfNot.class);
return onlyIfNot == null || runOnlyIf(onlyIfNot.value(), false);
}
@Override
protected void validatePublicVoidNoArgMethods(
Class<? extends Annotation> annotation, boolean isStatic, List<Throwable> errors) {
List<FrameworkMethod> methods = getTestClass().getAnnotatedMethods(annotation);
for (FrameworkMethod eachTestMethod : methods) {
validateTestMethod(isStatic, errors, eachTestMethod);
}
}
boolean expectedToPass(FrameworkMethod method)
{
if (overrideOn)
{
return true;
}
if (! parameterization.expectedToPass())
{
return false;
}
Failing failing = method.getAnnotation(Failing.class);
if (failing == null)
{
return true;
}
if (failing.value().length == 0)
{
return false;
}
for (final String paramName : failing.value())
{
if (paramNameUsesRegex(paramName))
{
if (paramNameMatchesRegex(parameterization.getName(), paramName))
{
return false;
}
}
else if (parameterization.getName().equals(paramName))
{
return false;
}
}
return true;
}
@Override
protected void runChild(final FrameworkMethod method, RunNotifier notifier) {
Description description = describeChild(method);
if (isIgnored(method)) {
notifier.fireTestIgnored(description);
} else {
runLeaf2(methodBlock(method), description, notifier);
}
}
@Override
protected Statement methodBlock(final FrameworkMethod method) {
return new Statement() {
@Override
public void evaluate() throws Throwable {
String testData = AbstractParallelScenarioRunner.this.testData.remove(method);
if (testData != null)
process(testData);
}
};
}
@Override
protected void runChild(FrameworkMethod method, RunNotifier notifier) {
if (areWeRunningInAlfresco()) {
// Just run the test as normally
super.runChild(method, notifier);
} else {
// We are not running in an Alfresco Server, we need to call one and have it execute the test...
Description desc = describeChild(method);
if (method.getAnnotation(Ignore.class) != null) {
notifier.fireTestIgnored(desc);
} else {
callProxiedChild(method, notifier, desc);
}
}
}
private MethodPosition getIndexOfMethodPosition(final Object method)
{
if (method instanceof FrameworkMethod)
{
return this.getIndexOfMethodPosition((FrameworkMethod) method);
}
else if (method instanceof Method)
{
return this.getIndexOfMethodPosition((Method) method);
}
else
{
return new NullMethodPosition();
}
}
public MainJvmAfterJUnitStatement(
FrameworkMethod frameworkMethod
, TestExecutionContext testExecutionContext
, QuickPerfConfigs quickPerfConfigs
, Statement junitAfters) {
this.testExecutionContext = testExecutionContext;
this.frameworkMethod = frameworkMethod;
this.testAnnotationConfigs = quickPerfConfigs.getTestAnnotationConfigs();
this.junitAfters = junitAfters;
}
synchronized ParameterDescription take(final FrameworkMethod method) {
final Queue<ParameterDescription> descriptions = parameters.get(method);
try {
if (descriptions == null || descriptions.isEmpty()) {
return null;
}
return descriptions.poll();
} finally {
if (descriptions != null && descriptions.isEmpty()) {
parameters.remove(method);
}
}
}
@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;
}
}
};
}
@Test
public void testGenerateExplodedTestMethodsForShouldReturnEmptyListIfArgumentIsNull() {
// Given:
// When:
List<FrameworkMethod> result = underTest.generateExplodedTestMethodsFor(null, null);
// Then:
assertThat(result).isEmpty();
}
public Statement apply(Statement base, final FrameworkMethod method, Object target) {
final Statement statement = resources.apply(base, method, target);
return new Statement() {
@Override
public void evaluate() throws Throwable {
String className = method.getMethod().getDeclaringClass().getSimpleName();
maybeCopy(String.format("%s/shared", className));
maybeCopy(String.format("%s/%s", className, method.getName()));
for (String extraResource : extraResources) {
maybeCopy(extraResource);
}
statement.evaluate();
}
};
}
/** {@inheritDoc} */
@Override
protected List<FrameworkMethod> computeTestMethods() {
final Collection<FrameworkMethod> result = Sets.newHashSet();
for (final Class<? extends Annotation> annotationClass : TEST_ANNOTATIONS) {
result.addAll(getTestClass().getAnnotatedMethods(annotationClass));
}
return Lists.newArrayList(result);
}
/**
* Check for the presence of a {@link Repeat} annotation and return a {@link RepeatStatement}
* to handle executing the test repeated or the original value if not repeating.
*
* @return either a {@link RepeatStatement}, or the supplied {@link Statement} as appropriate.
*/
protected Statement withPotentialRepeat(FrameworkMethod frameworkMethod, Statement next) {
Repeat repeatAnnotation = frameworkMethod.getAnnotation(Repeat.class);
if (repeatAnnotation != null) {
next = RepeatStatement.builder().build(repeatAnnotation, next);
}
return next;
}