Search in sources :

Example 11 with Statement

use of org.junit.runners.model.Statement in project junit4 by junit-team.

the class ExpectExceptionTest method whenExpectingAssumptionViolatedExceptionStatementsThrowingItShouldPass.

@Test
public void whenExpectingAssumptionViolatedExceptionStatementsThrowingItShouldPass() {
    Statement delegate = new Fail(new AssumptionViolatedException("expected"));
    ExpectException expectException = new ExpectException(delegate, AssumptionViolatedException.class);
    try {
        expectException.evaluate();
    // then AssumptionViolatedException should not be thrown
    } catch (Throwable e) {
        // need to explicitly catch and re-throw as an AssertionError or it might be skipped
        fail("should not throw anything, but was thrown: " + e);
    }
}
Also used : AssumptionViolatedException(org.junit.internal.AssumptionViolatedException) Statement(org.junit.runners.model.Statement) Test(org.junit.Test)

Example 12 with Statement

use of org.junit.runners.model.Statement 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);
    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 13 with Statement

use of org.junit.runners.model.Statement in project junit4 by junit-team.

the class ParentRunner method classBlock.

/**
     * Constructs a {@code Statement} to run all of the tests in the test class.
     * Override to add pre-/post-processing. Here is an outline of the
     * implementation:
     * <ol>
     * <li>Determine the children to be run using {@link #getChildren()}
     * (subject to any imposed filter and sort).</li>
     * <li>If there are any children remaining after filtering and ignoring,
     * construct a statement that will:
     * <ol>
     * <li>Apply all {@code ClassRule}s on the test-class and superclasses.</li>
     * <li>Run all non-overridden {@code @BeforeClass} methods on the test-class
     * and superclasses; if any throws an Exception, stop execution and pass the
     * exception on.</li>
     * <li>Run all remaining tests on the test-class.</li>
     * <li>Run all non-overridden {@code @AfterClass} methods on the test-class
     * and superclasses: exceptions thrown by previous steps are combined, if
     * necessary, with exceptions from AfterClass methods into a
     * {@link org.junit.runners.model.MultipleFailureException}.</li>
     * </ol>
     * </li>
     * </ol>
     *
     * @return {@code Statement}
     */
protected Statement classBlock(final RunNotifier notifier) {
    Statement statement = childrenInvoker(notifier);
    if (!areAllChildrenIgnored()) {
        statement = withBeforeClasses(statement);
        statement = withAfterClasses(statement);
        statement = withClassRules(statement);
    }
    return statement;
}
Also used : Statement(org.junit.runners.model.Statement)

Example 14 with Statement

use of org.junit.runners.model.Statement in project junit4 by junit-team.

the class BlockJUnit4ClassRunner method withRules.

private Statement withRules(FrameworkMethod method, Object target, Statement statement) {
    List<TestRule> testRules = getTestRules(target);
    Statement result = statement;
    result = withMethodRules(method, testRules, target, result);
    result = withTestRules(method, testRules, result);
    return result;
}
Also used : TestRule(org.junit.rules.TestRule) Statement(org.junit.runners.model.Statement)

Example 15 with Statement

use of org.junit.runners.model.Statement in project neo4j by neo4j.

the class SessionRule method apply.

@Override
public Statement apply(final Statement base, Description description) {
    return new Statement() {

        @Override
        public void evaluate() throws Throwable {
            Map<Setting<?>, String> config = new HashMap<>();
            config.put(GraphDatabaseSettings.auth_enabled, Boolean.toString(authEnabled));
            gdb = (GraphDatabaseAPI) new TestGraphDatabaseFactory().newImpermanentDatabase(config);
            DependencyResolver resolver = gdb.getDependencyResolver();
            Authentication authentication = authentication(resolver.resolveDependency(AuthManager.class), resolver.resolveDependency(UserManagerSupplier.class));
            boltFactory = new BoltFactoryImpl(gdb, new UsageData(null), NullLogService.getInstance(), resolver.resolveDependency(ThreadToStatementContextBridge.class), authentication, BoltConnectionTracker.NOOP, Config.defaults());
            boltFactory.start();
            try {
                base.evaluate();
            } finally {
                try {
                    if (runningMachines != null) {
                        runningMachines.forEach(BoltStateMachine::close);
                    }
                } catch (Throwable e) {
                    e.printStackTrace();
                }
                gdb.shutdown();
            }
        }
    };
}
Also used : UsageData(org.neo4j.udc.UsageData) HashMap(java.util.HashMap) AuthManager(org.neo4j.kernel.api.security.AuthManager) Statement(org.junit.runners.model.Statement) Setting(org.neo4j.graphdb.config.Setting) UserManagerSupplier(org.neo4j.kernel.api.security.UserManagerSupplier) DependencyResolver(org.neo4j.graphdb.DependencyResolver) BoltFactoryImpl(org.neo4j.bolt.v1.runtime.BoltFactoryImpl) BoltStateMachine(org.neo4j.bolt.v1.runtime.BoltStateMachine) Authentication(org.neo4j.bolt.security.auth.Authentication) BasicAuthentication(org.neo4j.bolt.security.auth.BasicAuthentication) TestGraphDatabaseFactory(org.neo4j.test.TestGraphDatabaseFactory)

Aggregations

Statement (org.junit.runners.model.Statement)129 Test (org.junit.Test)22 FrameworkMethod (org.junit.runners.model.FrameworkMethod)15 Method (java.lang.reflect.Method)10 AssumptionViolatedException (org.junit.internal.AssumptionViolatedException)10 Fail (org.junit.internal.runners.statements.Fail)9 TestRule (org.junit.rules.TestRule)7 Description (org.junit.runner.Description)7 MethodRule (org.junit.rules.MethodRule)6 MultipleFailureException (org.junit.runners.model.MultipleFailureException)6 ExecutorService (java.util.concurrent.ExecutorService)5 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)5 ReflectiveCallable (org.junit.internal.runners.model.ReflectiveCallable)5 ArrayList (java.util.ArrayList)4 AtomicReference (java.util.concurrent.atomic.AtomicReference)4 EmptyStatement (com.hazelcast.util.EmptyStatement)3 CountDownLatch (java.util.concurrent.CountDownLatch)3 TestExecutorService (org.apache.beam.fn.harness.test.TestExecutors.TestExecutorService)3 CompositeConfiguration (org.apache.logging.log4j.core.config.composite.CompositeConfiguration)3 LoggerContextRule (org.apache.logging.log4j.junit.LoggerContextRule)3