protected Statement methodBlock(FrameworkMethod method) { 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(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; }
/** * Augment the default JUnit behavior * {@linkplain #withPotentialRepeat with potential repeats} of the entire * execution chain. * <p>Furthermore, support for timeouts has been moved down the execution * chain in order to include execution of {@link org.junit.Before @Before} * and {@link org.junit.After @After} methods within the timed execution. * Note that this differs from the default JUnit behavior of executing * {@code @Before} and {@code @After} methods in the main thread while * executing the actual test method in a separate thread. Thus, the net * effect is that {@code @Before} and {@code @After} methods will be * executed in the same thread as the test method. As a consequence, * JUnit-specified timeouts will work fine in combination with Spring * transactions. However, JUnit-specific timeouts still differ from * Spring-specific timeouts in that the former execute in a separate * thread while the latter simply execute in the main thread (like regular * tests). * @see #possiblyExpectingExceptions(FrameworkMethod, Object, Statement) * @see #withBefores(FrameworkMethod, Object, Statement) * @see #withAfters(FrameworkMethod, Object, Statement) * @see #withRulesReflectively(FrameworkMethod, Object, Statement) * @see #withPotentialRepeat(FrameworkMethod, Object, Statement) * @see #withPotentialTimeout(FrameworkMethod, Object, Statement) */ @Override protected Statement methodBlock(FrameworkMethod frameworkMethod) { Object testInstance; try { testInstance = new ReflectiveCallable() { @Override protected Object runReflectiveCall() throws Throwable { return createTest(); } }.run(); } catch (Throwable ex) { return new Fail(ex); } Statement statement = methodInvoker(frameworkMethod, testInstance); statement = possiblyExpectingExceptions(frameworkMethod, testInstance, statement); statement = withBefores(frameworkMethod, testInstance, statement); statement = withAfters(frameworkMethod, testInstance, statement); statement = withRulesReflectively(frameworkMethod, testInstance, statement); statement = withPotentialRepeat(frameworkMethod, testInstance, statement); statement = withPotentialTimeout(frameworkMethod, testInstance, statement); return statement; }
/** * Extends a given {@link Statement} for a {@link TestClass} with the evaluation of * {@link TestRule}, {@link ClassRule}, {@link Before} and {@link After}. * <p> * Therefore the test class will be instantiated and parameters will be injected with the same * mechanism as in {@link Parameterized}. * * Implementation has been extracted from BlockJUnit4ClassRunner#methodBlock(FrameworkMethod). * * @param baseStatementWithChildren - A {@link Statement} that includes execution of the test's * children * @param testClass - The {@link TestClass} of the test. * @param description - The {@link Description} will be passed to the {@link Rule}s and * {@link ClassRule}s. * @param singleParameter - The parameters will be injected in attributes annotated with * {@link Parameterized.Parameter} or passed to the constructor otherwise. * * @see BlockJUnit4ClassRunnerWithParameters#createTest() * @see BlockJUnit4ClassRunner#methodBlock(FrameworkMethod) */ public static Statement buildStatementWithTestRules(Statement baseStatementWithChildren, final TestClass testClass, Description description, final Object[] singleParameter) { final Object test; try { test = new ReflectiveCallable() { protected Object runReflectiveCall() throws Throwable { return createInstanceOfParameterizedTest(testClass, singleParameter); } }.run(); } catch (Throwable e) { return new Fail(e); } List<TestRule> testRules = BlockJUnit4ClassRunnerUtil.getTestRules(test, testClass); Statement statement = BlockJUnit4ClassRunnerUtil.withTestRules(testRules, description, baseStatementWithChildren); statement = ParentRunnerUtil.withBeforeClasses(statement, testClass); statement = ParentRunnerUtil.withAfterClasses(statement, testClass); statement = ParentRunnerUtil.withClassRules(statement, testClass, description); return statement; }
/** * Augment the default JUnit behavior * <p>Furthermore, support for timeouts has been moved down the execution * chain in order to include execution of {@link org.junit.Before @Before} * and {@link org.junit.After @After} methods within the timed execution. * Note that this differs from the default JUnit behavior of executing * {@code @Before} and {@code @After} methods in the main thread while * executing the actual test method in a separate thread. Thus, the net * effect is that {@code @Before} and {@code @After} methods will be * executed in the same thread as the test method. As a consequence, * JUnit-specified timeouts will work fine in combination with Spring * transactions. However, JUnit-specific timeouts still differ from * Spring-specific timeouts in that the former execute in a separate * thread while the latter simply execute in the main thread (like regular * tests). * * @see #methodInvoker(FrameworkMethod, Object) * @see #possiblyExpectingExceptions(FrameworkMethod, Object, Statement) * @see #withBefores(FrameworkMethod, Object, Statement) * @see #withAfters(FrameworkMethod, Object, Statement) * @see #withPotentialTimeout(FrameworkMethod, Object, Statement) */ @Override protected Statement methodBlock(FrameworkMethod frameworkMethod) { Object testInstance; try { testInstance = new ReflectiveCallable() { @Override protected Object runReflectiveCall() throws Throwable { return createTest(); } }.run(); } catch (Throwable ex) { return new Fail(ex); } Environment.inject(testInstance); Statement statement = methodInvoker(frameworkMethod, testInstance); statement = possiblyExpectingExceptions(frameworkMethod, testInstance, statement); statement = withBefores(frameworkMethod, testInstance, statement); statement = withAfters(frameworkMethod, testInstance, statement); statement = withPotentialTimeout(frameworkMethod, testInstance, statement); return statement; }
@Override protected Statement methodBlock(FrameworkMethod frameworkMethod) { testContext = new TestContextImpl(new HashMap<>(classAttributes), null); Object testInstance; try { testInstance = new ReflectiveCallable() { @Override protected Object runReflectiveCall() throws Throwable { return createTest(); } }.run(); } catch (Throwable ex) { return new Fail(ex); } Environment.inject(testInstance); Statement statement = methodInvoker(frameworkMethod, testInstance); statement = possiblyExpectingExceptions(frameworkMethod, testInstance, statement); statement = withBefores(frameworkMethod, testInstance, statement); statement = withAfters(frameworkMethod, testInstance, statement); statement = withPotentialTimeout(frameworkMethod, testInstance, statement); testContext = null; return statement; }
@Override protected Statement methodInvoker(final FrameworkMethod method, Object test) { if (canRunGuiTests()) { try { assertNotNull(myScreenshotTaker); return new MethodInvoker(method, test, myScreenshotTaker); } catch (Throwable e) { return new Fail(e); } } // Skip the test. return new Statement() { @Override public void evaluate() throws Throwable { String msg = String.format("Skipping test '%1$s'. UI tests cannot run in a headless environment.", method.getName()); System.out.println(msg); } }; }
protected Statement methodBlock( FuzzUnitTestMethod testMethod ) { 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( testMethod, test ); statement = withRules( testMethod, test, statement ); return statement; }
@Override protected synchronized Statement methodBlock(FrameworkMethod method) { FrameworkMethod newMethod = null; try { // Need the class from the custom loader now, so lets load the class. loadClassesWithCustomClassLoader( method ); // The method as parameter is from the original class and thus not found in our // class loaded by the custom name (reflection is class loader sensitive) // So find the same method but now in the class from the class Loader. newMethod = new FrameworkMethod( testClassFromClassLoader.getJavaClass().getMethod(method.getName())); } catch (Exception e) { // Show any problem nicely as a JUnit Test failure. return new Fail(e); } // We can carry out the normal JUnit functionality with our newly discoverd method now. return super.methodBlock(newMethod); }
/** * method taken as is from BlockJUnit4ClassRunner 4.7 in order to preserve * its functionality over following versions */ @Override protected Statement methodBlock(FrameworkMethod method) { 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(method, test); statement = possiblyExpectingExceptions(method, test, statement); statement = withPotentialTimeout(method, test, statement); statement = withRules(method, test, statement); statement = withBefores(method, test, statement); statement = withAfters(method, test, statement); return statement; }
@Override protected Statement methodBlock(FrameworkMethod method) { if (!(method instanceof ConcurrentFrameworkMethod)) { return super.methodBlock(method); } ConcurrentFrameworkMethod concurrentFrameworkMethod = (ConcurrentFrameworkMethod) method; Object test; try { test = createTestObject(); } catch (Throwable e) { return new Fail(e); } return buildStatements(concurrentFrameworkMethod, test); }
@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; }
@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 InvokeMethod(method, test); statement = withBefores(method, test, statement); statement = withAfters(method, test, statement); return statement; }
/** * had to override this whole method to get enough overall control to wrap * all method invocations... javadoc says that this can be overridden */ @Override protected Statement methodBlock(FrameworkMethod method) { 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(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; }
public void run(final TestClass testClass, final FrameworkMethod method, final RunNotifier notifier) { final Description description = describer.describe(method); if (method.getAnnotation(Ignore.class) != null) { notifier.fireTestIgnored(description); } else { try { final Stack<Class<?>> classHierarchy = getClassHierarchy(testClass.getJavaClass()); final Object target = createDeepInstance(classHierarchy); Statement statement = buildStatement(testClass, method, target, description, notifier); for (final MethodStatementBuilder builder : statementBuilders) { statement = builder.createStatement(testClass, method, target, statement, description, notifier); } statementExecutor.execute(statement, notifier, description); } catch (Throwable t) { statementExecutor.execute(new Fail(t), notifier, description); } } }
@Override protected Statement withBeforeClasses(Statement statement) { Object target; try { target = createTest(); } catch (Exception e) { return new Fail(e); } List<FieldAnnotationHandler> chain = annotationFieldProcessChain(target); statement = super.withBeforeClasses(statement); return new RunApimanInitAnnotations(statement, chain, false, true); }
@Override protected Statement withBefores(FrameworkMethod method, Object target, Statement statement) { try { resolveFailureCollectorRule(method); } catch (Exception e) { return new Fail(e); } return super.withBefores(method, target, statement); }
private Statement methodBlock(FrameworkMethod method) { Object testObject; try { testObject = new ReflectiveCallable() { @Override protected Object runReflectiveCall() throws Throwable { return getTestClass().getOnlyConstructor().newInstance(new Object[0]); } }.run(); } catch (Throwable throwable) { return new Fail(throwable); } return new InvokeMethod(method, testObject); }
/** * Augments the default JUnit behavior * {@link #withPotentialRepeat(FrameworkMethod, Object, Statement) with * potential repeats} of the entire execution chain. * <p>Furthermore, support for timeouts has been moved down the execution chain * in order to include execution of {@link org.junit.Before @Before} * and {@link org.junit.After @After} methods within the timed * execution. Note that this differs from the default JUnit behavior of * executing {@code @Before} and {@code @After} methods * in the main thread while executing the actual test method in a separate * thread. Thus, the end effect is that {@code @Before} and * {@code @After} methods will be executed in the same thread as * the test method. As a consequence, JUnit-specified timeouts will work * fine in combination with Spring transactions. Note that JUnit-specific * timeouts still differ from Spring-specific timeouts in that the former * execute in a separate thread while the latter simply execute in the main * thread (like regular tests). * @see #possiblyExpectingExceptions(FrameworkMethod, Object, Statement) * @see #withBefores(FrameworkMethod, Object, Statement) * @see #withAfters(FrameworkMethod, Object, Statement) * @see #withPotentialTimeout(FrameworkMethod, Object, Statement) * @see #withPotentialRepeat(FrameworkMethod, Object, Statement) */ @Override protected Statement methodBlock(FrameworkMethod frameworkMethod) { Object testInstance; try { testInstance = new ReflectiveCallable() { @Override protected Object runReflectiveCall() throws Throwable { return createTest(); } }.run(); } catch (Throwable ex) { return new Fail(ex); } Statement statement = methodInvoker(frameworkMethod, testInstance); statement = possiblyExpectingExceptions(frameworkMethod, testInstance, statement); statement = withBefores(frameworkMethod, testInstance, statement); statement = withAfters(frameworkMethod, testInstance, statement); statement = withRulesReflectively(frameworkMethod, testInstance, statement); statement = withPotentialRepeat(frameworkMethod, testInstance, statement); statement = withPotentialTimeout(frameworkMethod, testInstance, statement); return statement; }
@SuppressWarnings("deprecation") @Override protected Statement methodBlock(final FrameworkMethod method) { 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(method, test); statement = withTransaction(method, test, statement); statement = possiblyExpectingExceptions(method, test, statement); statement = withPotentialTimeout(method, test, statement); statement = withFieldsBinding(method, test, statement); statement = withContainer(method, test, statement); statement = withBefores(method, test, statement); statement = withAfters(method, test, statement); statement = withContext(method, test, statement); statement = withRootContainer(method, test, statement); statement = withClassLoader(statement); statement = withRules(method, test, statement); return statement; }
/** * Creates a new test instance * * @return new instance */ private Object newTestInstance() { try { return new ReflectiveCallable() { @Override protected Object runReflectiveCall() throws Throwable { return createTest(); } }.run(); } catch (final Throwable e) { return new Fail(e); } }
public void run(final TestClass testClass, final Class<?> clazz, final RunNotifier notifier) { try { new HierarchicalContextRunner(clazz).run(notifier); } catch (final Throwable t) { statementExecutor.execute(new Fail(t), notifier, describer.describe(clazz)); } }
@Test public void whenStatementExecutorThrowsException_failureIsReportedAtTheNotifier() throws Exception { doThrow(new RuntimeException("")).when(statementExecutor).execute(statement2, notifier, description); methodExecutor.run(testClass, method, notifier); verify(statementExecutor).execute(isA(Fail.class), same(notifier), same(description)); verifyNoMoreInteractions(notifier); }
private static Statement fail(String message){ return new Fail(new Exception(message)); }
/** * 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 {@code createTest()}, and * throw any exceptions thrown by either operation. * <li>HOWEVER, if {@code method}'s {@code @Test} annotation has the {@code * expecting} 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 org.junit.runners.model.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 org.junit.rules.TestRule} * </ul> * * This can be overridden in subclasses, either by overriding this method, * or the implementations creating each sub-statement. */ protected Statement methodBlock(FrameworkMethod method) { 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(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; }
@Override @SuppressWarnings("deprecation") protected Statement methodBlock(FrameworkMethod method) { Object test; try { test = new ReflectiveCallable() { @Override protected Object runReflectiveCall() throws Throwable { return createTest(); } }.run(); } catch (Throwable e) { return new Fail(e); } boolean validated = true; for (CdhServer cdhServer : getTestClass().getAnnotatedFieldValues(test, ClassRule.class, CdhServer.class)) { validated = validated && cdhServer.isValid(); if (validated) { for (CdhServer cdhServerDependency : cdhServer.getDependencies()) { validated = validated && cdhServerDependency.isValid(); } } } Statement statement; if (validated) { statement = methodInvoker(method, test); statement = withLogging(method, test, statement); statement = possiblyExpectingExceptions(method, test, statement); statement = withPotentialTimeout(method, test, statement); statement = withBefores(method, test, statement); statement = withAfters(method, test, statement); statement = withServerRules(method, test, statement); statement = withRules(method, test, statement); } else { statement = new Statement() { @Override public void evaluate() throws Throwable { if (LOG.isWarnEnabled()) { LOG.warn("Skipping [" + method.getDeclaringClass().getCanonicalName() + "." + method.getName() + "], invalid classpath"); } } }; } return statement; }
/** * 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 {@code createTest()}, and * throw any exceptions thrown by either operation. * <li>HOWEVER, if {@code method}'s {@code @Test} annotation has the {@code * expecting} 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(FrameworkMethod method) { 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(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; }