How get the context in Robolectric 3

Antonio Jose picture Antonio Jose · Jan 8, 2015 · Viewed 12.6k times · Source

I was able to access to the application context in Robolectric 2.x in this way but don't know how to do it in Robolectric 3.

return new ClassThatNeedsAContext(Robolectric.application.getApplicationContext());

Answer

Antonio Jose picture Antonio Jose · Jan 8, 2015

[edited]
Robolectric 2.4-to-3.0-Upgrade-Guide
https://github.com/robolectric/robolectric/wiki/2.4-to-3.0-Upgrade-Guide


[old response] Looking the Robolectric's sample code maybe I found the solution. https://github.com/robolectric/robolectric/blob/f4935bff58ae6a16b1230c3a415973e97713c0fb/robolectric/src/test/java/org/robolectric/shadows/ViewInnerTextTest.java#L22

return new ClassThatNeedsAContext(RuntimeEnvironment.application);

But I have other problems with some libraries so I was't unable to test it yet.