Search in sources :

Example 1 with TestContextManager

use of org.springframework.test.context.TestContextManager 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;
}
Also used : Statement(org.junit.runners.model.Statement) TestContextManager(org.springframework.test.context.TestContextManager)

Example 2 with TestContextManager

use of org.springframework.test.context.TestContextManager in project spring-framework by spring-projects.

the class DisabledIfConditionTestCase method buildExtensionContext.

// -------------------------------------------------------------------------
private TestExtensionContext buildExtensionContext(String methodName) {
    Class<?> testClass = SpringTestCase.class;
    Method method = ReflectionUtils.findMethod(getClass(), methodName);
    Store store = mock(Store.class);
    when(store.getOrComputeIfAbsent(any(), any(), any())).thenReturn(new TestContextManager(testClass));
    TestExtensionContext extensionContext = mock(TestExtensionContext.class);
    when(extensionContext.getTestClass()).thenReturn(Optional.of(testClass));
    when(extensionContext.getElement()).thenReturn(Optional.of(method));
    when(extensionContext.getStore(any())).thenReturn(store);
    return extensionContext;
}
Also used : Store(org.junit.jupiter.api.extension.ExtensionContext.Store) Method(java.lang.reflect.Method) TestContextManager(org.springframework.test.context.TestContextManager) TestExtensionContext(org.junit.jupiter.api.extension.TestExtensionContext)

Example 3 with TestContextManager

use of org.springframework.test.context.TestContextManager in project spring-framework by spring-projects.

the class SpringMethodRule method apply.

/**
	 * Apply <em>instance-level</em> and <em>method-level</em> features of
	 * the <em>Spring TestContext Framework</em> to the supplied {@code base}
	 * statement.
	 * <p>Specifically, this method invokes the
	 * {@link TestContextManager#prepareTestInstance prepareTestInstance()},
	 * {@link TestContextManager#beforeTestMethod beforeTestMethod()}, and
	 * {@link TestContextManager#afterTestMethod afterTestMethod()} methods
	 * on the {@code TestContextManager}, potentially with Spring timeouts
	 * and repetitions.
	 * <p>In addition, this method checks whether the test is enabled in
	 * the current execution environment. This prevents methods with a
	 * non-matching {@code @IfProfileValue} annotation from running altogether,
	 * even skipping the execution of {@code prepareTestInstance()} methods
	 * in {@code TestExecutionListeners}.
	 * @param base the base {@code Statement} that this rule should be applied to
	 * @param frameworkMethod the method which is about to be invoked on the test instance
	 * @param testInstance the current test instance
	 * @return a statement that wraps the supplied {@code base} with instance-level
	 * and method-level features of the Spring TestContext Framework
	 * @see #withBeforeTestMethodCallbacks
	 * @see #withAfterTestMethodCallbacks
	 * @see #withPotentialRepeat
	 * @see #withPotentialTimeout
	 * @see #withTestInstancePreparation
	 * @see #withProfileValueCheck
	 */
@Override
public Statement apply(Statement base, FrameworkMethod frameworkMethod, Object testInstance) {
    Method testMethod = frameworkMethod.getMethod();
    if (logger.isDebugEnabled()) {
        logger.debug("Applying SpringMethodRule to test method [" + testMethod + "]");
    }
    Class<?> testClass = testInstance.getClass();
    validateSpringClassRuleConfiguration(testClass);
    TestContextManager testContextManager = SpringClassRule.getTestContextManager(testClass);
    Statement statement = base;
    statement = withBeforeTestMethodCallbacks(statement, testMethod, testInstance, testContextManager);
    statement = withAfterTestMethodCallbacks(statement, testMethod, testInstance, testContextManager);
    statement = withTestInstancePreparation(statement, testInstance, testContextManager);
    statement = withPotentialRepeat(statement, testMethod, testInstance);
    statement = withPotentialTimeout(statement, testMethod, testInstance);
    statement = withProfileValueCheck(statement, testMethod, testInstance);
    return statement;
}
Also used : Statement(org.junit.runners.model.Statement) FrameworkMethod(org.junit.runners.model.FrameworkMethod) Method(java.lang.reflect.Method) TestContextManager(org.springframework.test.context.TestContextManager)

Example 4 with TestContextManager

use of org.springframework.test.context.TestContextManager in project spring-framework by spring-projects.

the class SpringExtension method getTestContextManager.

/**
	 * Get the {@link TestContextManager} associated with the supplied {@code ExtensionContext}.
	 * @return the {@code TestContextManager} (never {@code null})
	 */
private static TestContextManager getTestContextManager(ExtensionContext context) {
    Assert.notNull(context, "ExtensionContext must not be null");
    Class<?> testClass = context.getTestClass().get();
    Store store = context.getStore(NAMESPACE);
    return store.getOrComputeIfAbsent(testClass, TestContextManager::new, TestContextManager.class);
}
Also used : Store(org.junit.jupiter.api.extension.ExtensionContext.Store) TestContextManager(org.springframework.test.context.TestContextManager)

Aggregations

TestContextManager (org.springframework.test.context.TestContextManager)4 Method (java.lang.reflect.Method)2 Store (org.junit.jupiter.api.extension.ExtensionContext.Store)2 Statement (org.junit.runners.model.Statement)2 TestExtensionContext (org.junit.jupiter.api.extension.TestExtensionContext)1 FrameworkMethod (org.junit.runners.model.FrameworkMethod)1