Search in sources :

Example 1 with Sandbox

use of org.robolectric.internal.bytecode.Sandbox in project robolectric by robolectric.

the class SandboxTestRunner method methodBlock.

protected Statement methodBlock(final FrameworkMethod method) {
    return new Statement() {

        @Override
        public void evaluate() throws Throwable {
            Sandbox sandbox = getSandbox(method);
            // Configure shadows *BEFORE* setting the ClassLoader. This is necessary because
            // creating the ShadowMap loads all ShadowProviders via ServiceLoader and this is
            // not available once we install the Robolectric class loader.
            configureShadows(method, sandbox);
            final ClassLoader priorContextClassLoader = Thread.currentThread().getContextClassLoader();
            Thread.currentThread().setContextClassLoader(sandbox.getRobolectricClassLoader());
            //noinspection unchecked
            Class bootstrappedTestClass = sandbox.bootstrappedClass(getTestClass().getJavaClass());
            HelperTestRunner helperTestRunner = getHelperTestRunner(bootstrappedTestClass);
            helperTestRunner.frameworkMethod = method;
            final Method bootstrappedMethod;
            try {
                //noinspection unchecked
                bootstrappedMethod = bootstrappedTestClass.getMethod(method.getMethod().getName());
            } catch (NoSuchMethodException e) {
                throw new RuntimeException(e);
            }
            try {
                // Only invoke @BeforeClass once per class
                invokeBeforeClass(bootstrappedTestClass);
                beforeTest(sandbox, method, bootstrappedMethod);
                final Statement statement = helperTestRunner.methodBlock(new FrameworkMethod(bootstrappedMethod));
                // todo: this try/finally probably isn't right -- should mimic RunAfters? [xw]
                try {
                    statement.evaluate();
                } finally {
                    afterTest(method, bootstrappedMethod);
                }
            } finally {
                Thread.currentThread().setContextClassLoader(priorContextClassLoader);
                finallyAfterTest(method);
            }
        }
    };
}
Also used : Statement(org.junit.runners.model.Statement) URLClassLoader(java.net.URLClassLoader) SandboxClassLoader(org.robolectric.internal.bytecode.SandboxClassLoader) BeforeClass(org.junit.BeforeClass) TestClass(org.junit.runners.model.TestClass) AfterClass(org.junit.AfterClass) Method(java.lang.reflect.Method) FrameworkMethod(org.junit.runners.model.FrameworkMethod) FrameworkMethod(org.junit.runners.model.FrameworkMethod) Sandbox(org.robolectric.internal.bytecode.Sandbox)

Example 2 with Sandbox

use of org.robolectric.internal.bytecode.Sandbox in project robolectric by robolectric.

the class SandboxTestRunner method getSandbox.

@NotNull
protected Sandbox getSandbox(FrameworkMethod method) {
    InstrumentationConfiguration instrumentationConfiguration = createClassLoaderConfig(method);
    URLClassLoader systemClassLoader = (URLClassLoader) ClassLoader.getSystemClassLoader();
    ClassLoader sandboxClassLoader = new SandboxClassLoader(systemClassLoader, instrumentationConfiguration);
    Sandbox sandbox = new Sandbox(sandboxClassLoader);
    configureShadows(method, sandbox);
    return sandbox;
}
Also used : InstrumentationConfiguration(org.robolectric.internal.bytecode.InstrumentationConfiguration) URLClassLoader(java.net.URLClassLoader) URLClassLoader(java.net.URLClassLoader) SandboxClassLoader(org.robolectric.internal.bytecode.SandboxClassLoader) SandboxClassLoader(org.robolectric.internal.bytecode.SandboxClassLoader) Sandbox(org.robolectric.internal.bytecode.Sandbox) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

URLClassLoader (java.net.URLClassLoader)2 Sandbox (org.robolectric.internal.bytecode.Sandbox)2 SandboxClassLoader (org.robolectric.internal.bytecode.SandboxClassLoader)2 Method (java.lang.reflect.Method)1 NotNull (org.jetbrains.annotations.NotNull)1 AfterClass (org.junit.AfterClass)1 BeforeClass (org.junit.BeforeClass)1 FrameworkMethod (org.junit.runners.model.FrameworkMethod)1 Statement (org.junit.runners.model.Statement)1 TestClass (org.junit.runners.model.TestClass)1 InstrumentationConfiguration (org.robolectric.internal.bytecode.InstrumentationConfiguration)1