Search in sources :

Example 1 with Fail

use of org.junit.internal.runners.statements.Fail in project intellij-community by JetBrains.

the class GuiTestRunner method methodBlock.

@Override
protected Statement methodBlock(FrameworkMethod method) {
    FrameworkMethod newMethod;
    try {
        if (Arrays.stream(getTestClass().getAnnotations()).anyMatch(annotation -> annotation instanceof ParentPlugin)) {
            loadClassesWithNewPluginClassLoader();
        } else {
            loadClassesWithIdeClassLoader();
        }
        Method methodFromClassLoader = myTestClass.getJavaClass().getMethod(method.getName());
        newMethod = new FrameworkMethod(methodFromClassLoader);
    } catch (Exception e) {
        return new Fail(e);
    }
    Object test;
    try {
        test = new ReflectiveCallable() {

            @Override
            protected Object runReflectiveCall() throws Throwable {
                return createTest();
            }
        }.run();
    } catch (Throwable e) {
        return new Fail(e);
    }
    Statement statement = methodInvoker(newMethod, test);
    List<FrameworkMethod> beforeMethods = myTestClass.getAnnotatedMethods(Before.class);
    if (!beforeMethods.isEmpty()) {
        statement = new RunBefores(statement, beforeMethods, test);
    }
    List<FrameworkMethod> afterMethods = myTestClass.getAnnotatedMethods(After.class);
    if (!afterMethods.isEmpty()) {
        statement = new RunAfters(statement, afterMethods, test);
    }
    return statement;
}
Also used : RunAfters(org.junit.internal.runners.statements.RunAfters) ReflectiveCallable(org.junit.internal.runners.model.ReflectiveCallable) Statement(org.junit.runners.model.Statement) Method(java.lang.reflect.Method) FrameworkMethod(org.junit.runners.model.FrameworkMethod) RunBefores(org.junit.internal.runners.statements.RunBefores) FrameworkMethod(org.junit.runners.model.FrameworkMethod) AssumptionViolatedException(org.junit.internal.AssumptionViolatedException) Fail(org.junit.internal.runners.statements.Fail)

Example 2 with Fail

use of org.junit.internal.runners.statements.Fail in project android by JetBrains.

the class GuiTestRunner method methodBlock.

@Override
protected Statement methodBlock(FrameworkMethod method) {
    if (GraphicsEnvironment.isHeadless()) {
        // checked first because IdeTestApplication.getInstance below (indirectly) throws an AWTException in a headless environment
        return falseAssumption("headless environment");
    }
    Method methodFromClassLoader;
    try {
        ClassLoader ideClassLoader = IdeTestApplication.getInstance().getIdeClassLoader();
        Thread.currentThread().setContextClassLoader(ideClassLoader);
        myTestClass = new TestClass(ideClassLoader.loadClass(getTestClass().getJavaClass().getName()));
        methodFromClassLoader = myTestClass.getJavaClass().getMethod(method.getName());
    } catch (Exception e) {
        return new Fail(e);
    }
    return super.methodBlock(new FrameworkMethod(methodFromClassLoader));
}
Also used : TestClass(org.junit.runners.model.TestClass) FrameworkMethod(org.junit.runners.model.FrameworkMethod) Method(java.lang.reflect.Method) AssumptionViolatedException(org.junit.AssumptionViolatedException) FrameworkMethod(org.junit.runners.model.FrameworkMethod) Fail(org.junit.internal.runners.statements.Fail)

Example 3 with Fail

use of org.junit.internal.runners.statements.Fail in project pact-jvm by DiUS.

the class InteractionRunner method interactionBlock.

protected Statement interactionBlock(final Interaction interaction) {
    //1. prepare object
    //2. get Target
    //3. run Rule`s
    //4. run Before`s
    //5. run OnStateChange`s
    //6. run test
    //7. run After`s
    final Object test;
    try {
        test = new ReflectiveCallable() {

            @Override
            protected Object runReflectiveCall() throws Throwable {
                return createTest();
            }
        }.run();
    } catch (Throwable e) {
        return new Fail(e);
    }
    final Target target = testClass.getAnnotatedFieldValues(test, TestTarget.class, Target.class).get(0);
    if (target instanceof TestClassAwareTarget) {
        ((TestClassAwareTarget) target).setTestClass(testClass, test);
    }
    Statement statement = new Statement() {

        @Override
        public void evaluate() throws Throwable {
            target.testInteraction(pact.getConsumer().getName(), interaction);
        }
    };
    statement = withStateChanges(interaction, test, statement);
    statement = withBefores(interaction, test, statement);
    statement = withRules(interaction, test, statement);
    statement = withAfters(interaction, test, statement);
    return statement;
}
Also used : TestClassAwareTarget(au.com.dius.pact.provider.junit.target.TestClassAwareTarget) TestTarget(au.com.dius.pact.provider.junit.target.TestTarget) Target(au.com.dius.pact.provider.junit.target.Target) ReflectiveCallable(org.junit.internal.runners.model.ReflectiveCallable) TestTarget(au.com.dius.pact.provider.junit.target.TestTarget) Statement(org.junit.runners.model.Statement) Fail(org.junit.internal.runners.statements.Fail) TestClassAwareTarget(au.com.dius.pact.provider.junit.target.TestClassAwareTarget)

Example 4 with Fail

use of org.junit.internal.runners.statements.Fail in project tomee by apache.

the class MetaRunner method methodBlock.

@Override
protected Statement methodBlock(final FrameworkMethod method) {
    final Object test;
    try {
        test = new ReflectiveCallable() {

            @Override
            protected Object runReflectiveCall() throws Throwable {
                return createTest();
            }
        }.run();
    } catch (final Throwable e) {
        return new Fail(e);
    }
    Statement statement = new MetaTest.$(method, test);
    statement = withBefores(method, test, statement);
    statement = withAfters(method, test, statement);
    return statement;
}
Also used : ReflectiveCallable(org.junit.internal.runners.model.ReflectiveCallable) Statement(org.junit.runners.model.Statement) Fail(org.junit.internal.runners.statements.Fail)

Example 5 with Fail

use of org.junit.internal.runners.statements.Fail in project junit4 by junit-team.

the class BlockJUnit4ClassRunner method methodBlock.

/**
 * Returns a Statement that, when executed, either returns normally if
 * {@code method} passes, or throws an exception if {@code method} fails.
 *
 * Here is an outline of the default implementation:
 *
 * <ul>
 * <li>Invoke {@code method} on the result of {@link #createTest(org.junit.runners.model.FrameworkMethod)}, and
 * throw any exceptions thrown by either operation.
 * <li>HOWEVER, if {@code method}'s {@code @Test} annotation has the {@link Test#expected()}
 * attribute, return normally only if the previous step threw an
 * exception of the correct type, and throw an exception otherwise.
 * <li>HOWEVER, if {@code method}'s {@code @Test} annotation has the {@code
 * timeout} attribute, throw an exception if the previous step takes more
 * than the specified number of milliseconds.
 * <li>ALWAYS run all non-overridden {@code @Before} methods on this class
 * and superclasses before any of the previous steps; if any throws an
 * Exception, stop execution and pass the exception on.
 * <li>ALWAYS run all non-overridden {@code @After} methods on this class
 * and superclasses after any of the previous steps; all After methods are
 * always executed: exceptions thrown by previous steps are combined, if
 * necessary, with exceptions from After methods into a
 * {@link MultipleFailureException}.
 * <li>ALWAYS allow {@code @Rule} fields to modify the execution of the
 * above steps. A {@code Rule} may prevent all execution of the above steps,
 * or add additional behavior before and after, or modify thrown exceptions.
 * For more information, see {@link TestRule}
 * </ul>
 *
 * This can be overridden in subclasses, either by overriding this method,
 * or the implementations creating each sub-statement.
 */
protected Statement methodBlock(final FrameworkMethod method) {
    Object test;
    try {
        test = new ReflectiveCallable() {

            @Override
            protected Object runReflectiveCall() throws Throwable {
                return createTest(method);
            }
        }.run();
    } catch (Throwable e) {
        return new Fail(e);
    }
    Statement statement = methodInvoker(method, test);
    statement = possiblyExpectingExceptions(method, test, statement);
    statement = withPotentialTimeout(method, test, statement);
    statement = withBefores(method, test, statement);
    statement = withAfters(method, test, statement);
    statement = withRules(method, test, statement);
    statement = withInterruptIsolation(statement);
    return statement;
}
Also used : ReflectiveCallable(org.junit.internal.runners.model.ReflectiveCallable) Statement(org.junit.runners.model.Statement) Fail(org.junit.internal.runners.statements.Fail)

Aggregations

Fail (org.junit.internal.runners.statements.Fail)11 Statement (org.junit.runners.model.Statement)8 ReflectiveCallable (org.junit.internal.runners.model.ReflectiveCallable)6 FrameworkMethod (org.junit.runners.model.FrameworkMethod)4 Method (java.lang.reflect.Method)3 RunBefores (org.junit.internal.runners.statements.RunBefores)2 Description (org.junit.runner.Description)2 Target (au.com.dius.pact.provider.junit.target.Target)1 TestClassAwareTarget (au.com.dius.pact.provider.junit.target.TestClassAwareTarget)1 TestTarget (au.com.dius.pact.provider.junit.target.TestTarget)1 ArrayList (java.util.ArrayList)1 AssumptionViolatedException (org.junit.AssumptionViolatedException)1 Test (org.junit.Test)1 AssumptionViolatedException (org.junit.internal.AssumptionViolatedException)1 RunAfters (org.junit.internal.runners.statements.RunAfters)1 MultipleFailureException (org.junit.runners.model.MultipleFailureException)1 TestClass (org.junit.runners.model.TestClass)1