Skip to content

Instantly share code, notes, and snippets.

@emmano
Created April 20, 2020 21:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save emmano/403f25c5d0216675c3bbce0c4d3e545f to your computer and use it in GitHub Desktop.
Save emmano/403f25c5d0216675c3bbce0c4d3e545f to your computer and use it in GitHub Desktop.
Portion of SandboxTestRunner that reloads tests using reflection and custom ClassLoader
sandbox.runOnMainThread(() -> {
ClassLoader priorContextClassLoader = Thread.currentThread().getContextClassLoader();
Thread.currentThread().setContextClassLoader(sandbox.getRobolectricClassLoader());
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);
initialization.finished();
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);
}
} catch (Throwable throwable) {
throw Util.sneakyThrow(throwable);
} finally {
Thread.currentThread().setContextClassLoader(priorContextClassLoader);
try {
finallyAfterTest(method);
} catch (Exception e) {
e.printStackTrace();
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment