Java Code Examples for org.junit.runners.model.FrameworkMethod

The following examples show how to use org.junit.runners.model.FrameworkMethod. These examples are extracted from open source projects. 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 Project: buck   Source File: EndToEndRunner.java    License: 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 2
Source Project: Pushjet-Android   Source File: Sample.java    License: 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 3
Source Project: karate   Source File: Karate.java    License: 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 4
Source Project: geowave   Source File: GeoWaveITRunner.java    License: 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 5
Source Project: zerocode   Source File: ZeroCodeUnitRunner.java    License: 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 6
Source Project: htmlunit   Source File: StandardsTestClass.java    License: 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 7
Source Project: datakernel   Source File: DatakernelServiceRunner.java    License: 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 8
Source Project: android-test   Source File: UiThreadStatement.java    License: 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 9
Source Project: pinpoint   Source File: ForkedPinpointPluginTestRunner.java    License: 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 10
Source Project: qpid-broker-j   Source File: QpidUnitTestRunner.java    License: 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 11
Source Project: junit-dataprovider   Source File: TestGeneratorTest.java    License: 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 12
Source Project: JQF   Source File: JQF.java    License: 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 13
/**
 * {@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());
}
 
Example 14
Source Project: bazel   Source File: DesugarRunner.java    License: 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 15
Source Project: registry   Source File: CustomParameterizedRunner.java    License: Apache License 2.0 5 votes vote down vote up
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());
}
 
Example 16
Source Project: pravega   Source File: SystemTestRunner.java    License: 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 {
        //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);
    }
}
 
Example 17
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);
}
 
Example 18
Source Project: j2cl   Source File: AsyncTestRunner.java    License: Apache License 2.0 5 votes vote down vote up
@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);
  }
}
 
Example 19
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 20
@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 21
@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 Project: alfresco-sdk   Source File: AlfrescoTestRunner.java    License: 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 Project: stratio-cassandra   Source File: MethodComparator.java    License: 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 Project: quickperf   Source File: MainJvmAfterJUnitStatement.java    License: 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 25
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 26
Source Project: astor   Source File: RetryRunner.java    License: 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 27
Source Project: junit-dataprovider   Source File: TestGeneratorTest.java    License: 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 28
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();
        }
    };
}
 
Example 29
Source Project: dsl-devkit   Source File: ClassRunner.java    License: 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 30
Source Project: qpid-jms   Source File: QpidJMSTestRunner.java    License: 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;
}