use of org.junit.runners.model.Statement in project spring-boot by spring-projects.
the class WebMvcTestPrintDefaultRunner method methodBlock.
@Override
protected Statement methodBlock(FrameworkMethod frameworkMethod) {
Statement statement = super.methodBlock(frameworkMethod);
statement = new AlwaysPassStatement(statement);
OutputCapture outputCapture = new OutputCapture();
if (frameworkMethod.getName().equals("shouldPrint")) {
outputCapture.expect(containsString("HTTP Method"));
} else if (frameworkMethod.getName().equals("shouldNotPrint")) {
outputCapture.expect(not(containsString("HTTP Method")));
} else {
throw new IllegalStateException("Unexpected test method");
}
System.err.println(frameworkMethod.getName());
return outputCapture.apply(statement, null);
}
use of org.junit.runners.model.Statement in project spring-framework by spring-projects.
the class SpringJUnit4ClassRunner method methodBlock.
/**
* 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 #methodInvoker(FrameworkMethod, Object)
* @see #withBeforeTestExecutionCallbacks(FrameworkMethod, Object, Statement)
* @see #withAfterTestExecutionCallbacks(FrameworkMethod, Object, Statement)
* @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 = withBeforeTestExecutionCallbacks(frameworkMethod, testInstance, statement);
statement = withAfterTestExecutionCallbacks(frameworkMethod, testInstance, statement);
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;
}
use of org.junit.runners.model.Statement in project spring-framework by spring-projects.
the class SpringJUnit4ClassRunner method runChild.
/**
* Perform the same logic as
* {@link BlockJUnit4ClassRunner#runChild(FrameworkMethod, RunNotifier)},
* except that tests are determined to be <em>ignored</em> by
* {@link #isTestMethodIgnored(FrameworkMethod)}.
*/
@Override
protected void runChild(FrameworkMethod frameworkMethod, RunNotifier notifier) {
Description description = describeChild(frameworkMethod);
if (isTestMethodIgnored(frameworkMethod)) {
notifier.fireTestIgnored(description);
} else {
Statement statement;
try {
statement = methodBlock(frameworkMethod);
} catch (Throwable ex) {
statement = new Fail(ex);
}
runLeaf(statement, description, notifier);
}
}
use of org.junit.runners.model.Statement in project spring-framework by spring-projects.
the class SpringClassRule method apply.
/**
* Apply <em>class-level</em> features of the <em>Spring TestContext
* Framework</em> to the supplied {@code base} statement.
* <p>Specifically, this method retrieves the {@link TestContextManager}
* used by this rule and its associated {@link SpringMethodRule} and
* invokes the {@link TestContextManager#beforeTestClass() beforeTestClass()}
* and {@link TestContextManager#afterTestClass() afterTestClass()} methods
* on the {@code TestContextManager}.
* <p>In addition, this method checks whether the test is enabled in
* the current execution environment. This prevents classes with a
* non-matching {@code @IfProfileValue} annotation from running altogether,
* even skipping the execution of {@code beforeTestClass()} methods
* in {@code TestExecutionListeners}.
* @param base the base {@code Statement} that this rule should be applied to
* @param description a {@code Description} of the current test execution
* @return a statement that wraps the supplied {@code base} with class-level
* features of the Spring TestContext Framework
* @see #getTestContextManager
* @see #withBeforeTestClassCallbacks
* @see #withAfterTestClassCallbacks
* @see #withProfileValueCheck
* @see #withTestContextManagerCacheEviction
*/
@Override
public Statement apply(Statement base, Description description) {
Class<?> testClass = description.getTestClass();
if (logger.isDebugEnabled()) {
logger.debug("Applying SpringClassRule to test class [" + testClass.getName() + "]");
}
validateSpringMethodRuleConfiguration(testClass);
TestContextManager testContextManager = getTestContextManager(testClass);
Statement statement = base;
statement = withBeforeTestClassCallbacks(statement, testContextManager);
statement = withAfterTestClassCallbacks(statement, testContextManager);
statement = withProfileValueCheck(statement, testClass);
statement = withTestContextManagerCacheEviction(statement, testClass);
return statement;
}
use of org.junit.runners.model.Statement in project spock by spockframework.
the class ClassRuleInterceptor method intercept.
public void intercept(final IMethodInvocation invocation) throws Throwable {
Statement stat = createBaseStatement(invocation);
for (FieldInfo field : ruleFields) {
TestRule rule = (TestRule) getRuleInstance(field, field.isShared() ? invocation.getSharedInstance() : invocation.getInstance());
stat = rule.apply(stat, invocation.getSpec().getDescription());
}
stat.evaluate();
}
Aggregations