diff options
author | Paul Sowden <paulsowden@google.com> | 2022-06-06 18:07:14 -0700 |
---|---|---|
committer | Copybara-Service <copybara-worker@google.com> | 2022-06-06 18:07:44 -0700 |
commit | c5a926c6c90ae346bbd0b50d1df8e206f9f8e084 (patch) | |
tree | 9eae018b7089d3d6e6dfff2ccd3f70d86887a999 /junit | |
parent | 76dcd08748c9c2ef5ac1bfde43978d38d1f20823 (diff) | |
download | robolectric-c5a926c6c90ae346bbd0b50d1df8e206f9f8e084.tar.gz |
Cache the helper runners per sandbox
Don't create a new helper runner for every test, just create one per sandbox. In Robolectric the test runner is pretty lightweight but subclasses may wish to store state or perform non-trivial work in helpers, avoid creating new instances where it is unnecessary.
Also factors out the exception handling, by default the runner baseclass can throw an exception on construction so every subclass had to manually handle this when creating the helper runner, instead allow the protected method to just raise the exception and handle it when calling the method.
PiperOrigin-RevId: 453325195
Diffstat (limited to 'junit')
-rw-r--r-- | junit/src/main/java/org/robolectric/internal/SandboxTestRunner.java | 24 |
1 files changed, 17 insertions, 7 deletions
diff --git a/junit/src/main/java/org/robolectric/internal/SandboxTestRunner.java b/junit/src/main/java/org/robolectric/internal/SandboxTestRunner.java index d3c3115f9..05ba48620 100644 --- a/junit/src/main/java/org/robolectric/internal/SandboxTestRunner.java +++ b/junit/src/main/java/org/robolectric/internal/SandboxTestRunner.java @@ -64,6 +64,7 @@ public class SandboxTestRunner extends BlockJUnit4ClassRunner { private final List<PerfStatsReporter> perfStatsReporters; private final HashMap<Class<?>, Sandbox> loadedTestClasses = new HashMap<>(); + private final HashMap<Class<?>, HelperTestRunner> helperRunners = new HashMap<>(); public SandboxTestRunner(Class<?> klass) throws InitializationError { this(klass, DEFAULT_INJECTOR); @@ -253,7 +254,7 @@ public class SandboxTestRunner extends BlockJUnit4ClassRunner { Class<?> bootstrappedTestClass = sandbox.bootstrappedClass(getTestClass().getJavaClass()); - HelperTestRunner helperTestRunner = getHelperTestRunner(bootstrappedTestClass); + HelperTestRunner helperTestRunner = getCachedHelperTestRunner(bootstrappedTestClass); helperTestRunner.frameworkMethod = method; final Method bootstrappedMethod; @@ -327,12 +328,21 @@ public class SandboxTestRunner extends BlockJUnit4ClassRunner { protected void finallyAfterTest(FrameworkMethod method) {} - protected HelperTestRunner getHelperTestRunner(Class<?> bootstrappedTestClass) { - try { - return new HelperTestRunner(bootstrappedTestClass); - } catch (InitializationError initializationError) { - throw new RuntimeException(initializationError); - } + protected HelperTestRunner getHelperTestRunner(Class<?> bootstrappedTestClass) + throws InitializationError { + return new HelperTestRunner(bootstrappedTestClass); + } + + private HelperTestRunner getCachedHelperTestRunner(Class<?> bootstrappedTestClass) { + return helperRunners.computeIfAbsent( + bootstrappedTestClass, + klass -> { + try { + return getHelperTestRunner(klass); + } catch (InitializationError e) { + throw new RuntimeException(e); + } + }); } protected static class HelperTestRunner extends BlockJUnit4ClassRunner { |