Search in sources :

Example 6 with TestContextManager

use of cn.taketoday.test.context.TestContextManager in project today-infrastructure by TAKETODAY.

the class ApplicationMethodRule method apply.

/**
 * Apply <em>instance-level</em> and <em>method-level</em> features of
 * the <em>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 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 ApplicationMethodRule to test method [{}]", testMethod);
    }
    Class<?> testClass = testInstance.getClass();
    TestContextManager testContextManager = ApplicationClassRule.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(cn.taketoday.test.context.TestContextManager)

Example 7 with TestContextManager

use of cn.taketoday.test.context.TestContextManager in project today-infrastructure by TAKETODAY.

the class ApplicationClassRule 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 ApplicationMethodRule} 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 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 ApplicationClassRule to test class [" + testClass.getName() + "]");
    }
    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(cn.taketoday.test.context.TestContextManager)

Example 8 with TestContextManager

use of cn.taketoday.test.context.TestContextManager in project today-infrastructure by TAKETODAY.

the class DisabledIfConditionTests method buildExtensionContext.

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

Aggregations

TestContextManager (cn.taketoday.test.context.TestContextManager)8 Method (java.lang.reflect.Method)4 Store (org.junit.jupiter.api.extension.ExtensionContext.Store)4 Statement (org.junit.runners.model.Statement)4 ExtensionContext (org.junit.jupiter.api.extension.ExtensionContext)2 FrameworkMethod (org.junit.runners.model.FrameworkMethod)2