我曾以这种方式在Robolectric 2.x中访问应用程序上下文,但不知道如何在Robolectric 3中进行。
return new ClassThatNeedsAContext(Robolectric.application.getApplicationContext());
return new ClassThatNeedsAContext(Robolectric.application.getApplicationContext());
[旧响应] 通过查看 Robolectric 的示例代码,也许我找到了解决方案。 https://github.com/robolectric/robolectric/blob/f4935bff58ae6a16b1230c3a415973e97713c0fb/robolectric/src/test/java/org/robolectric/shadows/ViewInnerTextTest.java#L22
return new ClassThatNeedsAContext(RuntimeEnvironment.application);
但是我在使用某些库时遇到了其他问题,所以我还无法测试它。