Open warnyul opened 3 weeks ago
@warnyul Do you have evidence that it doesn't work as your expectation?
@utzcoz With jUnit 4 I do not have evidence, because in that case line 63 solves this issue in SandboxManager.java. Because the key contains the LooperMode
, 2 different sandboxes will be created with 2 different ClassLoader
s. One for PAUSED
and one for INSTRUMENTATION_TEST
mode.
I am only experiencing this issue with jUnit5, because I reuse the Sandbox
. in this case it is important that the shadows are reset. Because I cannot create new classloader per test method, because it is limited in jUnit 5. If I could create a new ClassLoader
that would solve the issue, like with jUnit 4. Because the ShadowPausedLooper
class is loaded with two different ClassLoader
s when I have two test methods annotated with different looper modes.
cc @brettchabot
Are you trying to reuse the same sandbox for some tests that use PAUSED looper mode (the default) and some tests that use INSTRUMENTATION_TEST looper mode? I am not sure if that is supported right now. @brettchabot would know more.
There are certain key parameters in Robolectric that will result in a new sandbox will be created. Looper mode is one of these. There is an LRU caching mechanism for sandboxes that tries to reuse sandboxes if possible.
public SandboxKey(
InstrumentationConfiguration instrumentationConfiguration,
Sdk sdk,
ResourcesMode resourcesMode,
LooperMode.Mode looperMode,
GraphicsMode.Mode graphicsMode) {
this.sdk = sdk;
this.instrumentationConfiguration = instrumentationConfiguration;
this.resourcesMode = resourcesMode;
this.looperMode = looperMode;
this.graphicsMode = graphicsMode;
}
So in the JUnit4 version of Robolectric, if a test runs with PAUSED looper mode, and then a subsequent test runs with INSTRUMENTATION_TEST looper mode, a new sandbox will be created.
@hoisie Yes, I am trying to reuse the same sandbox. I have some test and it seems working: RobolectricExtensionLooperModeSelfTest
In this case I just cleared the cached looper mode with reflection: https://github.com/apter-tech/junit5-robolectric-extension/pull/59/files#diff-f70ad8af7998e9e21db351e4d56ed03a59eef332f56919716375605a09e1a889R73
@warnyul Please rebase to the latest master branch to leverage bugfix for CI's Emulator testing.
Overview
The
unPause
is called fromresetLooperToInitialState
, which means the cached looper mode might already have been reset. I think inunPause
the looper mode should get from the registry.Proposed Changes
Use
ConfigurationRegistry.get(LooperMode.Mode.class)
instead oflooperMode()
in theunPause
method.