org.junit.runners.model.FrameworkMethod Java Examples

The following examples show how to use org.junit.runners.model.FrameworkMethod. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: UiThreadStatement.java    From android-test with Apache License 2.0 6 votes vote down vote up
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;
}
 
Example #2
Source File: StandardsTestClass.java    From htmlunit with Apache License 2.0 6 votes vote down vote up
/**
 * {@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);
        }
    }
}
 
Example #3
Source File: QpidUnitTestRunner.java    From qpid-broker-j with Apache License 2.0 6 votes vote down vote up
@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());
    }
}
 
Example #4
Source File: ZeroCodeUnitRunner.java    From zerocode with Apache License 2.0 6 votes vote down vote up
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;
}
 
Example #5
Source File: Karate.java    From karate with MIT License 6 votes vote down vote up
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());
}
 
Example #6
Source File: GeoWaveITRunner.java    From geowave with Apache License 2.0 6 votes vote down vote up
@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);
}
 
Example #7
Source File: Sample.java    From Pushjet-Android with BSD 2-Clause "Simplified" License 6 votes vote down vote up
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();
        }
    };
}
 
Example #8
Source File: ForkedPinpointPluginTestRunner.java    From pinpoint with Apache License 2.0 6 votes vote down vote up
@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);
            }
        }
    };
}
 
Example #9
Source File: EndToEndRunner.java    From buck with Apache License 2.0 6 votes vote down vote up
/**
 * 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());
    }
  }
}
 
Example #10
Source File: TestGeneratorTest.java    From junit-dataprovider with Apache License 2.0 6 votes vote down vote up
@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);
}
 
Example #11
Source File: DatakernelServiceRunner.java    From datakernel with Apache License 2.0 6 votes vote down vote up
@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();
	});
}
 
Example #12
Source File: QpidJMSTestRunner.java    From qpid-jms with Apache License 2.0 5 votes vote down vote up
/**
 * 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;
}
 
Example #13
Source File: BaseJdbcTest.java    From ormlite-jdbc with ISC License 5 votes vote down vote up
@Override
public Statement apply(Statement statement, FrameworkMethod method, Object junitClassObject) {
	for (Annotation annotation : method.getAnnotations()) {
		if (annotation.annotationType() == ExpectedBehavior.class) {
			ExpectedBehavior test = (ExpectedBehavior) annotation;
			tClass = test.expected();
			break;
		}
	}
	return new StatementWrapper(statement);
}
 
Example #14
Source File: NamedParameterizedRunner.java    From sql-layer with GNU Affero General Public License v3.0 5 votes vote down vote up
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;
}
 
Example #15
Source File: KurentoBlockJUnit4ClassRunnerWithParameters.java    From kurento-java with Apache License 2.0 5 votes vote down vote up
@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);
  }
}
 
Example #16
Source File: ClassRunner.java    From dsl-devkit with Eclipse Public License 1.0 5 votes vote down vote up
/** {@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);
}
 
Example #17
Source File: DataProviderRunnerTest.java    From junit-dataprovider with Apache License 2.0 5 votes vote down vote up
@Test
public void testGenerateExplodedTestMethodsForShouldCallTestGeneratorWithNotFoundDataProviderMethodAndAddResult() {
    // Given:
    doReturn(singletonList(null)).when(underTest).getDataProviderMethods(testMethod);
    when(testGenerator.generateExplodedTestMethodsFor(testMethod, null)).thenReturn(singletonList(testMethod));

    // When:
    List<FrameworkMethod> result = underTest.generateExplodedTestMethodsFor(singletonList(testMethod));

    // Then:
    assertThat(result).containsOnly(testMethod);

    verify(testGenerator).generateExplodedTestMethodsFor(testMethod, null);
    verifyNoMoreInteractions(testGenerator);
}
 
Example #18
Source File: DesugarRunner.java    From bazel with Apache License 2.0 5 votes vote down vote up
/**
 * 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);
  }
}
 
Example #19
Source File: QpidJMSTestRunner.java    From qpid-jms with Apache License 2.0 5 votes vote down vote up
@Override
protected Statement methodBlock(final FrameworkMethod method) {
    Statement statement = super.methodBlock(method);

    // Check for repeats needed
    statement = withPotentialRepeat(method, statement);

    return statement;
}
 
Example #20
Source File: DominoJUnitRunner.java    From org.openntf.domino with Apache License 2.0 5 votes vote down vote up
@Override
protected void afterTest(final FrameworkMethod method) {
	WrapperFactory wf = Factory.getWrapperFactory();
	wf.recycle(TestEnv.database);
	wf.recycle(TestEnv.session);
	TestEnv.session = null;
	TestEnv.database = null;
	Factory.termThread();
}
 
Example #21
Source File: AbstractParallelScenarioRunner.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
@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);
		}
	};
}
 
Example #22
Source File: AlfrescoTestRunner.java    From alfresco-sdk with Apache License 2.0 5 votes vote down vote up
@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);
        }
    }
}
 
Example #23
Source File: MethodComparator.java    From stratio-cassandra with Apache License 2.0 5 votes vote down vote up
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();
    }
}
 
Example #24
Source File: DefaultServer.java    From quarkus-http with Apache License 2.0 5 votes vote down vote up
@Override
protected String testName(FrameworkMethod method) {
    if (!isProxy()) {
        return super.testName(method);
    } else {
        StringBuilder sb = new StringBuilder(super.testName(method));
        if (isProxy()) {
            sb.append("[proxy]");
        }
        if (ajp) {
            sb.append("[ajp]");
        }
        if (https) {
            sb.append("[https]");
        }
        if (h2) {
            sb.append("[http2]");
        }
        if (h2c) {
            sb.append("[http2-clear]");
        }
        if (h2cUpgrade) {
            sb.append("[http2-clear-upgrade]");
        }
        return sb.toString();
    }
}
 
Example #25
Source File: MainJvmAfterJUnitStatement.java    From quickperf with Apache License 2.0 5 votes vote down vote up
public MainJvmAfterJUnitStatement(
          FrameworkMethod frameworkMethod
        , TestExecutionContext testExecutionContext
        , QuickPerfConfigs quickPerfConfigs
        , Statement junitAfters) {
    this.testExecutionContext = testExecutionContext;
    this.frameworkMethod = frameworkMethod;
    this.testAnnotationConfigs = quickPerfConfigs.getTestAnnotationConfigs();
    this.junitAfters = junitAfters;
}
 
Example #26
Source File: ParameterDescriptions.java    From wildfly-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
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);
        }
    }
}
 
Example #27
Source File: RetryRunner.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@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;
            }
        }
    };
}
 
Example #28
Source File: TestGeneratorTest.java    From junit-dataprovider with Apache License 2.0 5 votes vote down vote up
@Test
public void testGenerateExplodedTestMethodsForShouldReturnEmptyListIfArgumentIsNull() {
    // Given:

    // When:
    List<FrameworkMethod> result = underTest.generateExplodedTestMethodsFor(null, null);

    // Then:
    assertThat(result).isEmpty();
}
 
Example #29
Source File: JQF.java    From JQF with BSD 2-Clause "Simplified" License 5 votes vote down vote up
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"));
        }
    }
}
 
Example #30
Source File: TestResources.java    From Pushjet-Android with BSD 2-Clause "Simplified" License 5 votes vote down vote up
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();
        }
    };
}