Package net.thucydides.junit.spring

Source Code of net.thucydides.junit.spring.SpringIntegration

package net.thucydides.junit.spring;


import org.junit.rules.TestWatchman;
import org.junit.runners.model.FrameworkMethod;
import org.junit.runners.model.Statement;
import org.springframework.test.context.TestContextManager;

/**
* Use the Spring test annotations in Thucydides tests.
*
* @author johnsmart
*/
public class SpringIntegration extends TestWatchman {

    public SpringIntegration() {
    }

    @Override
    public Statement apply(Statement base, FrameworkMethod method, Object target) {
        TestContextManager contextManager = getTestContextManager(method.getMethod().getDeclaringClass());
        try {
            contextManager.prepareTestInstance(target);
        } catch (Exception e) {
            throw new IllegalStateException("Could not instantiate test instance", e);
        }
        Statement statement = super.apply(base, method, target);
        statement = withBefores(method, target, statement, contextManager);
        statement = withAfters(method, target, statement, contextManager);
        return statement;
    }

    protected TestContextManager getTestContextManager(Class<?> clazz) {
        return new TestContextManager(clazz);
    }

    protected Statement withBefores(FrameworkMethod frameworkMethod,
                                    Object testInstance,
                                    Statement statement,
                                    TestContextManager testContextManager) {
        return new RunBeforeTestMethodCallbacks(statement,
                testInstance,
                frameworkMethod.getMethod(),
                testContextManager);
    }

    protected Statement withAfters(FrameworkMethod frameworkMethod,
                                   Object testInstance,
                                   Statement statement,
                                   TestContextManager testContextManager) {
        return new RunAfterTestMethodCallbacks(statement,
                testInstance,
                frameworkMethod.getMethod(),
                testContextManager);
    }

}
TOP

Related Classes of net.thucydides.junit.spring.SpringIntegration

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.