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);
}
}
};
}
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;
}
Aggregations