java 如何在 Robolectric 3 中获取上下文

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27831272/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 12:27:48  来源:igfitidea点击:

How get the context in Robolectric 3

javarobolectric

提问by Antonio Jose

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.

我能够以这种方式访问​​ Robolectric 2.x 中的应用程序上下文,但不知道如何在 Robolectric 3 中执行此操作。

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

回答by Antonio Jose

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

[编辑]
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

[旧回复] 查看 Robolectric 的示例代码,也许我找到了解决方案。 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.

但是我对一些库还有其他问题,所以我还不能测试它。