有谁知道如何在Android junit测试用例(扩展AndroidTestCase)中获取测试项目的上下文。
注意:测试不是仪器测试。
注2:我需要测试项目的上下文,而不是测试的实际应用程序的上下文。
我需要这个来从测试项目的资产中加载一些文件。
答案 0 :(得分:107)
Android测试支持库(目前class ExampleInstrumentedTest {
lateinit var instrumentationContext: Context
@Before
fun setup() {
instrumentationContext = InstrumentationRegistry.getInstrumentation().context
}
@Test
fun someTest() {
TODO()
}
}
)采用了新方法。 Kotlin更新了例子:
InstrumentationRegistry.getInstrumentation().targetContext
如果您还想运行应用程序上下文:
<MultiBinding Converter="{converters:SkipRecords}">
<Binding ArrivalDepartures />
<Binding ElementName=tbTotalRows Path="Index"/>
</MultiBinding>
完整运行示例:https://github.com/fada21/AndroidTestContextExample
请看这里:What's the difference between getTargetContext() and getContext (on InstrumentationRegistry)?
答案 1 :(得分:37)
经过一番研究,唯一可行的解决方案似乎是yorkw已经指出的那个。您必须扩展InstrumentationTestCase然后使用getInstrumentation()来访问测试应用程序的上下文.getContext() - 这是一个使用上述建议的简短代码片段:
public class PrintoutPullParserTest extends InstrumentationTestCase {
public void testParsing() throws Exception {
PrintoutPullParser parser = new PrintoutPullParser();
parser.parse(getInstrumentation().getContext().getResources().getXml(R.xml.printer_configuration));
}
}
答案 2 :(得分:25)
正如您在AndroidTestCase source code中所读到的那样,隐藏了getTestContext()
方法。
/**
* @hide
*/
public Context getTestContext() {
return mTestContext;
}
您可以使用反射绕过@hide
注释。
只需在AndroidTestCase
中添加以下方法:
/**
* @return The {@link Context} of the test project.
*/
private Context getTestContext()
{
try
{
Method getTestContext = ServiceTestCase.class.getMethod("getTestContext");
return (Context) getTestContext.invoke(this);
}
catch (final Exception exception)
{
exception.printStackTrace();
return null;
}
}
然后随时拨打getTestContext()
。 :)
答案 3 :(得分:4)
更新: AndroidTestCase
此类已在API级别24中弃用。
请改用InstrumentationRegistry
。应使用Android测试支持库编写新测试。 Link to announcement
您应该从AndroidTestCase而不是TestCase扩展。
AndroidTestCase类概述
如果您需要访问依赖于活动上下文的资源或其他内容,请对此进行扩展。
答案 4 :(得分:2)
如果要获取Kotlin和Mockito的上下文,可以按照以下方式进行操作:
val context = mock(Context::class.java)
希望它对您有帮助
答案 5 :(得分:1)
这是获取上下文的正确方法。其他方法已被弃用
import androidx.test.platform.app.InstrumentationRegistry
InstrumentationRegistry.getInstrumentation().context
答案 6 :(得分:0)
如果您只需要访问项目资源,可以使用ActivityInstrumentationTestCase2 class的 getActivity()方法:
//...
private YourActivityClass mActivity;
@Override
protected void setUp() throws Exception {
//...
mActivity = getActivity();
}
//...
public void testAccessToResources() {
String[] valueList;
valueList =
mActivity.getResources().getStringArray(
com.yourporject.R.array.test_choices);
}
答案 7 :(得分:0)
其他答案已过时。现在,每次扩展AndroidTestCase时,都可以使用mContext Context对象。
答案 8 :(得分:0)
您可以使用Robolectric
在JVM上进行Android单元测试。
Robolectric
是一个框架,可让您编写单元测试并在仍使用Android API的情况下在桌面JVM上运行它们。
Robolectric
提供了android.jar文件的JVM兼容版本。 Robolectric处理视图膨胀,资源加载以及许多其他在Android设备上以本机C代码实现的内容。
dependencies {
...
// Robolectric
testCompile "org.robolectric:robolectric:3.3.2"
}
src/test
目录中。 @RunWith(RobolectricTestRunner.class
测试运行者注释。例如
@RunWith(RobolectricTestRunner.class)
@Config(constants = BuildConfig.class)
public class WelcomeActivityTest
{
private WelcomeActivity activity;
@Before
public void setUp() throws Exception
{
activity = Robolectric.buildActivity( WelcomeActivity.class )
.create()
.resume()
.get();
}
@Test
public void shouldNotBeNull() throws Exception
{
assertNotNull( activity );
}
}
了解更多here
答案 9 :(得分:0)
import androidx.test.core.app.ApplicationProvider;
private Context context = ApplicationProvider.getApplicationContext();
答案 10 :(得分:0)
对于那些在创建自动化测试时遇到这些问题的人,您必须这样做:
Context instrumentationContext;
@Before
public void method() {
instrumentationContext = InstrumentationRegistry.getInstrumentation().getContext();
MultiDex.install(instrumentationContext);
}
答案 11 :(得分:0)
@RunWith(AndroidJUnit4.class)允许您使用Android上下文
/**
* Instrumented test, which will execute on an Android device.
*
* @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
*/
@RunWith(AndroidJUnit4.class)
public class ExampleInstrumentedTest {
@Test
public void useAppContext() {
// Context of the app under test.
Context appContext = InstrumentationRegistry.getInstrumentation().getTargetContext();
assertEquals("com.android.systemui", appContext.getPackageName());
}
}