有谁知道如何在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
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)
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()随时打电话.:)
/**
* 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)
更新: AndroidTestCase此类在 API 级别 24 中已弃用。请InstrumentationRegistry改用。应使用 Android 测试支持库编写新测试。公告链接
您应该从 AndroidTestCase 而不是 TestCase 进行扩展。
AndroidTestCase 类概述
如果您需要访问资源或依赖于 Activity 上下文的其他内容,请扩展此类。
如果您想获取 Kotlin 和 Mockito 的上下文,可以通过以下方式进行:
val context = mock(Context::class.java)
Run Code Online (Sandbox Code Playgroud)
这是获取上下文的正确方法。其他方法已被弃用
import androidx.test.platform.app.InstrumentationRegistry
InstrumentationRegistry.getInstrumentation().context
Run Code Online (Sandbox Code Playgroud)
import androidx.test.core.app.ApplicationProvider;
private Context context = ApplicationProvider.getApplicationContext();
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
64475 次 |
| 最近记录: |