在Android junit测试用例中获取测试项目的上下文

pec*_*eps 100 junit android

有谁知道如何在Android junit测试用例中获得Test项目的上下文(扩展AndroidTestCase).

注意:测试不是仪器测试.

注2:我需要测试项目的上下文,而不是测试的实际应用程序的上下文.

我需要这个从测试项目的资产加载一些文件.

fad*_*a21 118

Android测试支持库(目前com.android.support.test:runner:0.3)发布了新方法.

class ExampleInstrumentedTest {

    lateinit var instrumentationContext: Context

    @Before
    fun setup() {
        instrumentationContext = InstrumentationRegistry.getInstrumentation().context
    }

    @Test
    fun someTest() {
        TODO()
    }
}
Run Code Online (Sandbox Code Playgroud)

如果您还想运行应用程序上下文:

InstrumentationRegistry.getInstrumentation().targetContext
Run Code Online (Sandbox Code Playgroud)

看这里:getTargetContext()和getContext(在InstrumentationRegistry上)有什么区别?

更新:androidx库的示例:https://github.com/fada21/AndroidTestContextExample

  • 最后是关于如何将 JUnit4 与 InstrumentationTest 结合使用的答案。经过几个小时的搜索。一定要热爱Android开发。 (3认同)
  • 它已被弃用,请改用`InstrumentationRegistry.getInstrumentation().context`。 (2认同)

Age*_*opf 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));
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 是的但是,在简单的JUnit测试中,Android无法访问测试项目上下文,这似乎很愚蠢.AndroidTestCase.mTestContext中存在上下文,但它是私有的.我不明白为什么. (7认同)

Tim*_*nin 25

正如您可以在AndroidTestCase源代码中读到的那样,该getTestContext()方法是隐藏的.

/**
 * @hide
 */
public Context getTestContext() {
    return mTestContext;
}
Run Code Online (Sandbox Code Playgroud)

您可以@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;
    }
}
Run Code Online (Sandbox Code Playgroud)

然后getTestContext()随时打电话.:)

  • 完美地为我工作,我使用AndroidTestCase的Context通过此方法加载资源,或者ActivityInstrumentationTestCase2.getInstrumentation().getContext()然后getResources().getAssets() (2认同)

Hit*_*ahu 8

@RunWith(AndroidJUnit4.class) 让您使用 Android Context

/**
 * 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());
    }


}
Run Code Online (Sandbox Code Playgroud)

您甚至可以使用在主线程上运行它runOnMainSync。这是完整的解决方案:

@RunWith(AndroidJUnit4::class)
class AwesomeViewModelTest {

    @Test
    fun testHandler() {

        getInstrumentation().runOnMainSync(Runnable {
            val context = InstrumentationRegistry.getInstrumentation().targetContext

          // Here you can call methods which have Handler

       
        })
    }


}
Run Code Online (Sandbox Code Playgroud)


esl*_*amb 5

更新: AndroidTestCase此类在 API 级别 24 中已弃用。请InstrumentationRegistry改用。应使用 Android 测试支持库编写新测试。公告链接

您应该从 AndroidTestCase 而不是 TestCase 进行扩展。

AndroidTestCase 类概述
如果您需要访问资源或依赖于 Activity 上下文的其他内容,请扩展此类。

AndroidTestCase - Android 开发者


Jua*_* T. 5

如果您想获取 Kotlin 和 Mockito 的上下文,可以通过以下方式进行:

val context = mock(Context::class.java)
Run Code Online (Sandbox Code Playgroud)


tom*_*ozb 5

这是获取上下文的正确方法。其他方法已被弃用

import androidx.test.platform.app.InstrumentationRegistry

InstrumentationRegistry.getInstrumentation().context
Run Code Online (Sandbox Code Playgroud)


eoi*_*nzy 5

import androidx.test.core.app.ApplicationProvider;

    private Context context = ApplicationProvider.getApplicationContext();
Run Code Online (Sandbox Code Playgroud)