Java Code Examples for org.junit.runners.model.FrameworkMethod#invokeExplosively()

The following examples show how to use org.junit.runners.model.FrameworkMethod#invokeExplosively() . 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: CmmnTestRunner.java    From flowable-engine with Apache License 2.0 6 votes vote down vote up
@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);
    }

}
 
Example 2
Source File: NamedParameterizedRunner.java    From sql-layer with GNU Affero General Public License v3.0 6 votes vote down vote up
/**
 * Gets the parameterization
 * @return the parameterization collection
 * @throws Throwable if the annotation requirements are not met, or if there's an error in invoking
 * the class's "get parameterizations" method.
 */
private Collection<Parameterization> getParameterizations() throws Throwable
{
    TestClass cls = getTestClass();
    List<FrameworkMethod> methods = cls.getAnnotatedMethods(TestParameters.class);

    if (methods.size() != 1)
    {
        throw new Exception("class " + cls.getName() + " must have exactly 1 method annotated with "
            + TestParameters.class.getSimpleName() +"; found " + methods.size());
    }

    FrameworkMethod method = methods.get(0);
    checkParameterizationMethod(method);

    @SuppressWarnings("unchecked")
    Collection<Parameterization> ret = (Collection<Parameterization>) method.invokeExplosively(null);
    checkParameterizations(ret);
    return ret;
}
 
Example 3
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 4
Source File: TestGenerator.java    From junit-dataprovider with Apache License 2.0 6 votes vote down vote up
/**
 * Creates a list of test methods out of an existing test method and its dataprovider method.
 * <p>
 * This method is package private (= visible) for testing.
 * </p>
 *
 * @param testMethod the original test method
 * @param dataProviderMethod the dataprovider method that gives the parameters
 * @return a list of methods, each method bound to a parameter combination returned by the dataprovider
 */
List<FrameworkMethod> explodeTestMethod(FrameworkMethod testMethod, FrameworkMethod dataProviderMethod) {
    DataProvider dataProvider = dataProviderMethod.getAnnotation(DataProvider.class);

    Object data;
    if (dataProviderDataCache.containsKey(dataProviderMethod)) {
        data = dataProviderDataCache.get(dataProviderMethod);
    } else {
        try {
            Class<?>[] parameterTypes = dataProviderMethod.getMethod().getParameterTypes();
            if (parameterTypes.length > 0) {
                data = dataProviderMethod.invokeExplosively(null, testMethod);
            } else {
                data = dataProviderMethod.invokeExplosively(null);
            }
            if (dataProvider.cache()) {
                dataProviderDataCache.put(dataProviderMethod, data);
            }

        } catch (Throwable t) {
            throw new IllegalArgumentException(String.format("Exception while invoking dataprovider method '%s': %s",
                    dataProviderMethod.getName(), t.getMessage()), t);
        }
    }
    return explodeTestMethod(testMethod, data, dataProvider);
}
 
Example 5
Source File: TrialRunner.java    From JQF with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Override protected Statement methodInvoker(
        FrameworkMethod frameworkMethod,
        Object test) {
    assert(this.method == frameworkMethod);
    return new Statement() {
        @Override public void evaluate() throws Throwable {
            frameworkMethod.invokeExplosively(test, args);
        }
    };
}
 
Example 6
Source File: CustomParameterizedBlockJUnit4Runner.java    From registry with Apache License 2.0 5 votes vote down vote up
@Override
public void evaluate() throws Throwable {
    for (FrameworkMethod before : befores) {
        final int paramCount = before.getMethod().getParameterTypes().length;
        before.invokeExplosively(null, paramCount == 0 ? (Object[]) null : parameters);
    }
    next.evaluate();
}
 
Example 7
Source File: ParameterTest.java    From openCypher with Apache License 2.0 5 votes vote down vote up
private Single( Class<?> testClass, FrameworkMethod method ) throws Throwable
{
    super( testClass );
    this.method = method;
    this.parameter = method.invokeExplosively( null );
    this.description = Description.createTestDescription(
            testClass, method.getName(), method.getAnnotations() );
}
 
Example 8
Source File: DatakernelRunner.java    From datakernel with Apache License 2.0 5 votes vote down vote up
@Override
protected Statement withBefores(FrameworkMethod method, Object target, Statement test) {
	List<FrameworkMethod> methods = getTestClass().getAnnotatedMethods(Before.class);
	if (methods.isEmpty()) {
		return test;
	}
	return new LambdaStatement(() -> {
		for (FrameworkMethod m : methods) {
			m.invokeExplosively(target, getArgs(m));
		}
		test.evaluate();
	});
}
 
Example 9
Source File: RunParameterizedBeforesClass.java    From hifive-pitalium with Apache License 2.0 5 votes vote down vote up
@Override
public void evaluate() throws Throwable {
	for (FrameworkMethod before : befores) {
		before.invokeExplosively(null, parameters);
	}
	next.evaluate();
}
 
Example 10
Source File: WildflyTestRunner.java    From wildfly-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
@SuppressWarnings("unchecked")
private Collection<Object> resolveParameters(final TestClass testClass) {
    final FrameworkMethod method = findParametersMethod(testClass);
    if (method == null) {
        return Collections.emptyList();
    }
    try {
        return (Collection<Object>) method.invokeExplosively(null);
    } catch (Throwable t) {
        throw new RuntimeException(String.format("Failed to invoke method %s on test %s", method.getName(), testClass.getName()), t);
    }
}
 
Example 11
Source File: ParameterStatement.java    From neodymium-library with MIT License 4 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
public List<Object> createIterationData(TestClass testClass, FrameworkMethod method) throws Exception
{
    List<FrameworkMethod> parametersMethods = testClass.getAnnotatedMethods(Parameters.class);
    Iterable<Object> parameter = null;

    for (FrameworkMethod parametersMethod : parametersMethods)
    {
        if (parametersMethod.isPublic() && parametersMethod.isStatic())
        {
            // take the first public static method. invoke it and use the result as parameter
            try
            {
                Object parametersResult = parametersMethod.invokeExplosively(null);
                if (parametersResult instanceof Iterable)
                {
                    parameter = (Iterable<Object>) parametersResult;
                }
                else if (parametersResult instanceof Object[])
                {
                    parameter = Arrays.asList((Object[]) parametersResult);
                }
                else
                {
                    String msg = MessageFormat.format("{0}.{1}() must return an Iterable of arrays.",
                                                      testClass.getJavaClass().getName(), parametersMethod.getName());
                    throw new Exception(msg);
                }
                break;
            }
            catch (Throwable e)
            {
                throw new RuntimeException(e);
            }
        }
    }

    if (!parametersMethods.isEmpty() && parameter == null)
    {
        throw new Exception("No public static parameters method on class " + testClass.getJavaClass().getCanonicalName());
    }

    List<FrameworkField> parameterFrameworkFields = testClass.getAnnotatedFields(Parameter.class);
    LOGGER.debug("Found " + parameterFrameworkFields.size() + " parameter fields");

    List<Object> iterations = new LinkedList<>();

    if (parameter != null)
    {
        int parameterSetCounter = 0;
        for (Object para : parameter)
        {
            Object[] p;
            if (para instanceof Object[])
            {
                p = (Object[]) para;
            }
            else
            {
                p = new Object[]
                {
                  para
                };
            }

            iterations.add(new ParameterStatementData(parameterSetCounter, p, parameterFrameworkFields));
            parameterSetCounter++;
        }
    }

    return iterations;
}
 
Example 12
Source File: DatakernelRunner.java    From datakernel with Apache License 2.0 4 votes vote down vote up
@Override
protected Statement methodInvoker(FrameworkMethod method, Object test) {
	return new LambdaStatement(() -> method.invokeExplosively(test, getArgs(method)));
}
 
Example 13
Source File: StubJUnitFrameworkMethod.java    From spectrum with MIT License 4 votes vote down vote up
@Test
public void stubCanBeRetrievedAndUsed() throws Throwable {
  FrameworkMethod method = stubFrameworkMethod();
  method.invokeExplosively(
      new com.greghaskins.spectrum.internal.junit.StubJUnitFrameworkMethod.Stub());
}