Created
April 20, 2020 21:36
-
-
Save emmano/403f25c5d0216675c3bbce0c4d3e545f to your computer and use it in GitHub Desktop.
Portion of SandboxTestRunner that reloads tests using reflection and custom ClassLoader
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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