3

I need to customize the RobolectricTestRunner from Robolectric project, but I found a strange behaviour that I don't understand:

RobolectricTestRunner contains the methodBlock which calls setUpApplicationState(bootstrappedMethod, parallelUniverseInterface, strictI18n, systemResourceLoader, appManifest, config); the method executes setUpApplicationState of ParallelUniverse class.

At line 146, setupApplicationState executes application.onCreate(); When I debug the application, when I check the Robolectric.application value at line 146 I see that it's created successfully, but if I check the same value in the RobolectricTestRunner class, after the line: 220, Robolectric.application is null... Why? it's a static value.. I can't understand that!

M-S
  • 617
  • 7
  • 19

2 Answers2

1

I found the answer to my question: parallelUniverse is created by calling the sdkEnvironment.getRobolectricClassLoader() which use a different classLoader: AsmInstrumentingClassLoader, RobolectricTestRunner is part of a different classLoader, so I can't access to the same object

M-S
  • 617
  • 7
  • 19
0

I am posting this, if this might help you How can we access context of an application in Robolectric?

And also consider answer by @EugenMartynov having 100+ upvotes

Just use for version 1.x and 2.x:

Robolectric.application;

And for version 3.x:

RuntimeEnvironment.application;

Hope this helps you....

Community
  • 1
  • 1
Bhuro
  • 348
  • 4
  • 22